#!/bin/bash
#
# Copyright (C) 2005-2008 Falko Schmidt.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies of the Software and its documentation and acknowledgment shall be
# given in the documentation and software packages that this Software was
# used.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# 
###############################################################################
#
# autoe version 0.1.4
# 
# A tool to build E17 Debian packages from CVS
#
# Author: Falko Schmidt <falko@alphagemini.org>
#
# Features:
#  * Menudriven via dialog
#  * Only build the components which got updated
#  * Manage build order (add, remove, reorder)
#  * Error handling
#  * Resetting of date-stamps
#  * Batch mode either via menu or via invocation with --batch
#
# Requirements:
#  * Terminal
#  * dialog
#  * dpkg-dev
#  * devscripts
#  * cdbs
#  * sudo, fakeroot or root access
#  * generous amount of diskspace (around 1.5Gb)
#
# Recommended:
#  * debarchiver for managing the repository structure
#  * a script like egpg for passwordless pgp signatures
#
# Important:
# You need to specify a custom pgp command or shell script in order to
# to sign packages without password prompt (man gpg). To use it invoke
# it with the -p paramater of dpkg-buildpackage in the $DBUILDPACKAGE
# variable (e.g. "dpkg-buildpackage -psomescript"). Alternatively you
# can specify $DBUILDPACKAGE e.g as "dpkg-buildpackage -uc -us" to not
# sign packages and sources at all.

# TODO:
#  * Finetune e_replace to reset only updated components
#  * Function to correct version number in changelog(.in) after checkout
#  * If a configure fails and autoe drops back to the main menu, the build
#    queue additionally contains the previous build job, even though it
#    was built correctly

# Configuration moved to $HOME/.autoe!

################################
# Don't edit beyond this point #
################################

# Make compliation faster by forking three processes at once
export MAKE="make -j3"

# Set the date
DATE=`date +$DATEFORMAT`

# The build list
ULIST=""

# List of addable items
ALIST=""


#-------------------------------------------------------------------#
# function which prints the help                                    #
#-------------------------------------------------------------------#
function e_printhelp {

   dialog --title "autoe - help" --clear \
          --msgbox " \
\n\
Welcome to autoe. This script is made for maintaining the\n \
E17 Debian repository at edevelop.org/debian. It might as\n \
well work for other source trees with some adjustments.\n \
\n\
Before using this script you should really make sure to\n \
edit this file and adjust the settings accordingly.\n \
\n\
The following lines describe the main menu option:\n\
\n\
* BatchRun     - This option runs of the following commands: \n \
                 * Update \n \
                 * CheckUpdates \n \
                 * Build \n \
                 * user defined commands (see '\$BATCHCOMMANDS') \n \
               These routines are automatically executed if \n \
               autoe is invoked with --batch or -b \n \
 \n \
* Update       - Checks out the CVS repository. \n \
 \n \
* CheckUpdates - Scan for items which have been updated \n \
                 during the checkout. Overrides the previous \n \
                 build list. \n \
 \n \
* ViewList     - Display the current build list. \n \
 \n \
* ReorderList  - Reorder items on the build list. \n \
 \n \
* RemoveItem   - Remove items from the build list. \n \
 \n \
* AddItem      - Manually add items to the build list. \n \
 \n \
* Build        - Build the items on the build list. \n \
 \n \
* Help         - Print this help text. \n \
 \n \
* Install      - Installs all Debian packages found. \n \
 \n \
* Move         - Moves .deb, .dsc, .changes and .tar.gz \n \
                 files (the latter only if applicable) \n \
                 to '\$MOVEDIR' \n \
 \n \
* Custom       - Prompt for running external scripts which \n \
                 can be specified with the '\$CUSTSCRIPTS' \n \
                 variable. \n \
 \n \
* Quit         - Quit autoe. The original build list can be \n \
                 recovered by choosing 'CheckUpdates' the \n \
                 next time you run autoe. \n \
 \n " 20 72
}

#-------------------------------------------------------------------#
# function which prints the list of remaining build jobs            #
#-------------------------------------------------------------------#
function e_printlist {
   
  if [[ $ULIST = "" ]]
  then
	  dialog --title "autoe - no build jobs" --clear \
	  --msgbox "The build queue is empty. Please make sure to check for updates before." 6 42
	  return
  fi

  COUNTER=1
  echo "" > /tmp/autoe-listtmp
  for e in $ULIST
  do
     echo "   "$COUNTER") "$e >> /tmp/autoe-listtmp
     let COUNTER=$COUNTER+1
  done
  dialog --title "autoe - current build jobs" --clear \
  --textbox /tmp/autoe-listtmp 18 46
  rm /tmp/autoe-listtmp
   
}

#-------------------------------------------------------------------#
# function to replace date stamps and adjust maintainer entries     #
#-------------------------------------------------------------------#
function e_replace {

   DATE=`date +$DATEFORMAT`
   
   cd trunk

   for file in $(find -name 'changelog' -or -name 'changelog.in')
   do 
      # add cvs+date stamp
      sed -e 's/(\([0-9]\(\.[0-9]\+\)\+\)\-1)/(\1\-0cvs'$DATE')/g' $file > 58239
      cat 58239 > $file
      sed -e 's/(\(\@VERSION\@\)\-1)/(\1\-0cvs'$DATE')/g' $file > 58239
      cat 58239 > $file
      sed -e 's/(\([0-9]\(\.[0-9]\+\)\+\))/(\1\-0cvs'$DATE')/g' $file > 58239
      cat 58239 > $file
      sed -e 's/(\(\@VERSION\@\))/(\1\-0cvs'$DATE')/g' $file > 58239
      cat 58239 > $file
      sed -e 's/(\([0-9]\):\(\@VERSION\@\)\-1)/(\1:\2\-0cvs'$DATE')/g' $file > 58239
      cat 58239 > $file

      # adjust date
      sed -e 's/0cvs[0-9]*/0cvs'$DATE'/g' $file > 58239
      cat 58239 > $file

      if [ $REPLACE_CONTROL_MAINTAINER = "1" ]
      then
         # adjust maintainer
         sed -e 's/\-\-\ .*/\-\-\ '"$MAINTAINERADDRESS"'\ \ Tue\,\ 7\ Mar\ 2006\ 13\:30\:15\ \+0000/g' $file > 58239
         cat 58239 > $file
      fi

   done


   if [ $REPLACE_CONTROL_MAINTAINER = "1" ]
   then
      for file in $(find -name 'control')
      do

         sed -e 's/Maintainer.*/Maintainer\:\ '"$MAINTAINERADDRESS"'/g' $file > 58239
         cat 58239 > $file
  
      done
   fi

   rm -f 58239
   cd ..

}

