#!/bin/sh

# This script is a tool for easy installing of blackPanther OS onto an USB device.
# Written by Charles Barcza * kbarcza@blackpanther.hu
# License : GPL

#
# 2008. 08. 27	Hungarian fdisk problem solved
#		LANG dependent license texts with case
# 2008. 08. 28.	Some small errorous mistypes corrected
# 2008. 08. 29	LANG dependent help texts with case
# 2008. 08. 30	Countdown routine
# 2008. 09. 10	approved partition recognizing routine
# 2008. 09. 10	corrections in the -d parameter
# 2008. 09. 12	EXTREMELY approved partition recognizing routines
# 		can work offline, if src and md5 already downloaded
#			Thanks to kisbetu

# TODO!
#
# doesn't recognize superfloppy mode (partitionless) pendrives
# !! bugous command line processing !!
# should check free space on partitions (mount-df-umount)
#		will do it later (k.b.)
# suggest sending unzip/untar/cp messages to /dev/null
#		cannot do it (k.b.)


SCRIPTNAME=`basename $0`
SCRIPTPATH=$0
SCRIPTDIR=${SCRIPTPATH%%$SCRIPTNAME}
cd $SCRIPTDIR
# for graphic mode, later..
#zenity --info --text "$0"

# uj valtozok:
#	$kapottdev	a -d parameterrel kapott eszkoznev
#	$penpart	FAT particio a pendrive-on ( /dev/sdc3 )
#	$pendev		a pendrive eszkozneve ( /dev/sdc )
#	$penmount	ahova pendrive-ot felmountoljuk ( /mnt/usbstick )
#	$netok		OK ha van net
#

debug=0
if [ "$debug" = "" ];then
    debug=0
fi

BINPATHS="/usr/sbin /usr/bin /usr/local/bin /usr/local/sbin /bin /sbin"

# messages
suappno="Sudo application not found! Please run again this program in terminal"
clickmsg="Hello! What is Click-Click good for? :)"
warnautomode="Click-Click works only in ${RED}automatic mode!!${DEF} (optional parameters not implemented yet in click mode)"
warnnoroot="	${RED}RUN THIS PROGRAM ONLY WITH ROOT PERMISSIONS, PLEASE!${DEF}"
noaccepted="	License not accepted ... Aborting installation and exit"


mutat="gprintf"
penpart=""
netok=""
penmount="/mnt/usbstick"
opt="-o umask=0,rw"
rel=/etc/blackPanther-release
srel=/etc/SuSE-release
src="blackPantherUSB_stick_OS(KDE).tar.bz2"
psrc="./"
md5="blackPantherUSB_stick_OS(KDE).md5"
BLK="\\033[1;40m"
RED="\\033[1;40m\\033[1;31m"
GRN="\\033[1;40m\\033[1;32m"
YEL="\\033[1;40m\\033[1;33m"
BLU="\\033[1;40m\\033[1;34m"
MAG="\\033[1;40m\\033[1;35m"
CYN="\\033[1;40m\\033[1;36m"
DEF="\\033[1;40m\\033[1;37m"

# SOUND EXTENSION, later...
SPKPATH="http://www2.ku.edu/~magyar/courses/103/unit1"
PLAYER="mplayer"

SAYOK="$PLAYER $SPKPATH/audio/jo.mp3"
SAYER=
SAYBY="$PLAYER $SPKPATH/audio/viszontlatasra.mp3"

OK=$(echo -e ${DEF}[ ${GRN}OK${DEF} ])
Error=$(echo -e ${DEF}[ ${RED}Error${DEF} ])
Warn=$(echo -e ${DEF}[ ${MAG}Warn${DEF} ])

echo -e "${BLK}"
clear
echo -e "${DEF}"


if [ "$debug" = "0" ];then
    hidewget=q
    progres="progres &"
    progress="progress &"
    ndbg="-n"
 else
    hidewget=
    progres=
    progress=
    space=$(echo "")
fi


usage() {
case "$LANG" in
	"hu_HU")
