#!/bin/bash
#
# By Maxim F. Stewart
# Contact: [1itdominator@gmail.com]
#
# Copyright 2013 Maxim F. Stewart
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#---------------------------------------------------------------------------------------#

declare -a menu=("Accessories" "Utility" "Multimedia" "Video" "Audio"
                 "Development" "Game" "Internet" "Network" "Graphics"
                 "Office" "System" "Settings" "Wine")

mainMENU() {
exec 3>&1; # Custom stream to set variable from dialog

INPUT=$(dialog --clear --backtitle "Shellmen" \
--title "[ M A I N - M E N U ]" \
--menu "Please Select An Option" 16 50 15 \
Accessories "General Programs" \
System "Main System Programs" \
Settings "Main System Settings" \
Multimedia "Audio & Video Programs" \
Graphics "Image Programs" \
Games "Gaming Programs" \
Office "Wordprocess & Documents Programs" \
Development "Programing Programs" \
Internet "Various Internet Related Programs" \
Wine "Windows Exe & Program Support"  2>&1 1>&3 )

case $INPUT in
	Accessories) bash /tmp/sysMENU/Accessories.sh ;;
	System) bash /tmp/sysMENU/System.sh ;;
	Settings) bash /tmp/sysMENU/Settings.sh ;;
	Multimedia) bash /tmp/sysMENU/Multimedia.sh ;;
	Graphics) bash /tmp/sysMENU/Graphics.sh ;;
	Games) bash /tmp/sysMENU/Game.sh ;;
	Office) bash /tmp/sysMENU/Office.sh ;;
	Development) bash /tmp/sysMENU/Development.sh ;;
	Internet) bash /tmp/sysMENU/Internet.sh ;;
	Wine) bash /tmp/sysMENU/Wine.sh ;;
	Exit) echo "Bye!"; break ;;
esac
}