#-------------------------------------------------------------------#
# function which checks out the E17 CVS repository                  #
#-------------------------------------------------------------------#
function e_checkout {

   # Set the date again, in case this script runs nonstop.
   DATE=`date +$DATEFORMAT`
   
   (
   if [ "$DATERESET" = "1" ]
   then
	   echo "XXX"
	   echo "0"
	   echo "\n checking out the e17 branch"
	   echo "XXX"
   	   svn --non-interactive co http://svn.enlightenment.org/svn/e/trunk &> e17-log-co
	   
	   echo "XXX"
	   echo "50"
   else
	   echo "XXX"
	   echo "0"
	   echo "\n checking out the e17 branch"
	   echo "XXX"
   	   svn --non-interactive co http://svn.enlightenment.org/svn/e/trunk &> e17-log-co
	   
	   echo "XXX"
	   echo "99"
	   echo "\n done."
	   echo "XXX"
   fi
	   
   if [ "$DATERESET" = "1" ]
   then
	   echo "\n resetting the date for e17 branch ..."
	   echo "XXX"
	   e_replace

	   echo "XXX"
	   echo "99"
	   echo "\n done."
	   echo "XXX"
   fi

   ) |

   dialog --title "autoe - CVS checkout progress" --gauge "" 8 70 0

   # FIXME: this is a dirty hack and should be fixed upstream
   #rm -Rf e_modules/calendar e_modules/engage e_modules/winselector

   # dpkg-buildpackage needs all rules files to be executable
   find -name 'rules' -exec chmod +x {} \;

}

#-------------------------------------------------------------------#
# function which generates a list of updates elements so that       #
# only those are being built later.                                 #
# Needs a checkout log to parse, see e_checkout.                    #
#-------------------------------------------------------------------#
function e_checkupdates {
   
   # Trunk
   TRLIST=`grep -e U\ \ \ \ trunk e17-log-co -e A\ \ \ \ trunk | sed -e 's/U\ \ \ \ trunk\///g' | sed -e 's/A\ \ \ \ trunk\///g' | sed -e 's/\/.*//g' | uniq | grep -v -e '[A-Z]\+' | sed -e 's/.*/E17\/&/g'`

   # Proto
   PRLIST=`grep -e U\ \ \ \ trunk\/PROTO -e A\ \ \ \ trunk\/PROTO e17-log-co | sed -e 's/U\ \ \ \ trunk\/PROTO\///g' | sed -e 's/A\ \ \ \ trunk\/PROTO\///g' | sed -e 's/\/.*//g' | uniq | sed -e 's/.*/PROTO\/&/g'`

   # Misc
   MILIST=`grep -e U\ \ \ \ trunk\/MISC -e A\ \ \ \ trunk\/MISC e17-log-co | sed -e 's/U\ \ \ \ trunk\/MISC\///g' | sed -e 's/A\ \ \ \ trunk\/MISC\///g' | sed -e 's/\/.*//g' | uniq | sed -e 's/.*/MISC\/&/g'`

   # e-modules
   MOLIST="empty"
   MOLIST=`grep -e U\ \ \ \ trunk\/E\-MODULES\-EXTRA -e A\ \ \ \ trunk\/E\-MODULES\-EXTRA e17-log-co | sed -e 's/U\ \ \ \ trunk\/E\-MODULES\-EXTRA\///g' | sed -e 's/A\ \ \ \ trunk\/E\-MODULES\-EXTRA\///g' | sed -e 's/\/.*//g' | uniq | sed -e 's/.*/E-MODULES-EXTRA\/&/g'`
   if [[ $MOLIST != "empty" ]]
   then
	   MOLIST="E-MODULES-EXTRA"
   fi

   TLIST="$TRLIST $PRLIST $MILIST $MOLIST"

   if [[ $1 != "batch" ]]
   then

      if [[ $TLIST == "   " ]]
      then
         $TLIST=""
         dialog --title "autoe - no updates found" --clear \
         --yesno "No updates found or CVS error.\nDo you want to view the log file?" 6 40
         case $? in
            0)
            dialog --clear --title "autoe - checkout log file" \
            --textbox e17-log-co 20 75;;
            1)
            echo "no chosen";;
            255)
            exit;;
         esac
      else
         echo "" >> /tmp/autoe-listtmp
	 # FIXME: update this text (skipped, no dir tags)
         echo "The following updates to the source trunk have been found." >> /tmp/autoe-listtmp
         echo "Unlike the resulting build queue this list is neither sorted" >> /tmp/autoe-listtmp
         echo "nor stripped of items which are not meant to be built." >> /tmp/autoe-listtmp
         echo "Existing build jobs will be merged." >> /tmp/autoe-listtmp
         echo "" >> /tmp/autoe-listtmp
         COUNTER=1
         for e in $TLIST
         do
	    APPENDIX=""

            titem=`echo $e| sed -e 's/E17//g'`
            if [ ! -f trunk/$titem/debian/control ]
            then
	       APPENDIX=" (no debian dir)"
            fi

            echo $DONTBUILDLIST | grep \ $e\   && \
	       APPENDIX=" (skipped)"
            echo "   "$COUNTER") "$e $APPENDIX >> /tmp/autoe-listtmp
            let COUNTER=$COUNTER+1
         done
         dialog --title "autoe - updates found" --clear \
         --textbox /tmp/autoe-listtmp 20 75
         rm /tmp/autoe-listtmp
      fi

   fi
   
   # merge current build queue with new updates:
   ULIST="$ULIST $TLIST"
     
   for item in $ULIST
   do
	   echo $item >> /tmp/autoe-listtemp2
   done

   ULIST="$(cat /tmp/autoe-listtemp2|sort|uniq)"

   # remove items from the $DONTBUILDLIST
   for item in $DONTBUILDLIST
   do
   	ULIST="${ULIST/$item/}"
   done

   # remove items which don't have a ./debian directory
   TULIST="$ULIST"
   for item in $TULIST
   do
	titem=`echo $item| sed -e 's/E17//g'`
	if [ ! -f trunk/$titem/debian/control ]
	then
		ULIST="${ULIST/$item/}"
	fi
   done

   rm /tmp/autoe-listtemp2
   
   e_sort

   if [[ $GENCHANGELOGS == "1" ]]
   then
      e_generate_changelog
   fi
   
}

