#!/bin/sh -e
#
# Script that outputs toolchain status
#
# Hector Oron for Emdebian
#

source conf_vars.sh

REPREPRO_PATH=/var/emdebian/debian/

PKGLIST="linux-libc-dev libc6 libc6.1 libc6-dev binutils gcc-3.3 gcc-3.4 gcc-4.1 gcc-4.2 gcc-4.3"
# ARCHLIST="arm armel mips mipsel powerpc sparc s390 amd64 m68k hppa alpha ia64 i386"
SUITE=unstable
COMPONENT=main
# It can be i386, amd64, powerpc
ARCHOST=$1


for PKG in $PKGLIST; do

	debian_current=`rmadison $PKG | grep $SUITE | cut -d'|' -f2 | tr -d ' ' | tail -n 1`

	for ARCH in $ARCHLIST; do

		case $PKG in
		  linux-libc-dev)
		  emdebian_current=`reprepro -Vb $REPREPRO_PATH -C $COMPONENT list $SUITE $PKG-$ARCH-cross | grep $ARCHOST | cut -d' ' -f3 | tail -n 1`
		  ;;
		  libc6|libc6.1)
		  emdebian_current=`reprepro -Vb $REPREPRO_PATH -C $COMPONENT list $SUITE $PKG-$ARCH-cross | grep $ARCHOST | cut -d' ' -f3 | tail -n 1`
		  ;;
		  libc6-dev)
		  emdebian_current=`reprepro -Vb $REPREPRO_PATH -C $COMPONENT list $SUITE $PKG-$ARCH-cross | grep $ARCHOST | cut -d' ' -f3 | tail -n 1`
		  ;;
		  *)
		  if [ $ARCH == "armel" ]; then
		    emdebian_current=`reprepro -Vb $REPREPRO_PATH -C $COMPONENT list $SUITE $PKG-arm-linux-gnueabi | grep $ARCHOST | cut -d' ' -f3 | tail -n 1`
		  else
		    emdebian_current=`reprepro -Vb $REPREPRO_PATH -C $COMPONENT list $SUITE $PKG-$ARCH-linux-gnu | grep $ARCHOST | cut -d' ' -f3 | tail -n 1`
		  fi
		  ;;
		esac
	
	  if [ -z $emdebian_current ]; then
	  	FLAG="OUTDATED"
#	  elif [ $debian_current = $emdebian_current ]; then
	  elif dpkg --compare-versions $debian_current gt $emdebian_current; then
	  	FLAG="OUTDATED"
	  else 
	  	FLAG="U"
	  fi
	  echo "PKG: $PKG - ARCH: $ARCH - DEBIAN VERSION: $debian_current - EMDEBIAN VERSION: $emdebian_current $FLAG"		
	done
done