echo -e "
	Haszn�lat:

	$0 -d [Az�nPenEszk�ze] -v
	opci�k:
		-d	eszk�z (/dev/sda)
			particio (/dev/sda2), tobb FAT particio eseten
		-s	forr�s�tvonal k�zi megad�sa (alap keres�si �t: ./)
		-h	ez a k�perny�
		-v	besz�des �s hibakeres� m�d
		-x	grafikus m�d, alap�rtelmezett=auto (Ez m�g nem k�sz�lt el!)

	[ Futtat�si minta: $0 -d /dev/sda -s /tmp ]

"
	;;
	"hu_HU.UTF-8")
echo -e "
	Használat:

	$0 -d [AzÖnPenEszköze] -v
        opciók:
		-d	eszköz (/dev/sda)
			partíció (/dev/sda2), több FAT partíció esetén
		-s	forrásútvonal kézi megadása (alap keresési út: ./)
		-h	ez a képernyő
		-v	beszédes és hibakereső mód
		-x	grafikus mód, alapértelmezett=auto (Ez még nem készült el!)

	[ Futtatási minta: $0 -d /dev/sda -s /tmp ]

"
	;;
	*)
echo -e "
	Usage:

	$0 -d [device] -v
        options:
		-d	device (/dev/sda)
			partition (/dev/sda2), in case of more FAT partitions
		-s	source path manual (default find path ./)
		-h	this screen
		-v	verbose and debug mode
		-x	graphical mode, default=auto (NOT IMPLEMENTED YET!)

	[ Sample: $0 -d /dev/sda -s /tmp ]

"
	;;
esac


}

while [ "$#" != "0" ]
do
    case "$1" in
	"-?"|"-h"|"--help")
		header
		usage
		exit
	;;
	"-c"|"--click")
		shift
		click="1"
	;;
	"-d"|"--device")
		shift
		kapottdev="$1"
		shift
	;;
	"-s"|"--source")
		shift
		psrc="$1"
		shift
	;;
	"-t"|"--terms")
		terms
		exit
	;;
	"-v"|"--verbose")
		debug="1"
	;;
	*)
		usage
		exit
	;;
	esac
	shift
done

dialog() {
showapp=`which zenity || which kdialog`
#echo $showapp
}

sudo() {
suapp=`which kdesu || which gnomesu || which gsu`
#echo $suapp
if test ! -x "${suapp}"; then 
$showapp --error --text "Sudo application not found! Please run again this program in terminal"
exit
fi

}


term() {
if [ "$click" = "1" ];then
    echo ""
    echo "Hello! What is Click-Click good for? :)"
    sleep 2
#    clear
    echo ""
    echo -en "Click-Click works only in ${RED}automatic mode!!${DEF} (optional parameters not implemented yet in click mode)"
    sleep 4
else
    if [ "$0" != "./installtousb.sh" ];then 
	sudo
	findterm=`which konsole || which mrxvt || which xterm`

	if [ "$findterm" = `which konsole` ];then
	    defterm="$findterm --notabbar --nomenubar --noscrollbar --noclose --vt_sz 100x30 --schema Linux"
	elif [ "$findterm" = `which mrxvt` ];then
	    defterm="$findterm -tabTitle 'blackPanther USB installer..' -background 'black' -autohideTabbar -geometry 100x30"
	elif [ "$findterm" = `which xterm` ];then
	    defterm="$findterm -bg black -fg 'white' -geometry '100x30' -title 'blackPanther USB installer' -hold"
	fi

	$suapp "$defterm -e "$0" --click"
	exit
    fi
fi

}