#-------------------------------------------------------------------#
# function which generates debian/changelog automatically by        #
# parsing cvs logs and merging it with changelog.in.                #
# requires a cvs checkout log, see e_checkout.                      #
#-------------------------------------------------------------------#

function e_generate_changelog {
  

   AMOUNT=0
   for file in $ULIST
   do
      let AMOUNT=$AMOUNT+1
   done
   
   if [[ $AMOUNT = 0 ]]
   then
      return
   fi

   COUNTER=0
   (
      for update in $ULIST
      do
         echo "XXX"
         PCT=`expr \( \( 100 \/ $AMOUNT  \) \* $COUNTER \)`
         echo $PCT
	 echo "  generating changelog for "$update" ..."
         echo "XXX"
         
	 update=`echo $update | sed -e 's/E17\///g'`
         cd $WORKDIR && cd trunk && cd $update && svn --non-interactive log -l $GENCHANGELOGS_NUMENTRIES > /tmp/autoe-cvslog-log 2> /tmp/autoe-cvslog-err
         RC=$?

         if [ $RC -ne 0 ]
         then
            echo "cvslog" > /tmp/autoe-rs
            return
         fi
         
         LINE=2
	 MAXLINES=`cat /tmp/autoe-cvslog-log|wc -l`
	 while [ "$LINE" -le "$MAXLINES" ]
         do
	    # begin of changelog entry

	    # TODO: add all the mappings foldername -> debian package name
	    packagename=$update
	    if [[ $packagename == "e" ]]
	    then
		    packagename="e17"
	    elif [[ $packagename == "edje_editor" ]]
	    then
		    packagename="edje-editor"
	    elif [[ $packagename == "e_dbus" ]]
	    then
		    packagename="edbus"
	    elif [[ $packagename == "E-MODULES-EXTRA" ]]
	    then
		    packagename="emodules"
	    fi

            echo $packagename "(@VERSION@-1)" "unstable; urgency=low" >> /tmp/autoe-cvs-message-temp
	    echo >> /tmp/autoe-cvs-message-temp

	    # extract the time stamp and nick name associated with the commit
	    TS=`sed -n "$LINE"p /tmp/autoe-cvslog-log | sed -e 's/\S\+\ |\ \S\+\ |\ \S\+\ \(\S\+\)\ \(\S\+\)\ (\(.*\)).*/\3\ \1\ \2/g'`
	    NAME=`sed -n "$LINE"p /tmp/autoe-cvslog-log | sed -e 's/\S\+\ |\ \(\S\+\).*/\1/g'`
	    echo "  [ "$NAME" ]" >> /tmp/autoe-cvs-message-temp

	    # determine the commit log text
	    MESSAGELINES=1
	    let MESSAGELINE=$LINE+2
	    while [ "$MESSAGELINE" -gt 0 ]
	    do
		MESSAGE=`sed -n "$MESSAGELINE"p /tmp/autoe-cvslog-log`
	        echo "  "$MESSAGE >> /tmp/autoe-cvs-message-temp
		let NEXTLINE=$MESSAGELINE+1

		if [[ `sed -n "$NEXTLINE"p /tmp/autoe-cvslog-log` == "------------------------------------------------------------------------" ]]
		then
		   let LINE=$NEXTLINE
		   MESSAGELINE=-1
		fi
		let MESSAGELINE=$MESSAGELINE+1
	    done

	    let LINE=$LINE+1
	    echo >> /tmp/autoe-cvs-message-temp
            echo " --" $MAINTAINERADDRESS $TS >> /tmp/autoe-cvs-message-temp
	    echo >> /tmp/autoe-cvs-message-temp
	 done
	 
	 # delete the last line of the file
	 sed '$d' /tmp/autoe-cvs-message-temp > /tmp/autoe-cvs-message-temp2
	 rm -f /tmp/autoe-cvs-message-temp
	 mv /tmp/autoe-cvs-message-temp2 /tmp/autoe-cvs-message-temp
	 # back up the old changelog.in rather than deleting it
	 mv debian/changelog.in debian/changelog.in.org
	 mv /tmp/autoe-cvs-message-temp debian/changelog.in

         let COUNTER=$COUNTER+1
      done

      echo "ok" > /tmp/autoe-rs

   ) |
   dialog --title "autoe - changelog generation progress" --gauge "" 8 60 0
   
   STAGE=`cat /tmp/autoe-rs`
   rm -f /tmp/autoe-rs
   
   if [[ $STAGE = "cvslog" ]]
   then
      --yesno "CVS log retrieval failed for the package $file.\nDo you want to view the log file?" 6 50
      case $? in
         0)
         dialog --clear --title "autoe - changelog generation log file" \
         --textbox /tmp/autoe-cvslog-err 20 75
	 rm -f /tmp/autoe-cvslog-log;;
         1)
         return;;
         255)
         return;;
      esac
      dialog --title "autoe - continue?" --clear \
      --msgbox "Will drop back to the main menu.\nPlease run 'CheckUpdates' again once the issue is fixed." 6 55
      return
   fi

   if [[ $DATERESET == "1" ]]
   then
	   e_replace
   fi   

}

#-------------------------------------------------------------------#
# function which adds debian/changelog.in to the configure.in       #
# script of the source specified as an argument, if needed.         #
#-------------------------------------------------------------------#

function e_modifyautogen {
   
   if [ -f $1/configure.in ]
   then
      grep -e debian\/changelog $1/configure.in || \
         sed -e '/AC_OUTPUT(\[/a\
debian\/changelog' $1/configure.in > /tmp/autoe-new-configure.in
      if [ -f /tmp/autoe-new-configure.in ]
      then
         mv $1/configure.in $1/configure.in.org
         mv /tmp/autoe-new-configure.in $1/configure.in
      fi
   fi
   if [ -f $1/configure.ac ]
   then
      grep -e debian\/changelog $1/configure.ac || \
         sed -e '/AC_CONFIG_FILES(\[/a\
debian\/changelog' $1/configure.ac > /tmp/autoe-new-configure.ac
      if [ -f /tmp/autoe-new-configure.ac ]
      then
         mv $1/configure.ac $1/configure.ac.org
         mv /tmp/autoe-new-configure.ac $1/configure.ac
      fi
   fi
}

