#!/bin/bash
# fastbuildbl9sb1.sh - A shell script to be started from unetbootinbitti to prepare system and display dialogs and menus on screen and download with git Bittixlinux9 and make an iso of downloaded files
# Store menu options selected by the user
test -f /usr/bin/dialog
if test $? -eq 1
  then 
  	echo "This is probably the first time you use this script ! "
  	echo "You must run command sudo apt-get update and"
	echo "command sudo apt-get install dialog in a terminal"
	echo "before you can run this script !"
	pause
	exit
fi
logname >> /tmp/lgnb9i
LGN=$(cat /tmp/lgnb9i)
echo "" >>  /tmp/b9imenu
INPUT=/tmp/b9imenu
 
# Storage file for displaying command output
echo "" >> /tmp/b9iop
OUTPUT=/tmp/b9iop

#trap SIGHUP SIGINT SIGTERM 
trap "rm $OUTPUT; rm $INPUT;" SIGHUP SIGINT SIGTERM
echo ~/>>/tmp/b9iuser
USRD=$(cat /tmp/b9iuser)




test -f /tmp/.pV88tg4ed0OKJ
if test $? -eq 0
  then rm /tmp/.pV88tg4ed0OKJ
fi

h=1300
w=1502
t=$t
p=1
D=/tmp
T=/tmp
l=1199
a=bash
s=/etc/apt/sources.list



#
# Purpose - display output using msgbox 
#  $1 -> set msgbox height
#  $2 -> set msgbox width
#  $3 -> set msgbox title
#

function display_output(){
	local h=${1-1500}			# box height default 10
	local w=${2-1502} 		# box width default 41
	local t=${3-Output} 	# box title 
	 dialog --backtitle "Bittixlinux9 - Git & build the iso-file" --title "${t}" --msgbox ${h} ${w} "$(<$OUTPUT)"
}
# Purpose - display output using msgbox 
#  $1 -> set msgbox height
#  $2 -> set msgbox width
#  $3 -> set msgbox title
#
function display_output2(){
	local h=${1-300}			# box height default 10
	local w=${2-600} 		# box width default 41
	local t=${3-Output} 	# box title
	local p=${5-Percentage} 
	dialog --backtitle "Bittixlinux9 - Git & build the iso-file" --title "${t}" --clear --mixedgauge ${p} ${h} ${w} "$(<$OUTPUT)" 
}

# Purpose - display output using msgbox 
#  $1 -> set msgbox height
#  $2 -> set msgbox width
#  $3 -> set msgbox title
#  $4 -> set selected directory
#
function display_output7(){
	local h=${1-1200}			# box height default 10
	local w=${2-1500} 		# box width default 41
	local t=${3-Output} 	# box title 
	local D=${4-Directory}  # selected directory
	local l=${1-1199} # list height
	dialog --backtitle "Bittixlinux9 - Git & build the iso-file" --title "${t}" --clear --treeview "${D}" ${h} ${w} ${l} "$(<$OUTPUT)" 
}


# Purpose - display output using programbox
#  $1 -> set msgbox height
#  $2 -> set msgbox width
#  $3 -> set msgbox title
#
function display_output4(){
	local h=${1-1300}			# box height default 10
	local w=${2-1500} 		# box width default 41
	local t=${3-Output} 	# box title
	local a=${4-bash} 
	dialog --backtitle "Bittixlinux9 - Git & build the iso-file" --title ${t} --clear --prgbox ${a}  ${h} ${w} "$(<$OUTPUT)"
}

# Purpose - display output using textbox
#  $1 -> set msgbox height
#  $2 -> set msgbox width
#  $3 -> set msgbox title


function display_output5(){
	local h=${1-1500}			# box height default 10
	local w=${2-1500} 		# box width default 41
	local t=${3-Output} 	# box title
	local s=${7-File} 
	dialog --backtitle "Bittixlinux9 - Git & build the iso-file" --title "${t}" --clear --textbox ${s}  ${h} ${w} "$(<$OUTPUT)"
}

# Purpose - display output using passwordbox
#  $1 -> set msgbox height
#  $2 -> set msgbox width
#  $3 -> set msgbox title
#
function display_output6(){
	local h=${1-1300}			# box height default 10
	local w=${2-1500} 		# box width default 41
	local t=${3-Output} 	# box title
	local m=${8-Text}		# box text
	
	dialog --backtitle "Bittixlinux9 - Git & build the iso-file" --title "${t}" --clear --passwordbox ${m}  ${h} ${w} "$(<$OUTPUT)"
}

function display_output3(){
	local h=${1-1200}			# box height default 10
	local w=${2-1500} 		# box width default 41
	local t=${3-Output} 	# box title 
	local D=${4-/tmp}  # selected directory
	dialog --backtitle "Bittixlinux9 - select build directory or move to newly created directory with enough disk space" --title "${t}" --clear --dselect "${D}" ${h} ${w}
}


h=1200			# box height default 10
w=1500 		# box width default 41


test -f /etc/apt/sources.list
if test $? -eq 1
  then
echo "The system dont seem to have apt ! Use this from a debian or ubuntu based or derivative distro or you have to install dialog git genisoimage unzip squashfs-tools mc nano and bashburn to make all features work ! Press Ctrl C to STOP"
wait 10
fi