terms() {
if [ $UID != "0" ];then
    echo ""
    echo -e "	${RED}RUN THIS PROGRAM ONLY WITH ROOT PERMISSIONS, PLEASE!${DEF}"
    echo ""
    sleep 4
    exit 1
fi

echo -e "${DEF}"

    case "$LANG" in
	"hu_HU")
	echo ''
	echo ''
	echo '	 Felhaszn�l�si felt�telek:'
	echo ''
	echo ''
	echo ''
	echo '	   Ez a script egy seg�dprogram a blackPanther OS USB v�ltozat�nak telep�t�s�hez.'
	echo '	   A haszn�lat�b�l ered� k�zvetett illetve, k�zvetlen vagy b�rmilyen egy�b k�rok�rt'
	echo '	   felel�ss�get nem v�llal sem a fejleszt� sem a blackPanther Europe.'
	echo ''
	echo '	   Kiz�r�lag saj�t felel�ss�gre haszn�lhat� fel! A fenti figyemeztet�st elolvastam �s'
	echo '	   elfogadom a felhaszn�l�si felt�teleket.'
	echo ''
	echo '	   Ha futtatni akarja a scriptet �rja be "y" �s nyomja meg az [entert]. Minden egy�b'
	echo '	   esetben, nyomjon meg egy m�sik billenty�t vagy a CTRL+C gombokat.'
	echo ''
	accept="	Elfogadja a felhaszn�l�si felt�teleket ? y=igen n=nem (y/n)"
	;;
	"hu_HU.UTF-8")
	echo ''
	echo ''
	echo '		Felhasználási feltételek:'
	echo ''
	echo ''
	echo '		Ez a script egy segédprogram a blackPanther OS USB változatának '
	echo '		telepítéséhez. A használatából eredő közvetett, illetve közvetlen '
	echo '		vagy bármilyen egyéb károkért felelősséget nem vállal sem a fejlesztő '
	echo '		sem a blackPanther Europe.'
	echo ''
	echo '		Kizárólag saját felelősségre használható fel!'
	echo ''
	echo '		A fenti figyemeztetést elolvastam és elfogadom a felhasználási feltételeket.'
	echo ''
	echo '		Ha futtatni akarja a scriptet, írja be "y" és nyomja meg az [entert]. Minden egyéb'
	echo '		esetben nyomjon meg egy másik billentyűt vagy a CTRL+C gombokat.'
	echo ''
    	accept="		Elfogadja a felhasználási feltételeket ? y=igen n=nem (y/n)"
	;;
	*)
	echo ''
	echo ''
	echo '		Conditions of Usage:'
	echo ''
	echo '		This is a tool for installing blackPanther OS USB version. You use it without'
	echo '		any warranty about the correct work of this application. Got me?'
	echo ''
	echo '		If you accept these conditions please write "y" and press to [enter]. Any other'
	echo '		press CTRL+C or "n" and [enter] to exit this application...'
	echo ''
	accept="		Do you accept the terms and conditions of this license agreement? y=yes n=no (y/n)"
	;;
	esac

    echo ""
    echo ""
    ANSWER=""
    while [ -z "${ANSWER}" ] ; do
      #"${PRINTF}" "Do you accept the terms and conditions of this license agreement? (yes/no) "
      echo -en "$accept : "
      read ANSWER
      UCANSWER=`echo "${ANSWER}" | tr '[a-z]' '[A-Z]'`
      case "${UCANSWER}" in
      "YES"|"Y")
        ;;
      "NO"|"N")
        echo ""
        echo ""
        echo -e "		${RED}License not accepted ... ${CYN}Aborting installation and exit${DEF}


"
        echo ""
        exit 1
        ;;
      *)
        ANSWER=""
        ;;
      esac
    done
##clear
header

}


chkgrb() {
for grb in ${BINPATHS}; do
    if test -f "${grb}/grub"; then 
	if [ "$debug" = "1" ];then
	    echo ""
	    echo "      Grub found : $grb/grub     $OK"
	 else
	    echo "$OK"
	    #$SAYOK 2&>/dev/null
	fi
     else
	if [ "$debug" = "1" ];then
        echo "      Grub find and check $grb..."
	fi
    fi
 done
 
 if [ ! -x "$grb" ];then
    echo "$Error"
    echo ""
    echo "The Grub is not installed on your system!"
    echo "Please install it or boot blackPanther Live CD and run this script again..."
    echo ""
    exit
 fi
}