#-------------------------------------------------------------------#
# function which builds selected elements of the source tree        #
#-------------------------------------------------------------------#

function e_build {
   
   if [[ $ULIST = "" ]]
   then
	dialog --title "autoe - empty queue" --clear \
	--msgbox \
	"The build queue is empty.\nPlease make sure to check the trunk." 6 40
	case $? in
	   0)
	   return;;
	   255)
	   return;;
	esac
   fi
   
   # how many build jobs?
   AMOUNT=0
   for i in $ULIST
   do
	   let AMOUNT=$AMOUNT+1
   done

   # we need steps for configuring, building and installing
   let AMOUNT=$AMOUNT*3

   # remember the parent dir
   PD=$(pwd)
   
   COUNTER=0
   (
   for job in $ULIST
   do
           # backup the build list
	   echo $ULIST > /tmp/autoe-ulist

	   jobname=$job
	   job=`echo $job | sed -e 's/E17\///g'`

	   echo "XXX"
	   PCT=`expr \( \( 100 \/ $AMOUNT  \) \* $COUNTER \)`
	   echo $PCT
	   echo "\n  configuring $job ..."
	   echo "XXX"
	   ######################################
	   # run autogen.sh and configure
	   ######################################

	   # fix configure.in to include debian/changelog.in
	   e_modifyautogen trunk/$job

	   cd trunk/$job
           
           ./autogen.sh --prefix=/usr &> /tmp/autoe-conf-log
           RC=$?
           
	   # check the return code
           if [ "$RC" -ne 0 ]
           then
		   echo "config" > /tmp/autoe-rs
		   echo "$job" > /tmp/autoe-rsi
		   return
	   fi
	   let COUNTER=$COUNTER+1
	   ######################################
           
	   echo "XXX"
	   PCT=`expr \( \( 100 \/ $AMOUNT  \) \* $COUNTER \) \+ 1`
	   echo $PCT
	   echo "\n  compiling $job ..."
	   echo "XXX"
	   ######################################
           # build the package
	   ######################################
           $DBUILDPACKAGE -m"$MAINTAINERADDRESS" -e"$MAINTAINERADDRESS" &> /tmp/autoe-build-log
           RC=$?
           # check the return code
           if [ "$RC" -ne 0 ]
           then
		   echo "compile" > /tmp/autoe-rs
		   echo "$job" > /tmp/autoe-rsi
		   return
           fi

	   # remove the successfully built item from the list
	   ULIST="${ULIST/$jobname/}"
           echo $ULIST > /tmp/autoe-ulist

	   let COUNTER=$COUNTER+1
	   ######################################
           
	   echo "XXX"
	   PCT=`expr \( \( 100 \/ $AMOUNT  \) \* $COUNTER \)`
	   echo $PCT
	   echo "\n  installing $job ..."
	   echo "XXX"
	   sleep 1
	   ######################################
           # install packages if wanted by user
	   ######################################
           if [[ $AUTOINSTALL == "1" ]]
           then
           if [[ $( echo $DONTBUILDLIST | grep $job" " | wc -c ) = 0 ]]
           then
              cd ..
              DEBIAN_PRIORITY="critical" dpkg -i *.deb &> /tmp/autoe-install-log
              RC=$?
              if [ $RC -ne 0 ]
              then
		   echo "install" > /tmp/autoe-rs
		   echo "$job" > /tmp/autoe-rsi
		   return
              fi
              
              # move packages if wanted by user
              if [[ $AUTOMOVE = 1 ]]
              then
                 # check whether we build source files tarballs
                 if [[ $( echo $DBUILDPACKAGE | grep '\ \-b' | wc -c) > 0 ]]
                 then
                    mv *.deb *.changes $MOVEDIR
                 else   
                    mv *.deb *.tar.gz *.changes *.dsc $MOVEDIR
                 fi
              fi
           fi
           fi
           # return to parent dir
           cd $PD
	   ######################################


	   let COUNTER=$COUNTER+1

   done
   echo XXX
   echo "100"
   echo "\n Done."
   echo "XXX"
   sleep 1
   echo "ok" > /tmp/autoe-rs
   ) |
   dialog --title "autoe - build progress" --gauge "" 8 60 0

   STAGE=`cat /tmp/autoe-rs`
   rm -f /tmp/autoe-rs
   COMPILE_FAILED=0

   if [[ $STAGE = "config" ]]
   then
      JOB=`cat /tmp/autoe-rsi`
      rm -f /tmp/autoe-rsi
      COMPILE_FAILED=1
      dialog --title "autoe - configure error" --yesno "Something went wrong while running autogen.sh and configure for $JOB.\nDo you want to view the log file?" 0 0
      case $? in
         0)
         dialog --clear --title "autoe - checkout log file" \
         --textbox /tmp/autoe-conf-log 20 75;;
         1)
         cd $PD;;
         255)
         cd $PD;;
      esac
      dialog --title "autoe - continue?" --clear \
      --msgbox "Will drop back to the main menu.\nPlease run 'Build' again once the issue is fixed." 6 55
      cd $PD
#      return
   elif [[ $STAGE = "compile" ]]
   then
      JOB=`cat /tmp/autoe-rsi`
      rm -f /tmp/autoe-rsi
      COMPILE_FAILED=1
      dialog --title "autoe - build error" --clear \
      --yesno "Something went wrong while compiling and packaging $JOB.\nDo you want to view the log file?" 6 50
      case $? in
         0)
         dialog --clear --title "autoe - build log file" \
         --textbox /tmp/autoe-build-log 20 75;;
         1)
         cd $PD;;
	 255)
         cd $PD;;
      esac
      dialog --title "autoe - continue?" --clear \
      --msgbox "Will drop back to the main menu.\nPlease run 'Build' again once the issue is fixed." 6 55
      cd $PD
