#!/bin/bash
# Copyright 2003, 2004 Stefano Falsetto <falsetto@gnu.org>
# Copyright 2008 David Egan Evans <sinuhe@gnu.org>
#
# This program is free software.  You can redistribute it, or modify it,
# or both, under the terms of the GNU General Public License version 3
# (or any later version) as published by the Free Software Foundation.
#
# 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>.
#

# Don't allow pressing CTRL+C (or sending SIGINT) during TEMPDIR generation
trap '' 2

# Following code is taken from:
# checkinstall v1.4.1 (c) Felipe Eduardo Sanchez Diaz Duran
# (with little changes)

# Find a safe TEMPDIR
BASE_TMP_DIR="/tmp"
TEMPDIR=${BASE_TMP_DIR}/`awk 'BEGIN { srand(); for (i=1;i<21;i++) { a=95; while (a > 90 && a < 97) { a=65+int(50*rand())}; printf("%c", a) } }'`
[ -e "$TEMPDIR" ] && rm -rf $TMP_DIR
if [ -e "$TEMPDIR" ]; then
   echo
   echo "My temp dir exists already."
   echo "This looks like a symlink attack!"
   echo
   echo "*** Aborting"
   echo
   exit 1
fi

mkdir $TEMPDIR
chmod 1700 $TEMPDIR
RETURN=$?

if [ $RETURN -gt 0 ]; then
   echo
   echo "**** Failed to create temp dir!"
   echo "**** Do you have write permission for ${BASE_TMP_DIR}?"
   echo
   echo '**** Aborting execution.'
   echo
   exit  $RETURN
fi

# Original code from stefko begin here :-)

TMPFIL=$TEMPDIR/rotttempfile.$$
NEWtmpFILE=$TEMPDIR/rottnewtmpfile.$$
trap - 2
trap 'rm -Rf $TEMPDIR ; exit $USCITA' 0 2

MAINPATH="./rc"
MAINRC="$MAINPATH/rc"
MONTRC="$MAINPATH/monthly"
WEEKRC="$MAINPATH/weekly"
DAYRC="$MAINPATH/daily"

syn_error() {
  echo "Syntax error at $nline: $3"
  echo ">>> $2"
  while [ 0 ]; do
    echo "Press ENTER to re-edit config file"
    echo "Press CTRL+C to break process and undo last changes..."
    read Q
    if [ "$Q" = "" ]; then
      return
    fi
  done</dev/stdout
}

to_skip () {
  if [ $(expr "$1" : "[[:space:]]*#.*") -ne 0 ] || \
     [ "$(echo "$1"|tr -d ' ')" = "" ]; then
    return 1
  fi
  return 0
}
      
menu_rc () {
  local esci=0
  local KEYS="|packer|compress|packdir|fromuser|touser|notifempty|ifempty|mail|unpacker|uncompress|pager|extension|maxdepth|follow_symlinks|nomail|nomissingok|missingok|nocompress|notifempty|nocreate|createdir|dir_perm|dir_own|dir_grp|"
  local OnOffOpt="|notifempty|ifempty|follow_symlinks|nomail|nomissingok|missingok|nocompress|notifempty|nocreate|createdir|remove_missing|"
  local SKEYS=${KEYS//|/ }
  #echo "ora skeys=$SKEYS"
  if [ -e $MAINRC ]; then
    chmod 600 $MAINRC
    cp -f $MAINRC $NEWtmpFILE
    chmod 600 $NEWtmpFILE
  fi

  # TODO: cambiare!!!
  . $MAINRC
while [ 0 ]; do
  local c=0
  for i in $SKEYS; do
    value="$(eval echo \$$i)"
    #echo "Here value of $i=$value"
    if [ -z "$value" ]; then
      value="-unset-"
    fi
    values[$c]="$i"
    c=$[ c + 1 ]
    values[$c]="$value"
    c=$[ c + 1 ]
  done
  #echo "press enter"
  #read
  dialog --title "Main Configuration File" \
         --menu "These are valid options and its values\n" \
         20 75 12 "${values[@]}" \
         " "             " " \
         "Save Settings" "Save & Exit" 2>$TMPFIL
  RET_VAL=$?
  if [ $RET_VAL -ne 0 ]; then
    echo "Aborting with $RET_VAL "
    return 1
  fi
  VAR_NAME="$(cat $TMPFIL)"
  if [ "$VAR_NAME" = " " ]; then
    continue
  fi
  if [ "$VAR_NAME" = "Save Settings" ]; then
    echo "Exiting and saving main rc file..."
    >$MAINRC.new
    for s in $SKEYS; do
      if [ -z "$(eval echo \$$s)" ]; then
        echo "$s=" >>$MAINRC.new
      else
        echo "$s=\"$(eval echo \$$s)\"" >>$MAINRC.new
      fi
    done
    return
  fi

  VAR_VALUE="$(eval echo \$$VAR_NAME)"
  if [ $(expr "$OnOffOpt" : ".*|$VAR_NAME|.*" ) -eq ${#OnOffOpt} ]; then
    [ ! -z $VAR_VALUE ] && action="DE-activate" || action="activate"
    dialog --title "Modifiyng $VAR_NAME value" \
           --yesno "Do you wants to $action this feature?" 7 70
    if [ $? -eq 0 ]; then
      if [ "$action" = "activate" ]; then
        eval $VAR_NAME=1
      else
        eval $VAR_NAME=
      fi
    else
      if [ "$action" = "activate" ]; then
        eval $VAR_NAME=
      else
        eval $VAR_NAME=1
      fi
    fi
  else
    dialog --title "Modifiyng $VAR_NAME value:" --inputbox "" 10 75 \
       "$VAR_VALUE" 2>$TMPFIL
    if [ $? -ne 0 ]; then
      eval $VAR_NAME="$VAR_VALUE"
    else
      eval $VAR_NAME="\"$(cat $TMPFIL)\""
    fi
  fi
done

#######################################################################################
return
  
  nline=0
  while [ $esci -ne 1 ]; do
    $ED +$nline $NEWtmpFILE
    echo "Checking syntax..."
    nline=0
    ERROR=
    while read line; do
      nline=$[ nline + 1 ]
      to_skip "$line"
      if [ $? -eq 1 ]; then
        continue
      fi
      if [ $(expr "$line" : ".*=.*") -eq 0 ]; then
        syn_error $nline "$line" "Invalid syntax"
        ERROR=1
        break
      fi
      KEY="$(echo "$line"|cut -d'=' -f1)"
      VALUE="$(echo "$line"|cut -d'=' -f2)"
      if [ -z "$VALUE" ]; then
        syn_error $nline "$line" "Zero value to $KEY"
        ERROR=1
        break
      fi
      if [ $(expr "$KEYS" : ".*|$KEY|.*") -eq 0 ]; then
        syn_error $nline "$line" "Unknown keyword"
        ERROR=1
        break
      fi
    done<$NEWtmpFILE
    if [ -z "$ERROR" ]; then
      esci=1
    fi
  done
  echo "Done."
  install_rc "$MAINRC"
}

ltrim () {
  left=0
  while [ "${1:$left:1}" = " " ]; do
    left=$[ left + 1 ]
  done
  local tmp=${1//\"/\\\"}
  eval "$2=\"${tmp:$left}\""
}

rtrim () {
  right=${#1}
  while [ "${1:$right-1:1}" = " " ]; do
    right=$[ right - 1 ]
  done
  eval "$2=\"${1:0:$right}\""
}

menu_rc