bpheader() {
if [ -f "$rel" ];then 
    . /etc/rc.d/init.d/functions
    PRODUCT=`sed "s/.*release \([0-9.]*\).*/\1/g" /etc/blackPanther-release 2> /dev/null`
    SYSTEM=${SYSTEM="blackPanther OS"}
    SYSN1=" black"
    SYSN2="Panther OS "
    KIIR="`echo -en '\\033[1;40m''\\033[1;30m'${HIOF}`$SYSN1`echo -en '\\033[0;39m'`"
    KIIR2="`echo -en '\\033[1;40m''\\033[1;34m'`$SYSN2`echo -en '\\033[0;39m'`"
    SYSTEM="${KIIR}${KIIR2}"
    echo ""
    $mutat "\t   Yes! Dear My Friend Your running system is a %s""`echo -e`$SYSTEM `echo -en '\\033[0;31m'v`$PRODUCT"

    if [ ! -x /sbin/grub ];then
	echo "Grub is not installed, trying to install now..."
	wget -c ftp://ftp.blackpanther.hu/blackPanther/OS/All/v5.2.8\(Sea\)/ExtraRPMS/grub-0.97-20bP.i586.rpm
	rpm -ivh ./grub-0.97-20bP.i586.rpm
	rm -f ./grub-0.97-20bP.i586.rpm
    fi
 else 
  if [ -f "$srel" ];then

	# cat /etc/SuSE-release

	# openSUSE 11.0 (i586)
	# VERSION = 11.0

	# SUSE LINUX 10.0 (i586)
	# VERSION = 10.0

    VERSION=`cat $srel | grep VERSION | awk '{ print $3 }'`
    PRODUCT=`cat $srel | grep 86 | awk '{ print $1 }'`
    if [ $PRODUCT = "SUSE" ];then
	PRODUCT="SUSE LINUX"
    fi
#    VERSION=`cat $srel | grep VERSION | awk '{ print $3 }'`
    mutat="echo -en"
    echo ""
    $mutat "\t                   Your running system is ""`echo -e`${MAG}$PRODUCT${DEF}`echo -en ${DEF}" v."`${YEL}$VERSION${DEF}"
  else
    mutat="echo -en"
    SYSTEM="UNKNOWN"
    PRODUCT="version, NOT AVAILABLE or not DETECTED :("
    echo ""
    $mutat "\t     Your running system is a ""`echo -e`$SYSTEM `echo -en '\\033[0;31m'v`$PRODUCT"
  fi
fi
    $mutat "\n `echo -en  '	\\033[1;34m'`--------------------------------------------------------------------------------`echo -en '\\033[0;39m'`"
    echo ""
#    sleep 3
}


header() {
clear
echo -e "
			+---------------------------------------------------+  
			| blackPanther OS USB version pendrive install tool |
			+---------------------------------------------------+  
					${GRN}www.blackpanther.hu${DEF}"
bpheader
echo -e "${DEF}"
}


chknet() {
if [ -n "`ping -c 1 google.com | grep 'PING google.com'`" ]; then 
    echo "$OK"
    netok="OK"
    #$SAYOK 2&>/dev/null
else 
    echo "$Error"
#    echo "Check Your internet connection or settings. 
#    The ftp.blackpanther.hu is not available, install is now aborted, try again later.."
#    echo ""
#    exit
fi
}


progress() {
sleep "$tslp"
if [ "${COUNTER}" = "0" ]; then
    echo  -en "	[${YEL}|${DEF}"
fi 
if $(ps -C $lock --format args --noheaders | grep $lock > /dev/null 2>&1);then
    COUNTER=`expr ${COUNTER} + 1`
    if [ ${COUNTER} -eq 65 ]; then
        echo -ne "${YEL}|${DEF}"
        echo -e "]"
#        echo ""
        COUNTER="0"
    else
        echo -ne "${YEL}|${DEF}"
    fi
    progress
fi
}


tesped() {
case "${COUNTER}" in
    "0")
        echo ""
        ;;
    "1")
        echo  -en "	continue in ${YEL}"${COUNTER}"${DEF} sec  \r"
        sleep 1
        COUNTER=`expr ${COUNTER} - 1`
        tesped
        ;;
    *)
        echo  -en "	continue in ${YEL}"${COUNTER}"${DEF} secs \r"
        sleep 1
        COUNTER=`expr ${COUNTER} - 1`
        tesped
        ;;