#      return
   elif [[ $STAGE = "install" ]]
   then
      JOB=`cat /tmp/autoe-rsi`
      rm -f /tmp/autoe-rsi
      COMPILE_FAILED=1
      dialog --title "autoe - install error" --clear \
      --yesno "Something went wrong while installing the package $JOB.\nDo you want to view the log file?" 6 50
      case $? in
         0)
         dialog --clear --title "autoe - install log file" \
         --textbox /tmp/autoe-install-log 20 75;;
         1)
         cd $PD;;
         255)
         cd $PD;;
      esac
      dialog --title "autoe - continue?" --clear \
      --msgbox "Will drop back to the main menu.\nPlease run 'Build' again once the issue is fixed." 6 55
      cd $PD
#      return
   else
      ULIST=`cat /tmp/autoe-ulist|sort|uniq`
   fi
   
   ULIST=`cat /tmp/autoe-ulist|sort|uniq`
   rm -f /tmp/autoe-ulist #FIXME
}

#-------------------------------------------------------------------#
# function which adds an item to the list of build jobs             #
#-------------------------------------------------------------------#
function e_add {

   # create a list of all available source trees - if not yet done
   if [[ $ALIST = "" ]]
   then
      dialog --sleep 30 --title "autoe - please wait" \
--infobox "A list of all available packages is being generated." 3 56 &
      # Libs and apps
      APLIST="$(ls trunk/ | grep -v PROTO | grep -v BINDINGS | grep -v BROKEN | grep -v DOCS | grep -v E-MODULES-EXTRA | grep -v E16 | grep -v MISC | grep -v OLD | grep -v TEST | grep -v THEMES | grep -v TMP | sed -e 's/\(.*\)/E17\/\1/g')"
      # Proto
      PRLIST="$(ls trunk/PROTO/ | sed -e 's/\(.*\)/PROTO\/\1/g')"
      # Misc
      MILIST="$(ls trunk/MISC/ | sed -e 's/\(.*\)/MISC\/\1/g')"
      # e-modules
      MOLIST="E-MODULES-EXTRA"

      ALIST="$APLIST $PRLIST $MILIST $MOLIST"
      echo $ALIST > /tmp/autoe-ALIST
   fi
  
   dialog --title "autoe - add build jobs" \
          --checklist "Choose all the items which should be added to the\nbuild queue.\n" 20 61 10 \
        `for i in $ALIST;do echo $i" . \n ";done` \
	2> /tmp/autoe-addchoice

   ULIST=$ULIST" "`cat /tmp/autoe-addchoice |sed -e 's/\"//g'`

   e_sort

   rm -f /tmp/autoe-addchoice
}

#-------------------------------------------------------------------#
# function which sorts the queue to reflect e17 build order         #
#-------------------------------------------------------------------#
function e_sort {

   # only E17 needs order
   LLIST=`echo $ULIST | sed -e 's/MISC\/[-0-9_A-Z.a-z]*//g' | sed -e 's/PROTO\/[-0-9_A-Z.a-z]*//g' | sed -e 's/E\-MODULES\-EXTRA]*//g'`
   # the rest
   RLIST=`echo $ULIST | sed -e 's/E17\/[-0-9_A-Z.a-z]*//g'`
   
   # the build order is defined in OLIST. now delete every item in OLIST
   # which is not in LLIST, thus OLIST contains all relevant items. then
   # delete every item in LLIST which is in the resulted OLIST to obtain
   # a list of non-relevant items in LLIST.
   OLIST="E17/eet E17/evas E17/ecore E17/e_dbus E17/efreet E17/embryo E17/edje E17/epeg E17/epsilon E17/esmart E17/emotion E17/ewl E17/etk E17/enhance E17/e"
   for i in $OLIST
   do
	   if [[ `echo $LLIST|grep $i|wc -c` == 0 ]]
	   then
		   OLIST="${OLIST/$i/}"
	   fi
   done
   for i in $LLIST
   do
	   if [[ `echo $OLIST|grep $i|wc -c` > 0 ]]
	   then
		   LLIST="${LLIST/$i/}"
	   fi
   done

   # this list contains all libraries - sorted
   LLIST="$OLIST $LLIST"

   # put them back together
   ULIST="$LLIST $RLIST"


}

#-------------------------------------------------------------------#
# function which removes items from the list of build jobs          #
#-------------------------------------------------------------------#
function e_remove {

   if [[ $ULIST = "" ]]
   then
	dialog --title "autoe - empty queue" --clear \
	--msgbox \
	"The build queue is empty.\nPlease make sure to check the trunk." 6 40
	case $? in
	   0)
	   return;;
	   255)
	   return;;
	esac
   fi

   dialog --title "autoe - delete build jobs" \
          --checklist "Check all the items which should be \n\
deleted from the build queue.\n" 20 61 10 \
        `for i in $ULIST;do echo $i" . \n ";done` \
	2> /tmp/autoe-delchoice

   DLIST=`cat /tmp/autoe-delchoice |sed -e 's/\"//g'`
   for i in $DLIST
   do
	   ULIST=${ULIST/$i/}
   done

   rm -f /tmp/autoe-delchoice
   
}

#-------------------------------------------------------------------#
# function which manages shuffling items in the build list          #
#-------------------------------------------------------------------#
function e_reorder {
   
   if [[ $ULIST = "" ]]
   then
	dialog --title "autoe - empty queue" --clear \
	--msgbox \
	"The build queue is empty.\nPlease make sure to check the trunk." 6 40
	case $? in
	   0)
	   return;;
	   255)
	   return;;
	esac
   fi

   dialog --title "autoe - reorder build jobs" \
          --radiolist "Choose the item which should be\n\
reordered in the build queue.\n" 20 61 10 \
        `for i in $ULIST;do echo $i" . \n ";done` \
	2> /tmp/autoe-reochoice

   RCHOICE=`cat /tmp/autoe-reochoice |sed -e 's/\"//g'`
   rm -f /tmp/autoe-reochoice

   if [[ "$RCHOICE" = "" ]]
   then
	dialog --title "autoe - error" --clear \
	--msgbox \
	"No item chosen, use the space key to select." 5 50
	case $? in
	   0)
	   e_reorder;;
	   255)
	   e_reorder;;
	esac
   fi
   
   e_place "$RCHOICE"
   e_printlist

}

