#!/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
# 2014-08-12  sudodus  version for OBI (select_device backported from mkusb)
#            
#-----------------------------------------------------------------------------

# functions and variables

#source functions 

mkctrl "$system" "$tarball"

# variables

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

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

# usage

if [ "$(whoami)" != "root" ]
then
 echo "Usage:"
 echo "Install $system to a mass storage device, typically 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

# greeting

ans=$(dialog --backtitle "$version - Select device" \
--yesno "Do you want to install $system?" 0 0 \
3>&1 1>&2 2>&3 3>&- )

if [ "$?" == "0" ]
then

# menus to select device and start installing

 select_device all

 cnt="count=$count"

# wipe the first mibibyte and install

 echo "dd if=/dev/zero bs=$bs $cnt of=$target"
#  exit  -------------------------------------------  skarpt läge

 dd if=/dev/zero bs="$bs" "$cnt" of="$target"
 echo " "
 echo "Installing $system to $target ..."
 echo " "
 echo "mklub $system $tarball $target"
 read -sn1 -t3

 mklub "$system" "$tarball" "$target"

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