#!/bin/sh
# Script to create the packfile to list all packages and dependencies
# for tinycore linux. With this file you can find all URLS
# for master package and all dependencies and place them on a
# packfile which holds only the URLs.
#
# Note: This creates only the packfile. It does not fetches packages
#
#
# You are going to fetch the URLs by wget, or by using the fetch script
#
#
# For example, if you want to use firefox and all dependencies then just do
# sh $0 firefox.tcz
# It will create a packfile that holds all URLs for this package
# If you want another tinycore version, change the url
#
# USAGE: sh $0 firefox.tcz
#
# Minas Dasygenis, 2013
# http://arch.ece.uowm.gr
#
# $0 xauth openssh tightvnc rdesktop mc Xlibs Xprogs Xfbdev fltk-1.1.10 flwm_topside wbar aterm firefox Xorg-7.7


#USE absolute pathname here
outputdir=/tank/tmp/000

if [ $# -eq 0 ] ; then
	echo Mandatory parameter is missing.
	exit
fi

# Variable to hold the TAGS of the fetched dependencies
deptags=${outputdir}/deps

pidfile=${outputdir}/pid
if [ ! -f "$pidfile" ] ; then
	echo $$ > $pidfile
fi

cwd=`pwd`
fname=`basename $0`

if [ ! -d $deptags ]; then
	mkdir $deptags
fi

tmp=$deptags


#
# Variable for MASTER URL package directory
#
#url=http://distro.ibiblio.org/tinycorelinux/5.x/x86/tcz/
url=http://distro.ibiblio.org/tinycorelinux/6.x/x86/tcz/

#lets locate the tinycore
isofile=${url}../release/TinyCore-current.iso


if [ ! -f "boot/core.gz" -a ! -f "boot/vmlinuz" ] ; then
#we first need to extract the core.gz and vmlinuz
#this can only be done if 7z is on our system
which 7z > /dev/null
if [ $? -eq 0 ] ; then
	if [ ! -f TinyCore-current.iso ] ; then

	wget -q $isofile  
	if [ $? -ne 0 ] ; then
		echo Error at $isofile. Please check.
		break
	fi

	
	if [ ! -f TinyCore-current.iso ] ; then
		echo File TinyCore-current.iso absent. Please check.
		break
	fi
	#endif tinecore-current exist
	fi

	7z x TinyCore-current.iso boot/core.gz > /dev/null || echo Fail at extraction of boot/core.gz && exit 3
	7z x TinyCore-current.iso boot/vmlinuz > /dev/null || echo Fail at extracction of vmlinuz && exit 6

else
	echo 7z not found. Extract manually boot/core.gz and boot/vmlinuz

fi

#end if file already present
else
	#echo Using boot/vmlinuz boot/core.gz from local file system.
fi


echo $1| grep ".tcz" > /dev/null
if [ $? -ne 0 ] ; then
    master_pack=${1}".tcz"
else
#note the file that we want to bring
	master_pack=$1
fi

if [ -s "${tmp}/$1.dep" ] ; then
        #we have already fetch it...skip it
        return
fi

echo "$url${master_pack}"
echo "$url${master_pack}" >> ${tmp}/packfile
echo "$url${master_pack}.dep" >> ${tmp}/packfile
echo "$url${master_pack}.md5.txt" >> ${tmp}/packfile

if [ -z "$master_pack" ] ; then
	echo "Nothing to do"
	exit
fi


print_deps()
{
if [ -s "${tmp}/$1.dep" ] ; then
	#we have already fetch it...skip it
	return
fi


wget -q $url/$1.dep -O $tmp/$1.dep
#if the file does not exist just return
if [ ! -s "$tmp/$1.dep" ] ; then
	return 
fi




while read line
do
#the dep file has only lines each for every dependency
printf "%s " $line
done < $tmp/$1.dep

printf "\n"
}
#end printind dependencies


go=1
while [ $go -eq 1 ]; 
do

for i in `print_deps $master_pack`
do
#echo "DEP $i"
sh ${cwd}/${fname} $i
done

go=0

#done for every dependency 
done


#Perhaps we carry more variables..lets have another run
mypid=`cat $pidfile`
if [ $$ -ne "$mypid" ] ; then
	exit
fi


#Here we are only on the master thread
while [ $# -gt 1 ] ; 
do
	shift
	sh ${cwd}/${fname} $1
done



#Ok we did all our work. Lets build it.
rm $pidfile

echo Downloading packages
cd ${tmp}
cat ${tmp}/packfile | sort | uniq > packfinal
wget -q -c -np -r -nd -i packfinal
rm packfinal
rm ${tmp}/packfile


if [ "$go" -eq 0 ] ; then
	echo Building remastered...
	#Now that we have bring all packages, lets build the remastered

	cd $outputdir
	randomname=`mktemp rootXXXXXXXX`
	rm $randomname
	mkdir $randomname
	cd $randomname

	mkdir -p opt/tce/optional
	mkdir -p home/tc/.X.d/
	mkdir -p opt/home/tc/.X.d/
	chmod 777 opt/tce/optional home/tc/.X.d/ opt/home/tc/.X.d/
	#mv ${tmp}/*tcz*  opt/tce/optional
	#mv ${tmp}/*tcz*  opt/tce/
	mkdir tce
	mv ${tmp}/*tcz* tce/
	ls -1 tce/*.tcz > tce/onboot.lst
	ls -1 tce/*.tcz > tce/xbase.lst
	#ls -1 opt/tce/optional/*.tcz > opt/tce/onboot.lst
	#cp opt/tce/onboot.lst opt/tce/xbase.lst
	
wget -q http://www.terena.org/activities/tcs/repository/TERENA_SSL_CA.pem -O opt/tce/TERENASSLCA.crt
wget -q https://www.terena.org/activities/tcs/repository/UTN-USERFirst-Hardware.pem -O opt/tce/UTN-USERFirst-Hardware.pem 
wget -q http://arch.ece.uowm.gr/arch.ece.uowm.gr.pem -O opt/tce/arch.ece.uowm.gr.pem 
wget -q http://arch.ece.uowm.gr/ca-bundle.crt -O opt/tce/ca-bundle.crt 

cat >> opt/bootlocal.sh << EOM
#!/bin/sh
clear
echo "Dasygenis Customization in Progress..please wait"

su tc -c "/usr/bin/tce-load -i /opt/tce/optional/*.tcz
su tc -c "startx"
#remove tc user
cat /etc/sudoers | grep -v tc > /etc/sudoers >/dev/null

printf "minas\nminas" | passwd > /dev/null

#put the certificate back
cp /opt/tce/TERENASSLCA.crt /usr/local/etc/ssl/certs/
cp /opt/tce/UTN-USERFirst-Hardware.pem /usr/local/etc/ssl/certs/
cp /opt/tce/arch.ece.uowm.gr.pem /usr/local/etc/ssl/certs/
ln -s /opt/tce/TERENA_SSL_CA.pem /usr/local/etc/ssl/certs/TERENA_SSL_CA.pem
cat /opt/tce/ca-bundle.crt >  /usr/local/etc/ssl/certs/ca-bundle.crt

EOM

#Startup scripts to home/tc/.X.d/

cat > home/tc/.X.d/startup.sh << EOM
#!/bin/sh
# put other system startup commands here

setxkbmap -option "grp:switch,grp:alt_shift_toggle,grp_led:scroll" -layout "us,ro"  &

firefox -setDefaultBrowser http://arch.ece.uowm.gr &

EOM


cp home/tc/.X.d/startup.sh opt/home/tc/.X.d/startup.sh
touch home/tc/.X.d/_no_OP_ok


chmod -R 777 home
chmod 755 opt/bootlocal.sh 
chmod ugo+x home/tc/.X.d/startup.sh opt/home/tc/.X.d/startup.sh
find . > opt/.filetool.lst
chmod 755 opt/.filetool.lst

find . | cpio -o -H newc | gzip -2 > ${outputdir}/remaster.gz

if [ $? -eq 0 ] ; then
	echo File ${outputdir}/remaster.gz created.
else
	echo Failure at CPIO step.
fi


echo Cleaning dir ${randomname} in 2 sec
sleep 2
cd ${outputdir}
rm -rf  ${randomname}
#end for check flag
rmdir deps > /dev/null 2>&1
fi



