#!/bin/bash
function createmenu
{
if [ -z "$tempdir" ];then
pathcomp=$(mktemp -u --suffix "$(date +%s)"|tr / "\\n")
export pathcomp
temppath=$(echo "$pathcomp"|tr / "\\n"|grep -nw tmp.|cut -f 1 -d :)
export temppath
tempdir=$(echo "$pathcomp"|head -n $((temppath-1))|tr "\\n" /)
export tempdir
fi
menudesc=$(stty -a|grep rows|cut -f 2-6 -d \;|cut -f 3,5,8 -d \ |tr \; \ |sed s/"  "/" "/|sed s/"  "/" "/)
export menudesc
cat "$1"|tail -n 1 >"$tempdir""/itemlist"
numitems=$(cat "$tempdir""/itemlist"|wc -l)
export numitems
linecounter=1
export linecounter
while true;do
cat "$tempdir"/itemlist|head -n $linecounter|tail -n 1 >>"$tempdir"/items
linecounter=$((linecounter+1))
export linecounter
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"
choice=$(cat "$tempdir""/choice"|cut -f 2 -d \;)
export choice
cat "$tempdir"/itemlist|sed s/"\"\ /\\n/g"|sed "s/\"//g" > "$tempdir"/items
testnum=$(cat "$tempdir""/items"|grep -nw "$choice"|cut -f 1 -d :|head -n 1)
export testnum
itemname=$(cat "$tempdir"/items|head -n $((testnum+1))|tail -n 1)
export itemname
rm "$tempdir"/choice "$tempdir"/menu "$tempdir"/itemlist "$tempdir"/items "$tempdir"/dynmenu > /dev/null 2>/dev/null
clear
}
function createdynamicmenu
{
if [ -z "$tempdir" ];then
pathcomp=$(mktemp -u --suffix "$(date +%s)"|tr / "\\n")
export pathcomp
temppath=$(echo "$pathcomp"|tr / "\\n"|grep -nw tmp.|cut -f 1 -d :)
export temppath
tempdir=$(echo "$pathcomp"|head -n $((temppath-1))|tr "\\n" /)
export tempdir
fi
"$@">"$tempdir"/itemlist
numitems=$(cat "$tempdir"/itemlist|wc -l)
export numitems
counter=1
export counter
if [ -f "$tempdir""/menutitle" ];then
title=$(cat "$tempdir""/menutitle"|head -n 1)
export title
else
title="$numitems items available"
export title
fi
echo \""$title"\" > "$tempdir"/dynmenu
while true;do
ltr=$(cat "$tempdir"/itemlist|head -n $counter|tail -n 1|cut -c 1)
export ltr
item=$(cat "$tempdir""/itemlist"|head -n $counter|tail -n 1)
export item
echo -n \" >> "$tempdir"/dynmenu
if [ "$ltr" = \" ];then
ltr="|"
export ltr
fi
echo -n "$ltr"\;$counter\"\  \" >> "$tempdir"/"dynmenu"
if echo "$item"|grep -q \";then
item="${item/\"/\|/}"
export item
fi
echo -n "$item" >> "$tempdir"/dynmenu
echo -n \"\  >>"$tempdir"/dynmenu
counter=$((counter+1))
export counter
if [ $counter -gt "$numitems" ];then
break
else
continue
fi
done
createmenu "$tempdir"/dynmenu
rm "$tempdir"/menutitle > /dev/null 2>/dev/null
}