display_output 1000 9 "This is getting tools and building the Bittixlinux9 iso image with Main modules only" 

display_output 1000 9 "It's a 1.7 GB iso file AND NOW YOU NEED about 5 GB empty space altogether to build" 

display_output 1000 9 "Estimated download and build time 1.5 - 2.5 hours with 3g modem connection"

display_output 1000 9 "If the script seems to get stuck near the end at dnl8 then HIT Enter"

if test $? -eq 1
	then 
dialog \
		--backtitle "Bittixlinux9 - Git & build the iso-file !" \
		--title "[ PREPARING SYSTEM - NEEDED PROGRAMS]" \
				 --passwordbox "enter your password" 8 78 2> /tmp/.pV88tg4ed0OKJ
				 PSSS=$(cat /tmp/.pV88tg4ed0OKJ)
				 echo $PSSS |sudo -S apt-get update
fi


test -f /usr/bin/gksu
if test $? -eq 1
  then sudo -A -E apt-get install gksu
fi

SUDO_ASKPASS=/usr/bin/gksu
export SUDO_ASKPASS
/usr/bin/gksu -k -u root -D "Type your password to proceed at Bittixlinux9 iso build tools installations" apt-get update




test -f /usr/bin/git
if test $? -eq 1
  then sudo -A -E apt-get install git
fi

test -f /usr/bin/genisoimage
if test $? -eq 1
  then sudo -A -E apt-get install genisoimage
fi

test -f /usr/bin/unzip
if test $? -eq 1
  then sudo -A -E apt-get install unzip
fi

test -f /usr/bin/bashburn
if test $? -eq 1
  then sudo -A -E apt-get install bashburn
fi

test -f /usr/bin/nano
if test $? -eq 1
  then sudo -A -E apt-get install nano
fi

#test -f /usr/bin/mc
#if test $? -eq 1
#  then sudo -A -E pt-get install mc 
#fi



cd $D


git clone git://git.code.sf.net/p/bittix9dnlhlp1/code /tmp/dnl1	


#git clone git://git.code.sf.net/p/bittix9dnlhlp7/code /tmp/dnl7


#git clone git://git.code.sf.net/p/bittixdnlhlp4/code /tmp/dnl4


git clone git://git.code.sf.net/p/bittix9dnlhlp2/code /tmp/dnl2



git clone git://git.code.sf.net/p/bittix9dnlhlp3/code /tmp/dnl3


git clone git://git.code.sf.net/p/bittix9dnlhlp5/code /tmp/dnl5



git clone git://git.code.sf.net/p/bittix9dnlhlp6/code /tmp/dnl6



git clone git://git.code.sf.net/p/bittix9dnlhlp8/code /tmp/dnl8



echo "Press Enter"
read -r wait until Cloning into '/tmp/dnl8' ;continue \
#read -r wait until Receiving objects: 100% ;continue \

echo $ | dialog --clear --mixedgauge "Getting the Bittixlinux9 modules - download complete ... 100% of total!" 10  30 100


cd /tmp/dnl1
unzip makebittix.zip




cd /tmp/dnl2
mv *.sb  /tmp/dnl1/makebittix9slax/slax/

cd /tmp/dnl3
mv *.sb  /tmp/dnl1/makebittix9slax/slax/

cd /tmp/dnl5
mv *.sb  /tmp/dnl1/makebittix9slax/slax/

cd /tmp/dnl6
mv *.sb  /tmp/dnl1/makebittix9slax/slax/


cd /tmp/dnl8
mv *.sb  /tmp/dnl1/makebittix9slax/slax/


cd /tmp/dnl4
mv *.sb  /tmp/dnl1/makebittix9slax/slax/


cd /tmp/dnl1
mv *.sb  /tmp/dnl1/makebittix9slax/slax/

cd  /tmp/dnl1/makebittix9slax/slax/
echo "Press Enter"

CDLABEL="bittix"
ISONAME="bittixlinux9-xfce-dvd-amd64.iso"

genisoimage -o "$ISONAME" -v -J -R -D -A "$CDLABEL" -V "$CDLABEL" \
-no-emul-boot -boot-info-table -boot-load-size 4 \
-b boot/isolinux/isolinux.bin -c boot/isolinux/isolinux.boot ../. \


echo "Press Enter"
#read -r wait until extents written then; continue \


display_output 1000 9 "/tmp/dnl1/makebittix9slax/slax/bittixlinux9-xfce-dvd-amd64.iso READY !" \

echo "Removing temporary files !"
rm -f /tmp/b9iuser
rm -f /tmp/lgnb9i

rm -f -r /tmp/dnl2
rm -f -r /tmp/dnl3
rm -f -r /tmp/dnl4
rm -f -r /tmp/dnl5
rm -f -r /tmp/dnl6
rm -f -r /tmp/dnl7
rm -f -r /tmp/dnl8


cd /tmp/dnl1/makebittix9slax/slax
ls *.iso
echo "You can burn the iso to dvd by bashburn or use unetbootinbitti to burn it to usb-stick "
sleep 5

[ -f lgnb9i ] && rm lgnb9i | [ -f b9iuser ] && rm b9iuser
[ -f $OUTPUT ] && rm $OUTPUT |[ -f $INPUT ] && rm $INPUT