#-------------------------------------------------------------------#
# function which places an build list item in front of another      #
#-------------------------------------------------------------------#
function e_place {
   
   dialog --title "autoe - reorder build jobs" \
          --radiolist "Choose an item where "$1"\n\
should be placed in front of.\n" 20 61 10 \
        `for i in $ULIST;do echo $i" . \n ";done` \
	2> /tmp/autoe-tarchoice
   
   TCHOICE=`cat /tmp/autoe-tarchoice |sed -e 's/\"//g'|sed -e 's/.$//g'`
   rm -f /tmp/autoe-tarchoice
   
   if [[ "$TCHOICE" = "" ]]
   then
	dialog --title "autoe - error" --clear \
	--msgbox \
	"No item chosen, use the space key to select." 5 50
	case $? in
	   0)
	   e_place $1;;
	   255)
	   e_place $1;;
	esac
   fi
  
   if [[ "$TCHOICE" = "$1" ]]
   then
	dialog --title "autoe - error" --clear \
	--msgbox \
	"Can't place an item in front of itself." 5 45
	case $? in
	   0)
	   return;;
	   255)
	   return;;
	esac
   elif [ `echo $ULIST | grep "$TCHOICE" | wc -c` > 0 ]
   then
   	ULIST="${ULIST/$1/}"
	SUBSTR=$1" "$TCHOICE
	ULIST="${ULIST/$TCHOICE/$SUBSTR}"
	return
   else
	dialog --title "autoe - error" --clear \
	--msgbox \
	"Bad option or bug in this script." 5 42
	case $? in
	   0)
	   return;;
	   255)
	   return;;
	esac
   fi

}

#-------------------------------------------------------------------#
# function which calls scripts defined by the user                  #
#-------------------------------------------------------------------#
function e_custom {
  
   COUNTER=0
   for i in $CUSTSCRIPTS
   do
	   let COUNTER=$COUNTER+1
   done
   if [[ $COUNTER = 0 ]]
   then
	   dialog --title "autoe - no commands found" --clear \
	          --msgbox "No commands were defined." 5 40
		  return
   fi

   dialog --title "autoe - custom applications" \
          --checklist "Choose commands which should be executed. The \nrunning order is defined by the item's position.\n" 20 61 10 \
        `for i in $CUSTSCRIPTS;do echo $i" . \n ";done` \
	2> /tmp/autoe-comchoice

   CCHOICE=`cat /tmp/autoe-comchoice |sed -e 's/\"//g'`
   for item in $CCHOICE
   do
           dialog --sleep 30 --title "autoe - please wait" \
           --infobox "Running "$item" ..." 3 50 &
	   sh $item &> /tmp/autoe-exe-log
           RC=$?
           if [ $RC -ne 0 ]
           then
	      dialog --title "autoe - execution error" --clear \
	      --yesno "Something went wrong while executing the file $item.\nDo you want to view the log file?" 6 50
	      case $? in
	         0)
	         dialog --clear --title "autoe - execution log file" \
	         --textbox /tmp/autoe-exe-log 20 75
		 rm -f /tmp/autoe-exe-log;;
	         1)
	         return;;
	         255)
	         return;;
	      esac
	      dialog --title "autoe - continue?" --clear \
	      --msgbox "Will drop back to the main menu.\nPlease run 'Custom' again once the issue is fixed." 6 55
	      return
           fi
   done

}

#-------------------------------------------------------------------#
# function which installs all packages in e17/, misc/ & e_modules   #
#-------------------------------------------------------------------#
function e_install {
   
   DLIST=$(find trunk -name '*.deb' | sed -e 's/\n/\ /g' )

   AMOUNT=0
   for file in $DLIST
   do
	   let AMOUNT=$AMOUNT+1
   done
   
   if [[ $AMOUNT = 0 ]]
   then
	   dialog --title "autoe - no packages found" --clear \
	   --msgbox "No Debian packages found! Nothing to do." 5 44
	   return
   fi

   COUNTER=0
   (
   for file in $DLIST
   do
	   echo "XXX"
	   PCT=`expr \( \( 100 \/ $AMOUNT  \) \* $COUNTER \)`
	   echo $PCT
	   echo "  installing "$file" ..."
	   echo "XXX"
	   DEBIAN_PRIORITY="critical" dpkg -i $file &> /tmp/autoe-install-log
           RC=$?
           if [ $RC -ne 0 ]
           then
		   echo "install" > /tmp/autoe-rs
		   return
           fi
              
	   let COUNTER=$COUNTER+1
   done
   echo "ok" > /tmp/autoe-rs
   ) |
   dialog --title "autoe - install progress" --gauge "" 8 60 0

   STAGE=`cat /tmp/autoe-rs`
   rm -f /tmp/autoe-rs
   
   if [[ $STAGE = "install" ]]
   then
      dialog --title "autoe - install error" --clear \
      --yesno "Something went wrong while installing the package $file.\nDo you want to view the log file?" 6 50
      case $? in
         0)
         dialog --clear --title "autoe - install log file" \
         --textbox /tmp/autoe-install-log 20 75
	 rm -f /tmp/autoe-install-log;;
         1)
         return;;
         255)
         return;;
      esac
      dialog --title "autoe - continue?" --clear \
      --msgbox "Will drop back to the main menu.\nPlease run 'Install' again once the issue is fixed." 6 55
      return
   fi
   
}

#-------------------------------------------------------------------#
# function which moves all repository relevant files to $MOVEDIR    #
#-------------------------------------------------------------------#
function e_move {
   
   find trunk -name '*.deb' -exec mv {} $MOVEDIR \;
   find trunk -name '*.changes' -exec mv {} $MOVEDIR \;  
   # check whether we build source files tarballs
   if [[ $( echo $DBUILDPACKAGE | grep '\-b' | wc -c) = 0 ]]
   then
      find trunk -name '*.tar.gz' -exec mv {} $MOVEDIR \;
      find trunk -name '*.dsc' -exec mv {} $MOVEDIR \;
      dialog --title "autoe - finished moving files" --clear \
      --msgbox "All files ending in either .deb, .changes .tar.gz or .dsc have been moved to $MOVEDIR. Please verify manually." 7 60
   else
      dialog --title "autoe - finished moving files" --clear \
      --msgbox "All files ending in either .deb or .changes have been moved to $MOVEDIR. Please verify manually." 7 60
   fi
   
}

