#!/bin/bash

#############################################
#		MINT-FM2-FUNCTION-LIBRARY
#--------------------------------------------
# Contributors: Shane Joe Lazar <shane AT archlinux DOT us>
# 		Kendall Tristan Weaver <kendalltweaver@gmail.com>
# Description: This is the debug library file for mint-fm2, written for Linux Mint 9 Fluxbox CE.
# License: GPL v2 or later.
#		/MINT-FM2-FUNCTION-LIBRARY
#############################################



launcherChecks () {
echo "CHECKS"
echo "------"
echo ""
echoVariables
echo "------"
echo ""
echo "Check for running daemon: ps aux | grep -e mfmuserdaemon -e inotify | cat"
ps aux | grep -e mfmuserdaemon -e inotify | cat 
echo ""
echo "Directory check: ls -Rla $WDIR"
ls -Rla $WDIR
echo "------"

} 

debugDelete () {
  echo "Deleting: $1"
  echo ""
}

debugCreateIconDB () {
  echo "Creating icon database: ${DATABASES[$folder]} from ${FOLDERS[$folder]}"
  echo ""
}

debugActiveIconDB () {
  if [[ ${#ACTIVEICONDB[@]} = 0 ]]
  then
    echo "No active Icon databases."
  else
    echo "Active DBs: ${ACTIVEICONDB[@]}"
  fi
  echo ""
}

debugMenuCacheGen () {
 echo "Creating menu cache in $LANG"
  echo ""
}

debugIconLocate () {
  echo "Locate command is: $ICONLOCATE"
  echo ""
}

debugFilesNConfigs () {
  echo "Check Files and directories."
  ls -Rla $WDIR
  echo ""
  echo "Loaded User configs from: $USRCONFIG"
  cat $USRCONFIG | grep -v -e "^#" -e "^$"
  echo ""
}

debugMenuRoot () {
  echo "Creating menu root."
  echo ""
}

debugCatMenu () {
  echo "Creating categories menu file."
  echo ""
}

debugChanges () {
  echo "Checking for changes:"
  echo "diff $LISTCURRENT $LISTNEW"
  diff $LISTCURRENT $LISTNEW | cat
  echo ""
}

debugUpdateCache () {
  echo "Updating menu cache."
  echo ""
}

debugEcho () {
  echo ""
}

debugAddQ () {
  echo "Adding application entries for:"
  echo "${ADDQUEUE[@]}"
  echo ""
}

debugRmQ () {
  echo "Removing application entries for:"
  echo "${RMQUEUE[@]}"
  echo ""
}

debugSubmenu () {
  echo "Updating $1"
}

debugNewSubmenu () {
  echo "Submenu '$FILENAME' does not exist. Creating now."
}

debugCurrSubmenu () {
  echo "Submenu '$FILENAME' present."
}

debugCustomEntries () {
  echo "Custom entries found: $customDesktops"
}

debugNoCustomEntries () {
  echo "No custom entries found."
}

detachPath () {
  mkdir -p /tmp/$USER/bin
  COMMANDS=("echo" "zenity" "touch" "ps" "bc" "updatedb" "locate" "gettext" "cat" "sed" "mkdir" "rm" "cp" "grep" "mv" "diff" "sort" "true" "date" "clear" "tty" "which" "ls" )
  for c in ${COMMANDS[@]}
  do
    ln -s "$(which $c)" "/tmp/$USER/bin/$c"
  done
# Default PATH
# PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
  PATHORIG="$PATH"
  PATH="/tmp/$USER/bin"
}

tempPathReset () {
  PATH="$PATHORIG"
}

tempCustomPathSet () {
  PATH="/tmp/$USER/bin"
}

echoVariables () {
  ARRAYLIST=("WATCHEDDIRS" "CHANGEDATA" "CLEANUPTARGETS" "ACTIVEICONDB" "ENTRYORDER" "CHANGEQUEUE" "ADDQUEUE" "RMQUEUE" "CURRSUBMENU")
  VARLIST=("ZENPROGRESS" "MENUTITLE" "WDIR" "APPDIR" "SUBMENUDIR" "FBDIR" "FBMENU" "FBCUSTOM" "FBCAT" "USRDAEMON" "USRCONFIG" "CIAODIR" "DBGFUNCT" "LISTNEW" "LISTCURRENT" "ICONUSERDB" "ICONMINTDB" "ICONPREFDB" "ICONSYSDB" "ICONPIXDB" "ICONS" "ICONTHEME" "DBG" "ICONLOCATE" "MENUCACHEGEN" "ZEN")

  echo "ARRAYS"
  echo "------"
  for a in ${ARRAYLIST[@]}
  do
    echo ""
    eval echo "$a: \${$a[@]}"
  done
  echo ""
  echo "VARIABLES"
  echo "---------"
  for v in ${VARLIST[@]}
  do
    echo "$v: $(eval echo \$$v)"
  done
  echo ""
}

getIndex () {
  echo "==> Enter the application or category to search for:"
  read -p ">>> " app
  echo "==> Search in new or current list? [n/c]"
  read -p ">>> " list
  if [[ $list = "n" ]]
  then
    grep -n -i -e "^-.*$app" -e "^-.*$app" $LISTNEW
    echo "==> Please enter the number corresponding to the best match"
    read -p ">>> " RAWINDEX
  elif [[ $list = "c" ]]
  then
    grep -n -i -e "^-.*$app" -e "^+.*$app" $LISTCURRENT
    echo "==> Please enter the number corresponding to the best match"
    read -p ">>> " RAWINDEX
  else
    echo "==> Invalid list option."
  fi
  if [[ $RAWINDEX -gt 0 ]]
  then
    echo "==> RAWINDEX has been set to $RAWINDEX"
    echo "==> You can now run other functions"
  else
    echo "==> Invalid value for RAWINDEX."
  fi
}

welcomeMsg () {
  echo "#--------------------------------------------"
  echo "# This is the Mint-FM2 debug prompt for expert users."
  echo "# It provides access to the commands and functions required by Mint-FM2."
  echo "# Hints:"
  echo "#	echoVariables # Prints current variables and arrays"
  echo "#	declare -F # Prints available functions"
  echo "#	declare -f <NameOfFunction> # Prints the function"
  echo "#	getIndex # Helps you set the value of RAWINDEX"
  echo "#	welcomeMsg # Prints this message"
  echo "#	help # Prints function descriptions"
  echo "#	exit # Exits to normal Bash prompt"
  echo "#--------------------------------------------"
}

help () {
  echo "A SHORT DESCRIPTION ON EACH OF THE AVAILABLE FUNCTIONS."
  echo "Run 'declare -f <NameOfFunction>' for detailed info"
  echo "-------------------------------------------------------"
  echo "browserEntry: Generates the default web browser entry"
  echo "checkFileAndDirectories: Checks and creates required files and directories"
  echo "cleanUp: Deletes unrequired and old files"
  echo "createAppEntry: Outputs the complete menu entry for the set app. (First run getIndex)"
  echo "createCacheArrays: Creates cache array from menu cache"
  echo "createCategoryMenu: Outputs the category menu"
  echo "createIconDB: Creates the database of icons"
  echo "createIconLocate: Builds the icon search command"
  echo "createMenuCache: Creates the menu cache from .desktop files"
  echo "createMenuRoot: Builds the menu root file (~/.fluxbox/menu)"
  echo "createPreservedSubmenu: Generates the submenu files while preserving custom changes"
  echo "customEntry: Includes the user's custom entries file"
  echo "daemonControl: Controls the daemon. Requires [start|restart|stop]. It will exit this script"
  echo "debug: The base debug function to enable debug output."
  echo "debug*: Various debug output functions"
  echo "detachPath: Creates custom PATH for debug prompt"
  echo "echoVariables: Outputs many important variables and arrays for debugging"
  echo "fixEntry: Fixes generated entries for known issues"
  echo "generatedEntry: Includes the autogenerated section in the menu root"
  echo "getAllApps: Generates and outputs entries for all applications to stdout"
  echo "getAppCategory: Outputs the category of an app. (First run getIndex)"
  echo "getCategoryApps: Outputs all app entries in a given category. (Requires many variables to be set)"
  echo "getChanges: Outputs raw data of detected changes"
  echo "getCommand: Outputs the command for running set app (First run getIndex)"
  echo "getFolder: Outputs the folder index in which the .desktop file is located (First run getIndex)"
  echo "getIconByIndex: Outputs icon path for set app (First run getIndex)"
  echo "getIconByName: Outputs icon path for named app (\$1)"
  echo "getIndex: Interactive function to determine RAWINDEX"
  echo "getName: Outputs name of app for set app (First run getIndex)"
  echo "getTag: Outputs app tag for set app (First run getIndex)"
  echo "getTranslation: Retrieves translations (\$1=TEXTDOMAIN \$2=String)"
  echo "help: This help info"
  echo "homeEntry: Generates the default Home folder entry"
  echo "launcherChecks: Outputs some debug info"
  echo "loadCurrSubmenu: Loads an existing submenu into CURRSUBMENU array for editing"
  echo "loadDebugLibrary: Loads the debug library"
  echo "loadEssentials: Runs essential checks and populates essential variables and arrays"
  echo "loadUserConfig: Imports the user's configurations"
  echo "mintConfigEntry: Generates the mintConfig entry"
  echo "mintInstallEntry: Generates the mintInstall entry"
  echo "outputMenu: Writes the entire menu to disk"
  echo "processAddQ: Processes apps to be added"
  echo "processRemoveQ: Processes apps to be removed"
  echo "quitEntry: Generates the Quit entry"
  echo "runEntry: Generates the Run entry"
  echo "separator: Generates the seperators"
  echo "sortArray: Sorts arrays alphabetically"
  echo "synapticEntry: Generates the Synaptic package manager entry"
  echo "terminalEntry: Generates the default terminal entry"
  echo "timerEnd: Stops the timer"
  echo "timerStart: Starts the timer"
  echo "updateCurCache: Updates the menu cache"
  echo "utilsGenerate: Generates the menu"
  echo "utilsGenerateWithZenity: Generates the menu with a Zenity progress bar"
  echo "utilsUpdate: Updates the menu"
  echo "utilsUsage: Outputs usage info"
  echo "welcomeMsg: Debug welcome message"
  echo "zenityProgress: Updates Zenity progress"

}