#!/bin/sh
# $Id: 6.packages.sh,v 1.11 2004/03/17 15:48:57 saturnero Exp $

set -u

. ./config.sh

cd $LOCALDIR
[ -d work ] && rm -rf work
mkdir work
cd work

DIR_TEMP=temp
FILE_CAT=packages.sh

#
#	Create list of installed packages classified by directory
#
if [ ! -d ${DIR_TEMP} ]; then
	mkdir ${DIR_TEMP}
fi

echo -n "Creating list of installed packages in your REAL system... "
	
pkg_info -oa | awk -v tempdir=${DIR_TEMP} \
	'{
                 if ($1=="Information")
		 {
                     pack=substr($3,1,length($3)-1);
                     getline; getline; getline;
                     split($1,tcat,"/");
                     system("echo \"\\\"" pack "\\\" \\\"\\\" off \\\\\" >> " tempdir "/" tcat[1] ".src");
		 } 
         }'

echo " [OK]"
CATEGORIES=`ls ${DIR_TEMP}/*.src | cut -d '/' -f2 | cut -d '.' -f1`

echo -n "Creating list of installed packages in your LIVE system... "
[ -f tlist ] && rm -f tlist
chroot ${FREESBIEBASEDIR} pkg_info -oa | awk -v tempdir=${DIR_TEMP} \
        '{
                 if ($1=="Information")
                 {
                     pack=substr($3,1,length($3)-1);
                     getline; getline; getline;
                     split($1,tcat,"/");
                     system("echo " pack " >> tlist");
                     system("echo \"\\\"" pack "\\\" \\\"\\\" on \\\\\" >> " tempdir "/" tcat[1] ".lst");
                 }
         }'
echo " [OK]"
sleep 1
FLAG=true
while [ "${FLAG}" = "true" ]
do
	echo "/usr/bin/dialog --title \"FreeSBIE LiveCD - Packages\" --clear \
	--menu \"These are the packages installed on your system \n\
	Choose the FreeSBIE packages\" -1 -1 10 \\" > $FILE_CAT
	echo "Save and exit" | awk '{print "\"" $0 "\" \"\" \\"}' >> $FILE_CAT
	echo " " | awk '{print "\"" $0 "\" \"\" \\"}' >> $FILE_CAT
	for i in $CATEGORIES
	do
		echo $i | awk '{print "\"" $1 "\" \"\" \\"}' >> $FILE_CAT
	done
	echo "2> /tmp/checklist.tmp.$$" >> $FILE_CAT
	echo "retval=\$?" >> $FILE_CAT
	echo "case \$retval in" >> $FILE_CAT 
	echo "  0)" >> $FILE_CAT
	echo "echo \"0\" > /tmp/result.tmp.$$ ;;" >> $FILE_CAT
	echo "  1)" >> $FILE_CAT
	echo "echo \"1\" > /tmp/result.tmp.$$ ;;" >> $FILE_CAT
	echo "  255)" >> $FILE_CAT
	echo "echo \"255\" > /tmp/result.tmp.$$ ;;" >> $FILE_CAT
	echo "esac" >> $FILE_CAT
	sh $FILE_CAT
	choice=`cat /tmp/checklist.tmp.$$`
	rm -f /tmp/checklist.tmp.$$
	if [ "$choice" = " " ]; then
		continue
	elif [ "$choice" = "Save and exit" ]; then
		echo "Saving preferences..."
		if [ -e plist ]; then
			rm plist
		fi
		for i in `ls ${DIR_TEMP}/*.lst`
		do
			cat $i | awk '{print substr($1,2,length($1)-2)}'>> plist.tmp
		done
		cat plist.tmp | sort > plist
		rm plist.tmp
		if [ -r ${FILE_CAT} ]; then
			rm ${FILE_CAT}
		fi
		#
		#	Creating list of packages not installed and list of packages to
		#	delete 
		#
		if [ -r to_delete ]; then
			rm to_delete
		fi
		if [ -r to_install ]; then
			rm to_install
		fi

		for i in `cat plist`	
		do
            if [ -f tlist ]; then
			    RESULT_A=`grep $i tlist`
            else
                RESULT_A=""
            fi
			if [ "${RESULT_A}" = "" ]; then
				echo $i >> to_install
			fi
		done
        if [ -f tlist ]; then
		    for i in `cat tlist`	
		    do
			    RESULT_B=`grep $i plist`
			    if [ "${RESULT_B}" = "" ]; then
			    	echo $i >> to_delete
			    fi
		    done
        fi
        FLAG="false"
	    break	
	fi
	loop=`cat /tmp/result.tmp.$$`
	rm -f /tmp/result.tmp.$$
	if [ ! "${loop}" = "0" ]; then
		exit
	fi
	echo "#!/bin/sh" > ${DIR_TEMP}/${choice}.sh
	echo "/usr/bin/dialog --title \"FreeSBIE LiveCD - Packages\" --clear \
	--checklist \"These are the packages installed on your system \n\
	Choose the FreeSBIE packages\" -1 -1 10 \\" >> ${DIR_TEMP}/${choice}.sh
	
	if [ -e ${DIR_TEMP}/${choice}.lst ]; then
		PRESENT=`cat ${DIR_TEMP}/${choice}.src | awk '{print $1}'`
		FUTURE=`cat ${DIR_TEMP}/${choice}.lst | awk '{for (i=1;i<=NF;i++) print $i;}'`
		echo $FUTURE > ${DIR_TEMP}/packages_future.tmp
		if [ -e ${DIR_TEMP}/${choice}.dst ]; then
			rm ${DIR_TEMP}/${choice}.dst
		fi
		for j in ${PRESENT}
		do
			RESULT=`grep ${j} ${DIR_TEMP}/packages_future.tmp`
			if [ "${RESULT}" = "" ]; then
				echo "${j} \"\" off \\" >> ${DIR_TEMP}/${choice}.sh
			else
				echo "${j} \"\" on \\" >> ${DIR_TEMP}/${choice}.sh
			fi
		done
		rm  ${DIR_TEMP}/packages_future.tmp
	else
		cat ${DIR_TEMP}/${choice}.src >> ${DIR_TEMP}/${choice}.sh
		cp ${DIR_TEMP}/${choice}.src ${DIR_TEMP}/${choice}.lst
	fi
	echo "2> ${DIR_TEMP}/${choice}.lst.tmp1" >> ${DIR_TEMP}/${choice}.sh
	echo "if [ \$? = 0 ]; then " >> ${DIR_TEMP}/${choice}.sh
	echo "   cat ${DIR_TEMP}/${choice}.lst.tmp1 > ${DIR_TEMP}/${choice}.lst.tmp" >> ${DIR_TEMP}/${choice}.sh
	echo "fi"  >> ${DIR_TEMP}/${choice}.sh
	echo "rm ${DIR_TEMP}/${choice}.lst.tmp1" >> ${DIR_TEMP}/${choice}.sh

	sh ${DIR_TEMP}/${choice}.sh
	if [ -f ${DIR_TEMP}/${choice}.lst.tmp ]; then
		cat ${DIR_TEMP}/${choice}.lst.tmp | awk '{for (i=1; i<=NF;i++) {print $i " \"\" on \\"}}' > ${DIR_TEMP}/${choice}.lst
		rm ${DIR_TEMP}/${choice}.lst.tmp
	fi
