#!/bin/sh
# $Id: update_freesbie_pkg.sh,v 1.6 2004/10/22 18:43:57 drizzt Exp $

. ../../config.sh

if [ "$RELEASE" -ge "5" ]
then
	#We have to copy pkg_update in $FREESBIEBASEDIR/usr/sbin
	#if it doesn't exist
	if [ ! -x "$FREESBIEBASEDIR/usr/sbin/pkg_update" ]; then
		cp ./pkg_update $FREESBIEBASEDIR/usr/sbin
	fi
fi
echo -n "Creating list of installed packages in your REAL system... "
[ -f list.real.orig ] && rm list.real.orig
pkg_info -oa | awk \
    '{
        if ($1=="Information")
         {
				pack=substr($3,1,length($3)-1);
				getline; getline; getline;
				origin=$1;
				system("echo " pack "#" origin "# >> list.real.orig"); 
         } 
         }'

echo " [OK]"
echo -n "Creating list of installed packages in your LIVE system... "
[ -f list.live.orig ] && rm list.live.orig
chroot ${FREESBIEBASEDIR} pkg_info -oa | awk \
        '{
                 if ($1=="Information")
                 {
                     pack=substr($3,1,length($3)-1);
                     getline; getline; getline;
                     origin=$1;
                     system("echo " pack "#" origin "# >> list.live.orig");
			}
         }'

[ -f list.live.temp ] && rm list.live.temp
cat list.live.orig | awk -F '#' \
    '{
        pack=$1;
	    system("echo " pack " >> list.live.temp"); 
    }'

if [ ! -s list.live.temp ]; then
    echo "No packages found in LIVE system."
    exit
fi    

echo " [OK]"
echo -n "Creating list of packages to update... "

LIVE_ORIG=`cat list.live.orig | awk -F '#' '{print $2}'`
for i in $LIVE_ORIG
do
    LIVE_NAME=`grep ${i}# list.live.orig | awk -F '#' '{print $1}'`
    REAL_NAME=`grep ${i}# list.real.orig | awk -F '#' '{print $1}'`
    if [ "${REAL_NAME}" = "" ]; then
		echo ""
		echo "${LIVE_NAME} is not installed in your REAL system."
		echo "Aborting."
		rm list.live.temp
		rm list.live.orig
		rm list.real.orig
		exit
	fi
    LIVE_NAME=`grep ${i}# list.live.orig | awk -F '#' '{print $1}'`
    if [ "${REAL_NAME}" != "${LIVE_NAME}" ]; then
        echo "$REAL_NAME" >> list.diff
    fi
done
echo " [OK]"
rm list.live.orig
rm list.real.orig
if [ ! -s list.diff ]; then
	echo "No updates available"
	rm list.live.temp
	exit
fi
echo "Installing packages..."
#
# Only list.live.temp and list.diff are necessary
#
ADDLIST=""
for i in `sed s/@/-/ < list.diff`; do ADDLIST="$ADDLIST $i"; done
for i in `pkg_info -qr $ADDLIST | cut -d ' ' -f2 | sort | uniq`; do
    RESULT_A=`echo $ADDLIST | grep $i`
    RESULT_B=`grep $i list.live.temp`
    [ "$RESULT_A" = "" ] && [ "$RESULT_B" = "" ] && ADDLIST="$ADDLIST $i"
done
for i in `pkg_info -qr $ADDLIST | cut -d ' ' -f2 | sort | uniq`; do
    RESULT_A=`echo $ADDLIST | grep $i`
    RESULT_B=`grep $i list.live.temp`
    [ "$RESULT_A" = "" ] && [ "$RESULT_B" = "" ] &&  ADDLIST="$ADDLIST $i"
done
BACK=$PWD; cd $FREESBIEBASEDIR
for i in $ADDLIST; do pkg_create -zvb $i; done
for i in `ls *.tgz`; do chroot $FREESBIEBASEDIR /usr/sbin/pkg_update -v $i; done
chroot $FREESBIEBASEDIR pkg_info > pkg_info.txt
rm *.tgz
cd $BACK
rm list.live.temp
rm list.diff
echo "Update completed"
