#!/bin/sh
# Gets Debian and Emdebian binary package versions
# 
# TODO: 
#  - Would be useful to create a database?
#  - HTML Output
#  - Need to run update

# Configuration variables

source conf_vars.sh

# Package name
PKG=$1
ARCH=$2

# Default values
DEB_VERSION=4.0
EM_VERSION=0.0

# Finds Debian version 
DEB_VERSION=`rmadison $PKG | grep $ARCH | grep $SUITE | cut -d'|' -f2 | tr -d ' '`

# Create a file with toolchain binary packages tree 
# Finds emdebian latest build version
# FIXME: What happens when there is no page?
#        Should be updated to which Emdebian DB?
ls -1 /jails/$HOST-$SUITE/home/toolchain/trunk/$ARCH/*deb > /tmp/$HOST-$ARCH-$SUITE.txt
EM_VERSION=`cat /tmp/$HOST-$ARCH-$SUITE.txt | grep $PKG | cut -d'/' -f8 | cut -d'_' -f2`

if [ -n $EM_VERSION ]; then
	echo "FLAG: 1"
	echo "$ARCH $PKG - out sync"
elif [ $DEB_VERSION == $EM_VERSION ]; then
# Compares both versions
	echo "FLAG: 0"
	echo "$ARCH $PKG - in sync"
else
	echo "FLAG: 1"
	echo "$ARCH $PKG - out sync"
fi
echo "DEB_VERSION: $DEB_VERSION"
echo " EM_VERSION: $EM_VERSION"

# Do clean up
rm -rf /tmp/$HOST-$SUITE.txt