commandInsert() {
x=$(cat /tmp/sysMENU/menu.list | wc -l) >> /dev/null ;
i="1"

while [ $i -le $x ]; do
    line1=$(sed -n "${i}p" /tmp/sysMENU/menu.list);
    filename="${line1%.*}"
    execMethod=$(grep -A 0 "Exec=" /usr/share/applications/"$line1")
    catagory=$(grep -A 0 "Categories=" /usr/share/applications/"$line1")
    preComment=$(grep -A 0 "Comment=" /usr/share/applications/"$line1")
    execCMD=$(echo "${filename}) exec ${filename} ;;")

    writeToPath "$catagory" "$execCMD"

i=$[$i++1];
done

for opt in "${menu[@]}"; do
    if [[ $opt == "${menu[8]}" ]]; then
        opt=${menu[7]}
    fi

    if [[ $opt == "${menu[3]}" || $opt == "${menu[4]}" ]]; then
        opt=${menu[2]}
    fi
    echo "esac" >> /tmp/sysMENU/"${opt}".sh
done

chmod +x /tmp/sysMENU/*.sh
mainMENU;
}

menuHeaderInsert() {
x=$(cat /tmp/sysMENU/menu.list | wc -l) >> /dev/null ; # Variable set to number of lines filled in list.txt
i="1"

while [ $i -le $x ]; do
    line1=$(sed -n "${i}p" /tmp/sysMENU/menu.list); # Reads the number of lines in list.txt then sets as a variable counting up to variable x
    filename="${line1%.*}"
    execMethod=$(grep -A 0 "Exec=" /usr/share/applications/"$line1")
    catagory=$(grep -A 0 "Categories=" /usr/share/applications/"$line1")
    preComment=$(grep -A 0 "Comment=" /usr/share/applications/"$line1")
    comment=$(sed s/"Comment="//g <<< ${preComment})
    inputer=$(echo "$filename "\"$comment"\" \\")

    writeToPath "$catagory" "$inputer"

i=$[$i++1];
done

endMenuInsert=$(echo "2>"\"'${INPUT}'"\"")
menuitmVar=$(echo 'menuitem=$(<"${INPUT}")')
preCMD=$(echo "case \$menuitem in")
menuCall=$(echo "Main_Menu) bash /bin/shellMen ;;")


for opt in "${menu[@]}"; do
    if [[ $opt == "${menu[8]}" ]]; then
        opt=${menu[7]}
    fi

    if [[ $opt == "${menu[3]}" || $opt == "${menu[4]}" ]]; then
        opt=${menu[2]}
    fi

    echo "$endMenuInsert" >> /tmp/sysMENU/"${opt}".sh
    echo "$menuitmVar" >> /tmp/sysMENU/"${opt}".sh
    echo "$preCMD" >> /tmp/sysMENU/"${opt}".sh
    echo "$menuCall" >> /tmp/sysMENU/"${opt}".sh
done

commandInsert;
}

function writeToPath() {
    catagory=$1
    inputer=$2


    if [[ "$catagory" == *"${menu[0]}"* ]] || [[ "$catagory" == *"${menu[1]}"* ]]; then
        echo "$inputer" >> /tmp/sysMENU/"${menu[0]}".sh
    elif [[ "$catagory" == *"${menu[2]}*" ]] \
            || [[ "$catagory" == *"${menu[3]}"* ]] \
	    || [[ "$catagory" == *"${menu[4]}"* ]]; then
	        echo "$inputer" >> /tmp/sysMENU/"${menu[2]}".sh
    elif [[ "$catagory" == *"${menu[5]}"* ]]; then
        echo "$inputer" >> /tmp/sysMENU/"${menu[5]}".sh
    elif [[ "$catagory" == *"${menu[6]}"* ]]; then
        echo "$inputer" >> /tmp/sysMENU/"${menu[6]}".sh
    elif [[ "$catagory" == *"${menu[7]}"* ]] || [[ "$catagory" == *"${menu[8]}"* ]] ; then
        echo "$inputer" >> /tmp/sysMENU/"${menu[7]}".sh
    elif [[ "$catagory" == *"${menu[9]}"* ]]; then
        echo "$inputer" >> /tmp/sysMENU/"${menu[9]}".sh
    elif [[ "$catagory" == *"${menu[10]}"* ]]; then
        echo "$inputer" >> /tmp/sysMENU/"${menu[10]}".sh
    elif [[ "$catagory" == *"${menu[11]}"* ]]; then
        echo "$inputer" >> /tmp/sysMENU/"${menu[11]}".sh
    elif [[ "$catagory" == *"${menu[12]}"* ]]; then
        echo "$inputer" >> /tmp/sysMENU/"${menu[12]}".sh
    elif [[ "$catagory" == *"${menu[13]}"* ]]; then
        echo "$inputer" >> /tmp/sysMENU/"${menu[13]}".sh
    fi
}


startScan() {
clear
mkdir /tmp/sysMENU
touch /tmp/sysMENU/menu.list ;
sed -i "d" /tmp/sysMENU/menu.list ;
ls -p /usr/share/applications/ | grep -v / >> /tmp/sysMENU/menu.list ;

header='''#!/bin/bash
INPUT=/tmp/menu.txt
dialog --clear --backtitle "Shellmen" \
--title "[ S U B - M E N U ]" \
--menu "Please Select An Option" 15 50 10 \
Main_Menu "Goes To Main Menu" \'''

for opt in "${menu[@]}"; do
    if [[ $opt == "${menu[8]}" ]]; then
        opt=${menu[7]}
    fi

    if [[ $opt == "${menu[3]}" || $opt == "${menu[4]}" ]]; then
        opt=${menu[2]}
    fi
    echo "$header" > /tmp/sysMENU/"${opt}".sh
done

menuHeaderInsert;
}

pre() {
    if [ -d /tmp/sysMENU/ ]; then
        mainMENU;
    else
        startScan;
    fi
}
pre;