done

if [ -f to_delete ]; then
	echo "Deleting packages..."
    DELLIST=""
    for i in `cat to_delete`; do DELLIST="$DELLIST $i"; done
    for i in `pkg_info -qr $DELLIST | cut -d ' ' -f2 | sort | uniq`; do
        RESULT_A=`echo $DELLIST | grep $i`
        [ "$RESULT_A" = "" ] && DELLIST="$DELLIST $i"; 
    done
    chroot $FREESBIEBASEDIR pkg_delete $DELLIST;
fi

if [ -f to_install ]; then
    echo "Installing packages..."
    ADDLIST=""
    for i in `cat to_install`; do ADDLIST="$ADDLIST $i"; done
    for i in `pkg_info -qr $ADDLIST | cut -d ' ' -f2 | sort | uniq`; do
        RESULT_A=`echo $ADDLIST | grep $i`
	if [ -f tlist ]; then
	    RESULT_B=`grep $i tlist`
	else
	    RESULT_B=""
	fi
        [ "$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`
	if [ -f tlist ]; then
	    RESULT_B=`grep $i tlist`
	else
	    RESULT_B=""
	fi
        [ "$RESULT_A" = "" ] && [ "$RESULT_B" = "" ] &&  ADDLIST="$ADDLIST $i" 
    done
    BACK=$PWD; cd $FREESBIEBASEDIR
    for i in $ADDLIST; do pkg_create -zvb $i; done
    cd $BACK
    for i in `cat to_install`; do chroot $FREESBIEBASEDIR pkg_add -v $i.tgz; done
fi                        
echo -n "Cleaning out working directory..."
cd ..
rm -rf work
rm $FREESBIEBASEDIR/*.tgz
echo " [OK]"