#-------------------------------------------------------------------#
# function which calls scripts after BatchRun has finished          #
#-------------------------------------------------------------------#
function e_runbatchcommands {
  
   COUNTER=0
   for command in $BATCHCOMMANDS
   do
	   let COUNTER=$COUNTER+1
   done
   if [[ $COUNTER = 0 ]]
   then
	   dialog --title "autoe - no commands found" --clear \
	   --msgbox "No additional batch commands were specified. Done." 5 44
	   return
   fi
   if [[ $COMPILE_FAILED = 1 ]]
   then
	   return
   fi

   for command in $BATCHCOMMANDS
   do
           dialog --sleep 30 --title "autoe - please wait" \
           --infobox "Running "$command" ..." 3 50 &
	   sh $command &> /tmp/autoe-exe-log
           RC=$?
           if [ $RC -ne 0 ]
           then
	      dialog --title "autoe - execution error" --clear \
	      --yesno "Something went wrong while executing the file $command.\nDo you want to view the log file?" 6 50
	      case $? in
	         0)
	         dialog --clear --title "autoe - execution log file" \
	         --textbox /tmp/autoe-exe-log 20 75
		 rm -f /tmp/autoe-exe-log;;
	         1)
	         return;;
	         255)
	         return;;
	      esac
	      dialog --title "autoe - continue?" --clear \
	      --msgbox "Will drop back to the main menu.\nPlease run 'Batch Run' again once the issue is fixed." 6 55
	      return
           fi
   done

   dialog --title "autoe - finished" --clear \
   --msgbox "Finished running all batch jobs." 5 40

}

#-------------------------------------------------------------------#
# function which checks parses the config file in $HOME/.autoe      #
#-------------------------------------------------------------------#
function e_parseconfig {
   
   CONFIG=0
   if [ ! -e $HOME/.autoe ]
   then
cat > $HOME/.autoe <<endoffile
###############################
# autoe configuration options #
###############################

# Define the working directory. This is used for storing the CVS tree
# and packages prior to moving them (if $AUTOMOVE=0 then packages will
# stay here).
WORKDIR=/usr/src/e/svn/e

# Flag to automatically install packages after building them. This
# might be necessary to fulfill build requirements for later 
# packages. It's recommended to leave it enabled.
AUTOINSTALL=1

# Flag to automatically move packages to $MOVEDIR after installation.
# It's recommended to leave it enabled and to set a proper $MOVEDIR
AUTOMOVE=1

# Flag whether to automatically generate changelog files or not. If
# enabled, debian/changelog files will be automatically generated
# based upon CVS log data and debian/changelog.in information.
# New feature as of 0.1.4.
GENCHANGELOGS=0

# This number determines the amount of log entries to be integrated into
# the changelog file. The default is 20.
GENCHANGELOGS_NUMENTRIES=20

# Directory to move packages to, optionally every time a package builds 
# successfully - see $AUTOMOVE.
MOVEDIR=/var/lib/eupload/

# Flag to reset date after checkout.
# This is recommended if your debian/changelog files should have a date
# stamp of the form 'date +%Y%m%d' (can be specified below).
DATERESET=1

# Format of the date string used in debian/changelog files
DATEFORMAT=%Y%m%d

# List of scripts that can be executed from the menu.
CUSTSCRIPTS=/usr/local/bin/eupload.sh /usr/local/bin/foo.sh

# Maintainer string for entry in debian/changelog
MAINTAINERADDRESS=Foo Bar <foobar@example.org>

# define the dpkg-buildpackage command line
DBUILDPACKAGE=dpkg-buildpackage -pegpg -b

# Flag to control whether to replace the Maintainer string in debian/control.
# This doesn't need to be touched usually.
REPLACE_CONTROL_MAINTAINER=0

# Define packages which don't build (yet), so the build process ignores them
DONTBUILDLIST=E17/devs PROTO/evil E17/web MISC/erss MISC/nexus MISC/engage MISC/embrace MISC/elapse PROTO/etk_server E17/imlib2 E17/imlib2_loaders E17/imlib2_tools

# List of scripts that will be run after BatchRun finished creating
# debs.
BATCHCOMMANDS=/usr/local/bin/eupload.sh /usr/local/bin/foo.sh

# Set this variable to 0 once all options are configured.
NOTCONFIGURED=1
endoffile

   else
      grep -v \# "$HOME"/.autoe | sort | uniq | grep -n \= > /tmp/autoe-options

      LINES=`cat /tmp/autoe-options | wc -l`

      # skip empty line at the beginning
      LINE=1

      # read each variable and its argument and assign it in this script's
      # scope
      while [ $LINE -le $LINES ]
      do

	 let LINE=$LINE+1

         VARIABLE=`cat /tmp/autoe-options | grep ^$LINE\: | sed s/$LINE://g | sed s/\=.*//g `
	 ARGUMENT=`cat /tmp/autoe-options | grep ^$LINE\: | sed s/$LINE://g | sed s/.*\=//g `

	 if [[ $VARIABLE = "WORKDIR" ]]
	 then
            WORKDIR=$ARGUMENT
	 elif [[ $VARIABLE = "AUTOINSTALL" ]]
         then
            AUTOINSTALL=$ARGUMENT
	 elif [[ $VARIABLE = "AUTOMOVE" ]]
         then
            AUTOMOVE=$ARGUMENT
	 elif [[ $VARIABLE = "DATERESET" ]]
         then
            DATERESET=$ARGUMENT
	 elif [[ $VARIABLE = "DATEFORMAT" ]]
         then
            DATEFORMAT=$ARGUMENT
	 elif [[ $VARIABLE = "MOVEDIR" ]]
         then
            MOVEDIR=$ARGUMENT
	 elif [[ $VARIABLE = "CUSTSCRIPTS" ]]
         then
            CUSTSCRIPTS=$ARGUMENT
	 elif [[ $VARIABLE = "MAINTAINERADDRESS" ]]
         then
            MAINTAINERADDRESS=$ARGUMENT
	 elif [[ $VARIABLE = "DBUILDPACKAGE" ]]
         then
            DBUILDPACKAGE=$ARGUMENT
	 elif [[ $VARIABLE = "REPLACE_CONTROL_MAINTAINER" ]]
         then
            REPLACE_CONTROL_MAINTAINER=$ARGUMENT
	 elif [[ $VARIABLE = "DONTBUILDLIST" ]]
         then
            DONTBUILDLIST=$ARGUMENT
	 elif [[ $VARIABLE = "GENCHANGELOGS" ]]
         then
            GENCHANGELOGS=$ARGUMENT
	 elif [[ $VARIABLE = "GENCHANGELOGS_NUMENTRIES" ]]
         then
            GENCHANGELOGS_NUMENTRIES=$ARGUMENT
	 elif [[ $VARIABLE = "BATCHCOMMANDS" ]]
         then
            BATCHCOMMANDS=$ARGUMENT
	 elif [[ $VARIABLE = "NOTCONFIGURED" ]]
         then
            NOTCONFIGURED=$ARGUMENT
	 else
            echo "Your configuration file seems to miss the variable $VARIABLE"
	    echo "Please add it to $HOME/.autoe"
            exit 1
         fi

      done
      
      #rm -f /tmp/autoe-options
  fi

}