esac
}


chksrc() {
if [ ! -f "${psrc}${src}" ];then
    echo -n " $Warn"
    sleep 1
    echo -en "   ${RED}++ WARN ++ ${YEL}Archive not found!${DEF} ${RED}++ WARN ++${DEF}  "
    echo ""
    sleep 1
    echo ""
    echo -e "	${GRN}Continue..${DEF}"
    echo ""
    sleep 4
    if [ "$netok"="OK" ];then
        header
        echo -ne "	${CYN}Do You want to download now blackPanther OS USB version ?${DEF} "
        sleep 2
        echo -e  "${GRN}Yes :)${DEF} Please Wait.." 
        lock=wget
        tslp="10"
        COUNTER="0"
        progress &
        wget -c$hidewget ftp://ftp.blackpanther.hu/blackPanther/OS/All/v5.2.8\(Sea\)/$src
        sleep 1
        echo  -n "]"
        echo "	Download done. Continue mission..."
        chmod 666 $src
        sleep 3
        if [ ! -f "${psrc}${src}" ];then
            echo -e "	${RED}Archive not found or download failed ${DEF}"
            exit 1
        else 
            echo "   $OK"
            sleep 2
        fi
    fi
else
    echo "$OK"
    echo ""
    echo -en "	Checking md5sum file...					"
    if [ ! -f "${psrc}${md5}" ];then
        echo "$error"
        echo -ne "	${CYN}Do You want to download ${md5} now?${DEF} "
        sleep 2
        echo -e  "${GRN}Yes :)${DEF} Please Wait.." 
        if [ "$netok"="OK" ];then
            lock="wget"
            tslp="0.2"
            COUNTER="0"
            progress &
            wget -c$hidewget ftp://ftp.blackpanther.hu/blackPanther/OS/All/v5.2.8\(Sea\)/$md5
            sleep 1
            echo  -n "]"
            echo ""
            if [ ! -f "${psrc}${md5}" ];then
                echo ""
                echo -e "	${RED}Md5sum file not found or download failed ${DEF}"
                exit 1
            else 
                echo "   $OK"
                sleep 2
            fi
        fi
    else
        echo "$OK"
    fi
    echo -e "	${GRN}Checking MD5SUM now,${DEF} please wait..."
    a1=`cat ${psrc}${md5} | awk '{ print $1 }'`
    lock="md5sum"
    tslp="0.1"
    COUNTER="0"
    progress &
    b1=`md5sum ${psrc}${src} | awk '{ print $1}'`
    sleep 1
    echo  -n "]"
##@ a1=$b1
    if [ "$a1" != "$b1" ];then
        echo ""
        echo ""
        echo "	**********************************************************"
        echo -e "	${RED} Archive or checksum is corrupted!${DEF}"
        echo -e "	${RED} Suggest to remove and download again..${DEF}"
        echo "	**********************************************************"
        echo -n "       "
        sleep 2
        exit 1
##        rm -f "${psrc}${src}"
##        chksrc
    else 
        echo -n $OK
    fi
##    rm -f "${psrc}${md5}"
fi
}


# megkeres minden FAT particiot
findfat() {
    COUNTER=0
    echo -en "	Searching for FAT partitions"
    fpart=""
    for b in `LANG=en_EN fdisk -l | grep FAT | awk '{print $1}'`
    do
        if [ -n "$b" ];then
            sleep 1
            echo ""
            COUNTER=`expr ${COUNTER} + 1`
            echo -en "		partition detected:${GRN} $b${DEF}"
            fpart=$b
        fi
    done
    sleep 1
    if [ "x$fpart" = "x" ]; then
        echo -e "	$Error"
        echo -e ""
        echo -e "	${RED}No suitable USB devices found :-(${DEF}"
        echo -e "	Please connect an USB stick to your PC and run ${YEL}$0 ${DEF}script again!"
        echo ""
        echo ""
        exit 1
    else
        echo -e ""
        echo -e ""
        echo -e "	${YEL}These are Your FAT partitions :-)${DEF}"
        echo -e "	${YEL}Please choose one and run ${GRN}$0 ${YEL}script again"
        echo -e "	with the proper -d parameter.${DEF}"
        echo -e ""
        echo -e ""
        echo -e ""
        echo -e ""
        exit 1
    fi
}


