#!/bin/bash

TITLE="nanodesk-installer-gxm"
VERSION=$(cat /usr/share/nanodesk/version)

GXMCMD="gxmessage -name $TITLE -center -geometry 669x420 -wrap -font mono,10 -file -"

GXMINFO="gxmessage -name $TITLE -center -buttons Close -default Close -font mono,16 -file -"

DOCROOT="/usr/share/doc/nanodesk-installer"
DILLOCMD="dillo -f -g 640x480 $DOCROOT"

TARGET=""

ERRORFILE="/tmp/nanodesk-installer.error"
SUCCESSFILE="/tmp/nanodesk-installer.success"

# initialize RETURN with a high value
RETURN=255


#####
#
# DIALOGES
#
#####

gxm-abort() {

  if gxm-confirm
  then
    echo "Installation aborted!"| $GXMINFO
    exit 1
  fi
}

gxm-confirm() {
  echo "Are you sure you want abort?" | $GXMINFO -buttons "Yes:0,No:1" 
  return $?
}

gxm-cleanup() {
  echo "!!! An old Errorfile at $ERRORFILE was found !!!

It is necessary to delete it if you want to continue.
When deleting it, you will be asked for your password.

Delete $ERRORFILE ?" | $GXMINFO -buttons "Yes:0,No:1" -default Yes
  return $?
}

gxm-welcome() {
	echo "                   ----==== nanodesk-installer-gxm ====----
nanodesk ${VERSION}

This is a gxmessage based Installation assistant for nanodesk.

To install nanodesk you need a partitioned and formated drive.
You can do the partioning and formating with 'GParted'.

If you need help how to do so, click on 'Help'.

When you are done partitioning and your installation target is formated,
go ahead with 'Install'." |
	$GXMCMD -buttons "Abort:1,Help:2,GParted:10,Install:0"
	return $?
}

gxm-select-target() {
	echo "Please enter the target and press 'Install'.
For example: /dev/sda1" |
	$GXMCMD -entry -buttons "Abort:1,Help:2,Install:0" 
	return $?
}

gmx-start-cli-installer() {
  echo "The commandline nanodesk-installer will now open in a new xterm
window.

Target device: $1

Please follow the instructions there and see you later again..." |
  $GXMCMD -buttons "Abort:1,open nanodesk-installer CLI:0" -default "open nanodesk-installer CLI"
  return $?
}

gxm-success() {
  echo "                   ----==== nanodesk-installer-gxm ====----
nanodesk ${VERSION}

Installation complete!

You can now reboot or continue using nanodesk live." | $GXMCMD -buttons Continue:0,Reboot:2 -default Continue
  return $?
}

gxm-error() {
  echo "                   ----==== nanodesk-installer-gxm ====----
nanodesk ${VERSION}

Installation FAILED!!

Last message was: $(cat $ERRORFILE)" | $GXMCMD -buttons Close -default Close
  return $?
}


#####
#
# HELP
#
#####

help-gparted() {
	
	echo "help-gparted" | $GXMCMD 
 }

help-select-target() {
	
	echo "help-gparted" | $GXMCMD 
 }


#####
#
# EXEC
#
#####

exec-gparted() {
	pkexec /usr/sbin/gparted
}

exec-nanodesk-installer() {
	# run the installer within xterm
  #xterm -e /bin/bash -c "pkexec /usr/sbin/nanodesk-installer $TARGET" 
  xterm -e /bin/bash -c "pkexec mc || echo 'pkexec auth failed' >> $ERRORFILE" 
  #xterm -e /bin/bash -c "pkexec 'false ' ; echo $? ; echo 'Please press enter to continue...' ; read"
}


#####
#
# MAIN
#
#####

# cleanup old errorfile
if [ -f "$ERRORFILE" ]
then
  RETURN=255
  while [ $RETURN -gt 0 ]
  do  
    gxm-cleanup
    RETURN=$?
    
    if [ $RETURN -gt 0 ]
    then
      gxm-abort
    else
      pkexec rm -f $ERRORFILE || RETURN=255
    fi
  done
fi

RETURN=255
# welcome screen
while [ $RETURN -gt 0 ]
do
	gxm-welcome
	RETURN=$?
	echo CODE $RETURN
	case $RETURN in
		1) gxm-abort ;;
		2) help-gparted ;;
		10) exec-gparted ;;
	esac
done

# target select
RETURN=255
while [ $RETURN -gt 0 ]
do
	TARGET=$(gxm-select-target)
	RETURN=$?
	echo CODE $RETURN
	echo TARGET $TARGET


	# if RC = 1, then user clicked "abort"
	case $RETURN in
		1) gxm-abort ;;
		2) help-select-target ;;
		0) echo test if blockdevice
	   	   if [ ! -b "$TARGET" ]
	   	   then
	   	   	RETURN=255
	   	   	echo no
			echo -e "Error!\n'${TARGET}' does not exist or is not a blockdevice!" | $GXMINFO
	   	   fi
		   ;;
	esac
done

# start cli nanodesk-installer
RETURN=255
while [ $RETURN -gt 0 ]
do
	gmx-start-cli-installer $TARGET
	RETURN=$?
  test $RETURN -eq 1 && gxm-abort
	echo CODE $RETURN
done

exec-nanodesk-installer

if [ -f "$SUCCESSFILE" ]
then
  gxm-success
  RETURN=$?
  
  case $RETURN in
    2) echo reboot ;;
    *) exit 0 ;;
  esac
elif [ -f "$ERRORFILE" ]
then
  gxm-error
  exit 1  
fi


#if [ $RETURN != 0 ]
#then
#	echo "ERROR!" | $GXMINFO
#	exit 1
#else
#	echo "SUCCESS!" | $GXMINFO
#	exit 0
#fi