#!/bin/bash
#
# Starting and configuring the linux updater
#

################################# define variables ################################
script_dir=`dirname $0`
VarLogFile="tc3g_updater.log"
VarLogFilePath="$script_dir/$VarLogFile"
VarTargetName="TC3G"
VarDataFolderPath="$script_dir/data"
VarTmpFile="temp.txt"
VarConfigFile="config.ttl"
VarVersionNumber="v2.03r0"
VarUpdateNow=false
PASSW=

################################# export variables ################################
export script_dir
export VarLogFilePath
export VarTargetName
export VarDataFolderPath
export VarConfigFile
export VarTmpFile
export VarVersionNumber
export VarUpdateNow
export PASSW

##################################### functions ###################################

function prepareHelpText()
{
   # print help text
   echo "The Linux updater is a SW tool to update the ESX-$VarTargetName from a Linux OS. " > "$script_dir/$VarTmpFile"
   echo "It accepts the following command-line options:"                                   >> "$script_dir/$VarTmpFile"
   echo ""                                                                                 >> "$script_dir/$VarTmpFile"
   echo "  -n | --now"                                                                     >> "$script_dir/$VarTmpFile"
   echo "    Starts updating without GUI, uses configuration from config.ttl file."        >> "$script_dir/$VarTmpFile"
   echo "  -h | --help"                                                                    >> "$script_dir/$VarTmpFile"
   echo "    Prints this help message."                                                    >> "$script_dir/$VarTmpFile"
   echo "  -v | --version"                                                                 >> "$script_dir/$VarTmpFile"
   echo "    Prints the version number of the $VarTargetName updater."                     >> "$script_dir/$VarTmpFile"
   echo ""                                                                                 >> "$script_dir/$VarTmpFile"
   echo "The $VarTargetName updater can be executed without a command-line option. In"     >> "$script_dir/$VarTmpFile"
   echo "that case one can adapt the updater on the fly by following the"                  >> "$script_dir/$VarTmpFile"
   echo "instructions on the screen."                                                      >> "$script_dir/$VarTmpFile"
   echo ""                                                                                 >> "$script_dir/$VarTmpFile"
   echo "For further information, please refer to the ESX-$VarTargetName User Manual."     >> "$script_dir/$VarTmpFile"
   echo ""                                                                                 >> "$script_dir/$VarTmpFile"
}


######################################## main #####################################

# prepare help text
prepareHelpText

# read input parameters
if [ "$1" == "--help" -o "$1" == "-h" ]; then
   # print help text
   cat "$script_dir/$VarTmpFile"
   exit

elif [ "$1" == "--version" -o  "$1" == "-v" ]; then
   # print version number
   echo "$VarVersionNumber"
   exit

elif [ "$1" == "--now" -o  "$1" == "-n" ]; then
   # update now, with settings from config.ttl
   VarUpdateNow=true
fi

# become sudo if not already
if [ -t 0 -a `id -u` -ne 0 ]; then
   if [ "$VarUpdateNow" == "true" ]; then
      # Read Password without GUI
      echo -n "Enter [sudo] password: "
      read -s PASSW
   else
      # get sudo password via GUI
      PASSW=$(zenity --entry --hide-text \
                     --cancel-label="Exit updater" \
                     --ok-label="Continue" \
                     --text "Enter sudo password:" \
                     --title "$VarTargetName Updater")
   fi

   # Check for null entry or cancellation.
   if [[ ${?} != 0 || -z ${PASSW} ]]; then
       exit
   fi

   # Check that the password is valid.
   if  ! sudo -kSp '' [ 1 ] <<<"${PASSW}" 2>/dev/null
      then
      if [ "$VarUpdateNow" == "true" ]; then
         # without GUI
         echo -e "\n\033[31mWrong password!\033[37m\nDid you forget how to sudo?"
      else
         # with GUI
         $(zenity --info \
                  --title "$VarTargetName Updater" \
                  --text "Wrong password!\nDid you forget how to sudo?")
      fi
      exit
   fi
fi

# check if all necessary scripts have the correct file permissions
if [ -d "$script_dir/scripts" -a ! -x "$script_dir/scripts/Update_linux" ]; then
   `chmod -R 764 $script_dir/scripts`
fi
if [ -d "$script_dir/make_FIT" -a ! -x "$script_dir/make_FIT/make_udimg" ]; then
   `chmod -R 764 $script_dir/make_FIT`
fi

# call main script and start logging
if [ -n "$VarLogFilePath" ]; then
   script -f -c "$script_dir/scripts/Update_linux" "$VarLogFilePath"
   killall cat

   # finalize / clean up log-file
   sed 's/- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \r//g' "$VarLogFilePath" > "$script_dir/$VarTmpFile"
   wait $!
   sed 's/| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | \r//g' "$script_dir/$VarTmpFile" > "$VarLogFilePath"
   wait $!
   sed 's/\/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \r//g' "$VarLogFilePath" > "$script_dir/$VarTmpFile"
   wait $!
   sed 's/\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \r//g' "$script_dir/$VarTmpFile" > "$VarLogFilePath"
   wait $!
   if [ -f "$script_dir/$VarTmpFile" ]; then
      `rm "$script_dir/$VarTmpFile"`
      wait $!
   fi
else
   echo -e "\033[31mError: Select log file!\n\033[37m"
   exit
fi


######################################## EOF ######################################