# -d-vel kapott disket ellenoriz
chkdisk() {
        echo -en "	Found disk: ${GRN}$kapottdev${DEF}					"
        sleep 1
        echo -e "$OK"
        sleep 1
        COUNTER=0
        penpart=""
        echo -en "	Searching for FAT partitions on disk ${GRN}$kapottdev${DEF}	"
        sleep 1
        for part in `LANG=en_EN fdisk -l "$kapottdev" | grep FAT | awk '{print $1}'`
        do
            if [ -n "$part" ];then
                echo ""
                COUNTER=`expr ${COUNTER} + 1`
                echo -en "		FAT partition detected:${GRN} $part${DEF}	"
                sleep 1
                fpart=$part
            fi
        done
        case "$COUNTER" in
            "0")
                sleep 1
                echo -e "	$Error"
                echo -e ""
                echo -e ""
                sleep 1
                findfat
            ;;
            "1")
                sleep 1
                penpart=$fpart
                echo -e "	$OK"
            ;;
            *)
                sleep 1
                echo -e "	$Error"
                sleep 1
                echo -e "							${RED}Too many FAT partitions found :-(${DEF}"
                echo -e ""
                echo -e ""
                sleep 1
                echo -e "	${YEL}Please choose one and run ${GRN}$0 ${YEL}script again"
                echo -e "	with the proper -d parameter.${DEF}"
                echo -e ""
                echo -e ""
                echo -e ""
                echo -e ""
                exit 1
            ;;
        esac
}


# -d-vel kapott particiot keres
chkpart() {
    echo -en "	Checking if ${GRN}$kapottdev${DEF} partition is FAT			"
    sleep 1
    aaa=""
    aaa=`fdisk -l  | grep FAT | grep "$kapottdev"`
    if [ -n "$aaa" ];then
        echo -e "$OK"
        penpart=$kapottdev
        pendev=`echo ${kapottdev:0:8}`
    else
        echo -e "$Error"
        echo ""
        sleep 1
        aaa=`fdisk -l  | grep "$kapottdev"| awk  '{print $6}'`
        echo -en "	${GRN}$kapottdev${DEF} partition is ${GRN}$aaa${DEF}"
        sleep 1
        echo ""
        echo ""
        findfat
##        exit 1
    fi
}


# -d-vel kapott eszkozt keres
chkpen() {
if [ "$kapottdev" = "" ]; then	#      nincs -d
    echo -en "	Checking -d parameter				"
    sleep 1
    echo -e "$Error"
    sleep 1
    echo -en "							${RED}parameter is missing${DEF}"
    echo ""
    echo ""
    findfat
fi
if [ -e $kapottdev ]; then	#      letezik az eszkoz
    echo -en "	Checking if device ${GRN}$kapottdev${DEF} exists			"
    sleep 1
    echo -e "$OK"
    bfdisk=`LANG=en_EN fdisk -l | grep Disk | grep "$kapottdev" | awk -F: '{ print $1 }' | awk -F/ '{ print $3 }'`
    if [ -n "$bfdisk" ];then 	#      ez egy disk
        chkdisk
    else			#      nem letezik ilyen disk, particiot kell keresni
        chkpart
    fi	
else				#      nem letezik a megadott eszkoz, keresunk egy FAT particiot
    echo -e "$Error"
    sleep 1
    echo -e "	Disk or partition ${GRN}$kapottdev${DEF} doesn't exist"
    echo ""
    echo ""
    findfat
fi
}


