#!/bin/zsh
function createmenu
{
if [ -z $tempdir ];then
export pathcomp=`mktemp -u --suffix a|tr / "\\n"`
export temppath=`echo $pathcomp|tr / "\\n"|grep -nw tmp.|cut -f 1 -d :`
export tempdir=`echo $pathcomp|head -n $(($temppath-1))|tr "\\n" /`
fi
export menudesc=`stty -a|grep rows|cut -f 2-6 -d \;|cut -f 3,5,8 -d \ |tr \; \ |sed s/"  "/" "/|sed s/"  "/" "/`
cat $1|tail -n 1 >$tempdir/itemlist
export numitems=`cat $tempdir/itemlist|wc -l`
export linecounter=1
while true;do
cat $tempdir/itemlist|head -n $linecounter|tail -n 1 >>$tempdir/items
export linecounter=$(($linecounter+1))
if [ $linecounter -gt $numitems ];then
break
else
continue
fi
done
echo whiptail --menu --nocancel `cat $1|head -n 1` `echo -n $menudesc` `cat $1|tail -n 1`>$tempdir/menu
chmod 755 $tempdir/menu
$tempdir/menu 2>$tempdir/choice
export choice=`cat $tempdir/choice|cut -f 2 -d \;`
cat $tempdir/itemlist|sed s/"\"\ /\\n/g"|sed "s/\"//g" > $tempdir/items
export testnum=`cat $tempdir/items|grep -nw $choice|cut -f 1 -d :|head -n 1`
export itemname=`cat $tempdir/items|head -n $(($testnum+1))|tail -n 1`
rm $tempdir/choice $tempdir/menu $tempdir/itemlist $tempdir/items $tempdir/dynmenu > /dev/null 2>/dev/null
clear
}
function createdynamicmenu
{
if [ -z $tempdir ];then
export pathcomp=`mktemp -u --suffix a|tr / "\\n"`
export temppath=`echo $pathcomp|tr / "\\n"|grep -nw tmp.|cut -f 1 -d :`
export tempdir=`echo $pathcomp|head -n $(($temppath-1))|tr "\\n" /`
fi
$@>$tempdir/itemlist
export numitems=`cat $tempdir/itemlist|wc -l`
export counter=1
if [ -f $tempdir/menutitle ];then
export title=`cat $tempdir/menutitle|head -n 1`
else
export title="$numitems items available"
fi
echo \"$title\" > $tempdir/dynmenu
while true;do
export ltr=`cat $tempdir/itemlist|head -n $counter|tail -n 1|cut -c 1`
export item=`cat $tempdir/itemlist|head -n $counter|tail -n 1`
echo -n \" >> $tempdir/dynmenu
if [ $ltr = \" ];then
export ltr="|"
fi
echo -n $ltr\;$counter >> $tempdir/dynmenu
echo -n \"\  >>$tempdir/dynmenu
echo -n \" >> $tempdir/dynmenu
if echo $item|grep -q \";then
export item=`echo $item|sed "s/\"/\|/g"`
fi
echo -n $item >> $tempdir/dynmenu
echo -n \"\  >>$tempdir/dynmenu
export counter=$(($counter+1))
if [ $counter -gt $numitems ];then
break
else
continue
fi
done
createmenu $tempdir/dynmenu
rm $tempdir/menutitle > /dev/null 2>/dev/null
}
echo "\"Welcome to Jenux!\"">/tmp/automenu
echo -n "\"a\" \"Install the Android OS on this device\" \"b\" \"Connect to a bluetooth device\" \"c\" \"Run the clonezilla backup tool to backup or restore this device's storage\" \"e\" \"enable remote access to this environment, use with caution! \" \"i\" \"install the software in this environment to this system\" \"j\" \"install Jenux on this device\" \"l\" \"enter live mode to test the software in this environment\" \"n\" \"Access this device's storage over the network, using the NBD protocol\" \"p\" \"partition a hard disk\" \"r\" \"Flash an image to a Raspberry Pi or Other ARM Device\" \"s\" \"drop to system shell\" \"w\" \"Reset a Windows Password on this device\"">>/tmp/automenu
createmenu /tmp/automenu
case "$choice" in
a)
androidbuild
;;
b)
blueconnect
;;
c)
clonezilla
;;
e)
sshenable
;;
j)
autobuild
;;
i)
offline-install
;;
l)
echo preparing to enter live mode
cp /proc/cmdline /root/c
chmod 644 /root/c
echo livemode=livemode >> /root/c
cat /root/c|tr \\n \  >/root/c2
mv /root/c2 /root/c
mount -o bind /root/c /proc/cmdline
cp /run/archiso/airootfs/root/.zlogin.iso /root/.zlogin
cp /run/archiso/airootfs/root/.zshrc.local /root/.zlogin.firstboot
cp /run/archiso/airootfs/etc/skel/.zshrc.local /root
systemctl restart sshcheck
sleep 2
rm -rf /.fscrypt
umount /proc/cmdline
rm /root/c
exit
;;
n)
tgtdisk
;;
p)
sh -c 'echo -n "print devices"|parted|grep /dev|grep \(>/tmp/disk;cat /tmp/disk|tr \  \:>/tmp/disks;rm /tmp/disk'
createdynamicmenu cat /tmp/disks
export disk=`echo $itemname|cut -f 1 -d \:|cut -f 2 -d \"`
unset choice
rm /tmp/disks
if blkid $disk|grep -qwi dos;then
fdisk $disk
else
gdisk $disk
fi
;;
r)
pibuild
;;
s)
zsh
;;
w)
winreset
break
;;
esac
