#!/bin/sh
set -x



## set variables  
MGPSPOINTVERSION="0.030519";
DIALOG=${DIALOG=dialog}

if [ $GPSPOINT="" ]; then
   GPSPOINT=$(which gpspoint);
fi

if [ $GPSPORT="" ]; then
   GPSPORT=/dev/gps
fi

if [ $GPSDATADIR="" ]; then
   GPSDATADIR=$HOME/gpsdata
fi

tempfile=`tempfile -p GPSPT 2>/dev/null` || tempfile=/tmp/test$$
#trap "rm -f $tempfile" 0 1 2 5 15

WAYPOINTS=true;
ROUTES=false;
TRACKS=false;

############################################################# main menu
QUIT=0;
while [ $QUIT = 0 ]; do
   $DIALOG --clear --title "mgpspoint - main" \
      --menu "Welcome to menu for gpspoint. Mayby check settings first\
			and try info afterwards. Have fun. " 16 51 8 \
      "about" "" \
      "info" "info and test device" \
		"off" "turn off device"\
      "download" ""\
      "upload" ""\
      "settings" ""\
      "Xit" ""\
      2> $tempfile
   QUIT=$?

   CHOICE=`cat $tempfile`;

   case $QUIT in
      0)
      echo "'$CHOICE' chosen.";;
      1)
      echo "Cancel pressed.";;
      255)
      echo "ESC pressed.";;
   esac


   case $CHOICE in
      about) ############################################ about Window
         $GPSPOINT --version 2> $tempfile;
         GPSPOINTVERSION =`cat $tempfile`; 
         $DIALOG --title "about" --clear \
            --msgbox \
            "mgpspoint $MGPSPOINTVERSION \n
            \na dialog based frontend for gpspoint2 \n
            \n by Thomas Schank \n
            \n
            " 10 60
      ;;
      info) ########################################################### info - test
         echo "gpspoint binary: $GPSPOINT " > $tempfile
         echo "version: " >> $tempfile
         $GPSPOINT --version 2>> $tempfile
         echo "PORT port: $GPSPORT" >> $tempfile
         echo "-----------------------------------" >> $tempfile
         $GPSPOINT --port $GPSPORT "--test"  2>> $tempfile
         MESSAGE=`cat $tempfile`
         echo "$MESSAGE";
         $DIALOG --title "gps device info" --clear --textbox $tempfile 30 70 
      ;;
      download) ########################################################################### download 
         DOWNLOADMENU=true;
			FILE=$(date  +%y-%m-%d_somegpsdata.gpd);
         while  $DOWNLOADMENU ; do
				DOWNLOAD=true;
            $DIALOG --title "mgpspoint - download" --clear --menu "" 15 50 10 \
               "continue" ""\
               "waypoints" "$WAYPOINTS"  \
               "routes" "$ROUTES"  \
               "tracks" "$TRACKS"  \
					"DATADIR" "$GPSDATADIR" \
					"file" "$FILE" \
					"back" "" \
               "Xit" "" \
               2> $tempfile;
            CHOICE=$(cat $tempfile);
            case $CHOICE in
					continue)
						COMMAND="";
						if [ ! -d $GPSDATADIR ]; then 
							$DIALOG --yesno "$GPSDATADIR does not exist; try to create it?" 10 40;
							if [ $? = 0 ]; then mkdir $GPSDATADIR; fi
						fi
						if [ ! -d $GPSDATADIR ]; then DOWNLOAD=false; fi
						OUTPUTFILE="$GPSDATADIR/$FILE";
						if [ -e $OUTPUTFILE ]; then
							$DIALOG --yesno "$FILE does exist; overwrite ?" 10 40;
							if [ ! $? = 0 ]; then DOWNLOAD=false; fi
						fi
						if $DOWNLOAD; then
							if  $WAYPOINTS ; then COMMAND=" $COMMAND --download-waypoints " ; fi 
							if  $ROUTES ; then COMMAND=" $COMMAND --download-routes " ; fi 
  							if  $TRACKS ; then COMMAND=" $COMMAND --download-tracks " ; fi 
							$GPSPOINT -d $GPSPORT $COMMAND --output-file "$OUTPUTFILE";
							sleep 1;
							DOWNLOADMENU=false;
						fi
					;;
               waypoints) if [ $WAYPOINTS = false ]; 
						then WAYPOINTS=true; else WAYPOINTS=false; fi ;;
               routes) if [ $ROUTES = false ]; then ROUTES=true; else ROUTES=false; fi ;;
               tracks) if [ $TRACKS = false ]; then TRACKS=true; else TRACKS=false; fi ;;
					DATADIR)
				      $DIALOG --title "INPUT BOX" --clear --inputbox \
                  "e.g. $HOME" 16 51 $GPSDATADIR 2> $tempfile
                  GPSDATADIR=`cat $tempfile`
					;;
  					file)
				      $DIALOG --title "INPUT BOX" --clear --inputbox \
                  "e.g. $HOME" 16 51 $FILE 2> $tempfile
                  FILE=`cat $tempfile`
					;;
				 back) DOWNLOADMENU=false; ;;
             Xit) exit 0; ;;
            esac
       done
      ;;
      upload) ########################################################################### upload
         UPLOADMENU=true;
			FILE=$(date  +%y-%m-%d_somegpsdata.gpd);
         while  $UPLOADMENU ; do
				UPLOAD=true;
            $DIALOG --title "mgpspoint - UPload" --clear --menu "" 15 50 10 \
               "continue" ""\
               "waypoints" "$WAYPOINTS"  \
               "routes" "$ROUTES"  \
               "tracks" "$TRACKS"  \
					"DATADIR" "$GPSDATADIR" \
					"file" "$FILE" \
					"back" "" \
               "Xit" "" \
               2> $tempfile;
            CHOICE=$(cat $tempfile);
            case $CHOICE in
					continue)
						INPUTFILE="$GPSDATADIR/$FILE";
						if [ ! -e $INPUTFILE ]; then
							$DIALOG --msgbox "$FILE does exist; choose an other" 10 40;
							UPLOAD=false;
						fi
						if $UPLOAD; then
							if  $WAYPOINTS ; then COMMAND=" $COMMAND --upload-waypoints " ; fi 
							if  $ROUTES ; then COMMAND=" $COMMAND --upload-routes " ; fi 
  							if  $TRACKS ; then COMMAND=" $COMMAND --upload-tracks " ; fi 
							$GPSPOINT -d $GPSPORT $COMMAND --input-file "$INPUTFILE";
							sleep 3;
							UPLOADMENU=false;
						fi
					;;
               waypoints) if [ $WAYPOINTS = false ]; 
						then WAYPOINTS=true; else WAYPOINTS=false; fi ;;
               routes) if [ $ROUTES = false ]; then ROUTES=true; else ROUTES=false; fi ;;
               tracks) if [ $TRACKS = false ]; then TRACKS=true; else TRACKS=false; fi ;;
					DATADIR)
				      $DIALOG --title "INPUT BOX" --clear --inputbox \
                  "e.g. $HOME" 16 51 $GPSDATADIR 2> $tempfile
                  GPSDATADIR=`cat $tempfile`
					;;
  					file)
				      $DIALOG --title "INPUT BOX" --clear --inputbox \
                  "e.g. $HOME" 16 51 $FILE 2> $tempfile
                  FILE=`cat $tempfile`
					;;
				 back) UPLOADMENU=false; ;;
             Xit) exit 0; ;;
            esac
       done
      ;;

		off) ######################################################
			$GPSPOINT --port $GPSPORT --off
		;;

      settings) ####################################################### settings
         QUITSETTINGS=0;
         while [ $QUITSETTINGS = 0 ]; do
            $DIALOG --clear --title "mgpspoint - settings" \
               --menu "choose:" 16 51 8 \
               "back" ""\
               "GPSPOINT" "$GPSPOINT"\
               "PORT" "$GPSPORT"\
               "EDITOR" "$EDITOR"\
					"DATADIR" "$GPSDATADIR"\
               2> $tempfile;
            QUITSETTINGS=$?
            CHOICE=`cat $tempfile`;
            case $CHOICE in
               GPSPOINT)
                  $DIALOG --title "INPUT BOX" --clear --inputbox \
                  "e.g. /dev/ttyS0" 16 51 $GPSPOINT 2> $tempfile
                  GPSPOINT=`cat $tempfile`
               ;;
               PORT)
                  $DIALOG --title "INPUT BOX" --clear --inputbox \
                  "e.g. /dev/ttyS0" 16 51 $GPSPORT 2> $tempfile
                  GPSPORT=`cat $tempfile`
               ;;
               EDITOR)
                  $DIALOG --title "INPUT BOX" --clear --inputbox \
                  "e.g. vim " 16 51 $EDITOR 2> $tempfile
                  EDITOR=`cat $tempfile`
               ;;
               DATADIR)
                  $DIALOG --title "INPUT BOX" --clear --inputbox \
                  "e.g. $HOME" 16 51 $GPSDATADIR 2> $tempfile
                  GPSDATADIR=`cat $tempfile`
               ;;
               back)
                  QUITSETTINGS=1;
               ;;
            esac
         done
      ;;
      Xit)
         exit 0;
      ;;
   esac
      

done

rm -rf $tempfile;