kibont() {
	echo "	* MKDIR $penmount" 
	mkdir -p $penmount
	sleep 2
	if [ -d $HOME/tmp/blackPantherUSBtmp ];then 
	    echo "	* Old tmp directory found, cleaning now.."
	    rm -rf $HOME/tmp/blackPantherUSBtmp
	fi
	echo "	* MKDIR Temporary in HOME"
	mkdir -p $HOME/tmp/blackPantherUSBtmp
	sleep 2
	echo "	* MOUNT ${penpart} here: ${penmount}  with: ${opt}"
	umount -dlf ${penpart} 2>/dev/null
	sleep 2
	mount ${penpart} ${penmount} ${opt}
	sleep 5
	echo "	* UnCompressing the Arcive...please wait"
	tslp="4"
	lock="bunzip2"
	COUNTER="0"
	progress &
	bunzip2 -cdv ${psrc}${src} | tar -C $HOME/tmp/blackPantherUSBtmp -xvf-
	sleep 1
	echo -n "]"
	echo ""
	sleep 3
	header
	echo "	* UnCompressing Done! Copying files to USB....Wait"
	echo ""
}


masol() {
	mkdir -p $penmount/boot
	mkdir -p $penmount/isolinux
	cp -rfv $HOME/tmp/blackPantherUSBtmp/boot/* $penmount/boot
	cp -rfv $HOME/tmp/blackPantherUSBtmp/isolinux/* $penmount/isolinux
	cp -rfv $HOME/tmp/blackPantherUSBtmp/blackPanther* $penmount/
	echo "	
	Syncing ..."
	sync

	#exit #test!

	COUNTER="10"
	tesped
	header
}


masol1() {
	echo -e "	Copying large filesystem. This will take a long time! Please Wait.."
	tslp="10"
	lock="cp"
	COUNTER="0"
	progress &
	echo  -en "	[${YEL}|${DEF}"
	cp -rfv $HOME/tmp/blackPantherUSBtmp/livecd.sqfs $penmount/
	sleep 1
	echo -n "]"
	echo ""
	echo "	Copy Done.."
}


insgrub() {
# installing GRUB
echo "(hd0) $pendev">$penmount/boot/grub/device.map
if [ -d /lib/grub/i386-blackpanther ];then 
    echo "
    Installed blackPantherOS detected.."
    cp /lib/grub/i386-blackpanther/* /mnt/usbstick/boot/grub
    grub-install --root-directory=/mnt/usbstick hd0
else
    echo -en "	Checking Grub loader install script...			"
    sleep 1
    if [ -f $penmount/boot/grub/install.sh ];then 
        echo -e "$OK"
        echo "	Installing Grub loader..."
#FAT!	    chmod 755 $penmount/boot/grub/install.sh
        perl -pi -e "s,//sbin/grub,grub," $penmount/boot/grub/install.sh
        sh $penmount/boot/grub/install.sh
        sync
    else
        echo -e "	Error: Install script not found or frozen USB port ?!"
        echo -e "	Please reboot system and try again...."
        rm -rf $HOME/tmp/blackPantherUSBtmp
        exit 1
    fi
fi
sync
sleep 2
}


byebye() {
# Not implemented yet
if [ $? = 1 ];then
echo "Process checking and killing... Exit"
else
echo -------------------------
fi

#$SAYBY
}



dialog
#sudo
term
header
sleep 1
terms
sleep 2

chkpen

echo ""
echo -n "	Checking Internet Connection				"
sleep 1
chknet
echo $ndbg "	Checking Grub bootloader				"
$space
sleep 1
chkgrb
echo -n "	Checking Source Package					"
sleep 1
chksrc
#exit #test!
#sleep 4
echo ""
COUNTER=4
tesped

header


echo ""
echo -e "	${RED}  WARNING! PLEASE CHECK IF VALUES ARE CORRECT! WARNING!${DEF}"
echo "	*********************************************************"
echo ""
echo -e "         Pendrive is: ${GRN}${penpart}${DEF}. Is it correct?? "
echo ""
echo -e "  	   Please check! If "$penpart" is incorrect press ${YEL}ctrl+c${DEF}"
echo "	**********************************************************"

COUNTER=15
tesped
header

kibont
masol

header

insgrub
masol1

sleep 3
echo "	Cleaning temporary files.."
rm -rf $HOME/tmp/blackPantherUSBtmp
sleep 2
echo ""
echo -e "	${RED}Tadadadaaa...USB Install Done...Try usbstick${DEF}"
echo ""
echo ""
cd /
umount ${penpart}
exit 0


