#!/bin/sh
. ./config.sh
rm $FREESBIEBASEDIR/plist*

echo "/usr/bin/dialog --title \"FreeSBIE LiveCD - Packages\" --clear \\" > $LOCALDIR/packages.sh
echo "--checklist \"These are the packages installed on your system \\n\\" >> $LOCALDIR/packages.sh
echo "Choose the FreeSBIE packages\" -1 -1 10 \\" >> $LOCALDIR/packages.sh
for i in `pkg_info | cut -d ' ' -f1`; do echo "\"$i\" \"\" off \\" >> $LOCALDIR/packages.sh ; done 
echo "2> /tmp/checklist.tmp.\$\$" >> $LOCALDIR/packages.sh
echo "retval=\$?" >> $LOCALDIR/packages.sh
echo "choice=\`cat /tmp/checklist.tmp.\$\$\`" >> $LOCALDIR/packages.sh
echo "rm -f /tmp/checklist.tmp.\$\$" >> $LOCALDIR/packages.sh
echo "case \$retval in" >> $LOCALDIR/packages.sh
echo "  0)" >> $LOCALDIR/packages.sh
#echo "for i in \`echo \$choice | sed -e 's/\"/#/g'\`; do pkg_create -vb \`echo \$i | awk -F\"#\" '{print \$2}'\`; done ;;" >> $LOCALDIR/packages.sh 
echo "for i in \`echo \$choice | sed -e 's/\"/#/g'\`; do echo \`echo \$i | awk -F\"#\" '{print \$2}'\` >> $FREESBIEBASEDIR/plist ; done ;;" >> $LOCALDIR/packages.sh 
echo "  1)" >> $LOCALDIR/packages.sh
echo "echo  \"Cancel pressed.\";;" >> $LOCALDIR/packages.sh
echo "  255)" >> $LOCALDIR/packages.sh
echo "[ -z \"\$choice\" ] || echo \$choice ;" >> $LOCALDIR/packages.sh
echo "echo \"ESC pressed.\";;" >> $LOCALDIR/packages.sh
echo "esac" >> $LOCALDIR/packages.sh
echo $LOCALDIR/packages.sh

/bin/sh $LOCALDIR/packages.sh

cd $FREESBIEBASEDIR

cp plist plist2

for i in `cat $FREESBIEBASEDIR/plist`; do pkg_info -qr $i | grep Dependency | cut -d ' ' -f2 >> $FREESBIEBASEDIR/plist2; done
for i in `cat $FREESBIEBASEDIR/plist2`; do pkg_info -qr $i | grep Dependency | cut -d ' ' -f2 >> $FREESBIEBASEDIR/plist3; done


cat $FREESBIEBASEDIR/plist2 $FREESBIEBASEDIR/plist3 | sort | uniq > $FREESBIEBASEDIR/plist2

rm plist3
for i in `cat $FREESBIEBASEDIR/plist2`;
do chroot $FREESBIEBASEDIR pkg_info -e $i || echo $i >> $FREESBIEBASEDIR/plist3;
done;

for i in `cat $FREESBIEBASEDIR/plist3`; do pkg_create -zvb $i; done
for i in `cat $FREESBIEBASEDIR/plist`; do chroot $FREESBIEBASEDIR pkg_add -v $i.tgz; done 

rm $FREESBIEBASEDIR/*.tgz $FREESBIEBASEDIR/plist* $LOCALDIR/packages.sh



