#!/bin/bash

#-----------------------------------------------------------------------------
#
# author sudodus alias nio-wiklund at launchpad
#
# date        editor   comment
# 2013-06-25  sudodus  linux installer from tarball of installed system
# 2013-06-27  sudodus  raring.tar.gz
# 2013-08-09  sudodus  removed lame logic for wipe as alternative to install
# 2013-12-05  sudodus  select-partition created from select-device
#            
#-----------------------------------------------------------------------------

# main program

# variables

mkctrl "$system" "$tarball" "$rootpart" "$swappart"

choice=1
bs=4096
count=256
cnt=
target=
source="$1"
LC_ALL=C
LANG=C
tmpstr=
tiptxt="tips.txt"

version=$( grep -i 'obi version' change.log|tail -n1|tr -s ' ' ' '| \
       cut -d ' ' -f 5-6)

if test -f /home/$USER/mkhelp; then dpath=/home/$USER
elif test -f ./mkhelp; then dpath=.
else echo "Script not found. Change directory!"
fi

# usage

if [ "$(whoami)" != "root" ]
then
 echo "Usage:"
 echo "Install $system to a partition on an"
 echo "internal hard disk drive or external USB or eSATA drive"
 echo "---- Install $system --------"
 echo "sudo $0"
 exit
fi
if [ "$1" != "" ]
then
 echo ":-( Use no extra parameters :-("
 exit
fi

# select action

ans=$(dialog --backtitle "One Button Installer $version - Confirm partition" \
--yesno "Do you want to install $system?" 0 0 \
3>&1 1>&2 2>&3 3>&- )

if [ "$?" != "0" ]
then
 read -p "Press Enter to quit (return to the main menu)"
 exit
fi

while [ "$ans" != "g" ]
do
 chocnt=7
 choice=g
 read rootpart swappart < "$partitions"
 if [ "$rootpart" == "" ]
 then
  rootpart=$(sudo blkid|grep -i obi|grep -im1 root|cut -d: -f1)
  swappart=$(sudo blkid|grep -i obi|grep -im1 swap|cut -d: -f1)
  echo "$rootpart $swappart" > "$partitions"
 fi
 
 if [ "${tarball/noswap}" == "$tarball" ]
 then
  if [ "$swappart" != "" ]
  then
   swapshow="$swappart"
  else
   choice=s
   swapshow="\Z1\ZbNO SWAP\Zn"
  fi
  swaptxt1="and the swap partition  \Z1$swapshow\Zn\n\n"
  swapcmd2="s"
  swaptxt2=" Swap partition - select or change"
 else
  chocnt=$(($chocnt-1))
  swaptxt1=""
  swapcmd2="q"
  swaptxt2=" Quit from this menu (return to the starter menu)"
  swappart=""
  echo "$rootpart" > "$partitions"
 fi
 if [ "$rootpart" != "" ]
 then
  rootshow="$rootpart"
  gocmd="g"
  gotxt=" Go ahead - confirm the choice"
 else
  choice=r
  rootshow="\Z1\ZbNO ROOT\Zn"
  gocmd="r"
  gotxt=" Start selecting a Root partition"
 fi


ans=$(dialog --cr-wrap --no-collapse --no-shadow --default-item "$choice" \
--backtitle "One Button Installer $version - Confirm partition" \
--title "Please select an action with a letter key or the arrow keys" \
--colors --cancel-label Quit --menu \
"The One Button Installer is prepared to start installing\n
the operating system    \Z4$system\Zn\n
to the root partition   \Z1$rootshow\Zn\n
$swaptxt1
Change or confirm the partitions or quit" 19 76 "$chocnt" \
"$gocmd" "$gotxt" \
o " Overview" \
a " Autoselect - select root and swap partitions automatically" \
r " Root partition - select or change" \
"$swapcmd2" "$swaptxt2" \
t " Tips (howto prepare partitions with Gparted)" \
q " Quit from this menu (return to the starter menu)" \
  3>&1 1>&2 2>&3 3>&- )

 if [ "$ans" == "g" ]
 then
  echo $ans
 elif [ "$ans" == "o" ]
 then 
  "$dpath/select-part1"
 elif [ "$ans" == "a" ]
 then 
  "$dpath/autoselect"
 elif [ "$ans" == "r" ]
 then
  "$dpath/select-part2" root
 elif [ "$ans" == "s" ]
 then 
  "$dpath/select-part2" swap
 elif [ "$ans" == "t" ]
 then 
  dialog --backtitle 'One Button Installer - Tips (use arrow keys to scroll)' \
  --textbox "$tiptxt" 17 76
 elif [ "$ans" == "q" ]
 then 
  exit
 else
  exit
 fi
done

# here we go

# echo "$system" "$tarball" "$target" "$rootpart" "$swappart"
# read -p "Press enter to continue or ctrl C to quit"

 warning_dialog


 ans=$(dialog --cr-wrap --no-collapse --defaultno --colors \
--backtitle "One Button Installer $version - Confirm partition - final WARNING" \
--yesno "The One Button Installer is prepared to start installing\n\n
the operating system    \Z4$system\Zn\n
to the root partition   \Z1$rootpart\Zn\n
$swaptxt1
\Z1This is the \Z7 FINAL WARNING \Z1 before starting to install !!!\n
Do you really want to install and overwrite these partitions?\Zn" 13 76 \
 3>&1 1>&2 2>&3 3>&- )

 ans="$?"
 normal_dialog

 if [ "$ans" != "0" ]
 then
  exit
 fi

 cnt="count=$count"

# install

 echo " "
 echo "Installing $system to $rootpart and sometimes swap $swappart ..."
 echo " "
 echo "mklub $system $tarball $rootpart $swappart"
 read -sn1 -t3

mklub "$system" "$tarball" "$rootpart" "$swappart"

 read -p "Check the result and press Enter to continue"