#-------------------------------------------------------------------#
# function which checks whether variables are set correctly         #
#-------------------------------------------------------------------#
function e_checkenv {

   FOUNDDIALOG=0
   for dpath in `echo $PATH | sed s/\:/\ /g`
   do
      if [ -e $dpath/dialog ]
      then
	  FOUNDDIALOG=1
      fi
   done
   if [[ $FOUNDDIALOG = "0" ]]
   then
      echo ""
      echo "   autoe couldn't find a dialog executable in the path."
      echo "   Please install dialog via apt."
      echo ""
      exit 0
   fi
   if [[ $NOTCONFIGURED != "0" ]]
   then
      echo ""
      echo "   autoe is not configured properly. Please edit $HOME/.autoe"
      echo "   and adjust the necessary options."
      echo ""
      exit 1
   fi
   
   for e in $CUSTSCRIPTS
   do
      if [ ! -e $e ] | [ ! -x $e ]
      then
         echo ""
	 echo "   Warning! The script "$e "was specified but"
	 echo "   \$CUSTSCRIPTS doesn't exist or is not executable."
	 echo ""
      fi
   done

   for e in $BATCHCOMMANDS
   do
      if [ ! -e $e ] | [ ! -x $e ]
      then
         echo ""
	 echo "   Warning! The script "$e "was specified but"
	 echo "   in \$BATCHCOMMANDS doesn't exist or is not executable."
	 echo ""
      fi
   done


   if [ ! -d $MOVEDIR ] && [ "$AUTOINSTALL" = "1" ]
   then
      echo ""
      echo "   Warning! Directory "$MOVEDIR "doesn't exist"
      echo "   (or is a file). Please provide a directory where"
      echo "   packages can be moved to by setting the \$MOVEDIR"
      echo "   variable."
      echo ""
      exit 1
   fi

   if [ ! -d $WORKDIR ]
   then
      echo ""
      echo "   Warning! Directory "$WORKDIR "doesn't exist"
      echo "   (or is a file). Please provide a directory where"
      echo "   this script should be operating in."
      echo ""
      exit 1
   fi

   # Modify DONTBUILDLIST for easier parsing of the last token.
   DONTBUILDLIST=$DONTBUILDLIST" ."
}

#-------------------------------------------------------------------#
# main                                                              #
#-------------------------------------------------------------------#

# parse the config file
e_parseconfig

# check the settings and manage command line options
e_checkenv
    
cd $WORKDIR

# Parse command line for batch mode
if [[ $1 = --batch ]] || [[ $1 = -b ]]
then
   e_checkout
   e_checkupdates "batch"
   e_build
   e_runbatchcommands
   exit 0
fi


# show the main menu
while $TRUE
do
   dialog --clear --item-help --title "autoe - main menu" \
          --menu "This program is used to maintain the Debian E17 repository at edevelop.org. See 'Help' for details.\n" 20 70 12 \
   	       "Batch Run"  "Runs 'Update trunk', 'Check trunk' and 'Build'" "afterwards custom commands will be executed, see 'Help'." \
   	       "Update trunk"  "Checks out the source tree from CVS" "" \
   	       "Check trunk"  "Finds packages which needs to be rebuild" "only updated source trees will be selected to cut down build time." \
   	       "View list"   "Lists the current build queue" "don't forget to choose 'check trunk' before." \
   	       "Reorder list"  "Reorder items on the build queue" "you can pick an item and place it somewhere else on the build queue." \
   	       "Add item"  "Queue a build item manually" "lets you choose an item from the list of all available source trees" \
   	       "Remove item"  "Remove an item from the build queue" " " \
   	       "Build"  "Builds all items found on the build queue" "make sure the order of the queue is sane before compiling." \
   	       "Install"  "Installs all available Debian packages (dangerous)" "this is usually not needed. better solve conflicts manually." \
	       "Move"  "Moves files to destination (dangerous)" "this is usually not needed. better solve conflicts manually." \
   	       "Custom"  "Invokes previously defined custom scripts"  "edit this script to define these scripts." \
   	       "Help"  "Displays help" " " \
   	       "Quit"  "Exit autoe" "the build queue can be restored by choosing 'Check trunk' again." \
   	       2> /tmp/autoe-menu1
   
   val=`cat /tmp/autoe-menu1`
   if [ "$val" = "Batch Run" ]; then
   	e_checkout
   	e_checkupdates "batch"
   	e_build
   	e_runbatchcommands
   elif [ "$val" = "Update trunk" ]; then
   	e_checkout
   elif [ "$val" = "Check trunk" ]; then
   	e_checkupdates
   elif [ "$val" = "View list" ]; then
   	e_printlist
   elif [ "$val" = "Reorder list" ]; then
   	e_reorder
   elif [ "$val" = "Add item" ]; then
   	e_add
   elif [ "$val" = "Remove item" ]; then
   	e_remove
   elif [ "$val" = "Build" ]; then
   	e_build
   elif [ "$val" = "Install" ]; then
   	e_install
   elif [ "$val" = "Move" ]; then
   	e_move
   elif [ "$val" = "Custom" ]; then
   	e_custom
   elif [ "$val" = "Help" ]; then
   	e_printhelp
   elif [ "$val" = "Quit" ]; then
	rm -f /tmp/autoe-*
   	exit
   else
	rm -f /tmp/autoe-*
   	exit
   fi
done
rm -f /tmp/autoe-*
reset

