#!/bin/bash

# Copy to USB Key Tool Copyright 2009,2011 by Tony Brijeski under the GPL V2
# Using yad for gui calls
#
# Remastered for SparkyLinux by Paweł "pavroo" Pijanowski <pavroo@onet.eu> 2014/06/05
# Copyright 2014-2023 Paweł Pijanowski under the GPL V2
# Last update 2023/03/25 by pavroo

# get default's locale file
DEFLOCDIR="/usr/share/pearl/pearl-live-usb-creator"
if [ "`cat /etc/default/locale | grep LANG= | grep ar`" != "" ]; then
. $DEFLOCDIR/ar
elif [ "`cat /etc/default/locale | grep LANG= | grep de`" != "" ]; then
. $DEFLOCDIR/de
elif [ "`cat /etc/default/locale | grep LANG= | grep el`" != "" ]; then
. $DEFLOCDIR/el
elif [ "`cat /etc/default/locale | grep LANG= | grep es`" != "" ]; then
	if [ "`cat /etc/default/locale | grep LANG= | grep es_AR`" != "" ]; then
. $DEFLOCDIR/es_AR
	else
. $DEFLOCDIR/es_ES
	fi
elif [ "`cat /etc/default/locale | grep LANG= | grep fr`" != "" ]; then
. $DEFLOCDIR/fr
elif [ "`cat /etc/default/locale | grep LANG= | grep hu`" != "" ]; then
. $DEFLOCDIR/hu
elif [ "`cat /etc/default/locale | grep LANG= | grep id_ID`" != "" ]; then
. $DEFLOCDIR/id_ID
elif [ "`cat /etc/default/locale | grep LANG= | grep it`" != "" ]; then
. $DEFLOCDIR/it
elif [ "`cat /etc/default/locale | grep LANG= | grep ja`" != "" ]; then
. $DEFLOCDIR/ja
elif [ "`cat /etc/default/locale | grep LANG= | grep pl`" != "" ]; then
. $DEFLOCDIR/pl
elif [ "`cat /etc/default/locale | grep LANG= | grep pt_BR`" != "" ]; then
. $DEFLOCDIR/pt_BR
elif [ "`cat /etc/default/locale | grep LANG= | grep pt_PT`" != "" ]; then
. $DEFLOCDIR/pt_PT
elif [ "`cat /etc/default/locale | grep LANG= | grep ru`" != "" ]; then
. $DEFLOCDIR/ru
elif [ "`cat /etc/default/locale | grep LANG= | grep sv`" != "" ]; then
. $DEFLOCDIR/se
elif [ "`cat /etc/default/locale | grep LANG= | grep uk`" != "" ]; then
. $DEFLOCDIR/uk
elif [ "`cat /etc/default/locale | grep LANG= | grep zh_CN`" != "" ]; then
. $DEFLOCDIR/zh_CN
else
. $DEFLOCDIR/en
fi

DIALOG="`which yad` --width 450"
DIALOGFIX="`which yad` --width 450 --fixed"
TITLE="--always-print-result --dialog-sep --window-icon=/usr/share/icons/pearl/persistent/pearl48.png --image=/usr/share/icons/pearl/persistent/pearl48.png --title="
TITLETEXT="$LOCAL1 Persistence"
TEXT="--text="
FILESELECTION="--file "
OKBUTTON="--button=Ok:0 "
DIALOGZEN="zenity --progress --pulsate --auto-close --auto-kill --no-cancel --window-icon=/usr/share/icons/pearl/persistent/pearl48.png --width=450 --height=150"
TITLEZEN="--title="
testroot="`whoami`"
if [ "$testroot" != "root" ]; then
	echo "must me root... exiting..."
	exit 1
fi
if [ -f /tmp/usb-creator/usb-creator-log.txt ]; then
	rm -fr /tmp/usb-creator
fi
mkdir -p /tmp/usb-creator
touch /tmp/usb-creator/usb-creator-log.txt
TMPSIZE=`df -h /tmp`
echo "TMPSIZE=$TMPSIZE" >> /tmp/usb-creator/usb-creator-log.txt
 
copymenu () {
if [ "$1" = "(null)" ]; then
	$DIALOG $TITLE"$TITLETEXT" $TEXT"\n$LOCAL2"
	mainmenu
fi

if [ "$2" = "(null)" ]; then
	$DIALOG $TITLE"$TITLETEXT" $TEXT"\n$LOCAL3"
	mainmenu
fi

$DIALOGFIX $TITLE"$TITLETEXT" $TEXT"\n$LOCAL4\n\n$LOCAL12 $FREESIZE MB\n$LOCAL13\n$LOCAL14"

if [ $? != 0 ]; then
	mainmenu
fi

### STEP1: recreate iso image adding persitence to live boot
if [ -d /tmp/redir ]; then
	rm -rf /tmp/redir
fi
mkdir -p /tmp/redir
cd /tmp/redir
$(LANG=C sed 's/quiet splash/persistence /;s/PearlLinux/Persistence/' $2 > pearllinux-persistent.iso) | $DIALOGZEN $TITLEZEN"$TITLETEXT" $TEXT"\n$LOCAL15"
sleep 1
if [ ! -f /tmp/redir/pearllinux-persistent.iso ]; then
	$DIALOG $TITLE"$TITLETEXT" $OKBUTTON $TEXT"\n$LOCAL16"
	exit 0
fi

### STEP2: copy new iso image to usb  
#umount `mount | grep $1 | awk '{print $1}'`
UMDEVS3=`mount | grep $1 | awk '{print $1}' | tail -n1 | grep 3`
if [ "$UMDEVS3" != "" ]; then
	umount "$UMDEVS3"
	sleep 1
fi
UMDEVS1=`mount | grep $1 | awk '{print $1}'`
umount "$UMDEVS1"
#do the copy
if [ ! -d /mnt/part1 ]; then
	mkdir -p /mnt/part1
fi
if [ ! -d /mnt/part3 ]; then
	mkdir -p /mnt/part3
fi
$(dd if=/tmp/redir/pearllinux-persistent.iso of=/dev/$1 bs=4M && sync) | $DIALOGZEN $TITLEZEN"$TITLETEXT" $TEXT"\n$LOCAL5\n"

MOUNT1=`fdisk -l /dev/$1 | tail -n2 | grep $1 | awk -F " " '{print $1}' | head -n1`
echo "MOUNT1=$MOUNT1" >> /tmp/usb-creator/usb-creator-log.txt
mount -t iso9660 "$MOUNT1" /mnt/part1

### STEP3: create 3rd persistent partition on usb
TRYMOUNT1=`cat /proc/mounts | grep /dev/$DEVS`
echo "TRYMOUNT1=$TRYMOUNT1" >> /tmp/usb-creator/usb-creator-log.txt
if [ "$TRYMOUNT1" = "" ]; then
	$DIALOG $OKBUTTON $TITLE"$TITLETEXT" $TEXT"\n$LOCAL23\n"
	sleep 3
	# mount usb again
	mount -t iso9660 "$MOUNT1" /mnt/part1
	TRYMOUNT2=`mount | grep $DEVS`
	echo "TRYMOUNT2=$TRYMOUNT2" >> /tmp/usb-creator/usb-creator-log.txt
	if [ "$TRYMOUNT2" = "" ]; then
		$DIALOG $OKBUTTON $TITLE"$TITLETEXT" $TEXT"\n$LOCAL20\n"
		exit 0
	fi
fi
# preper 3rd partition on USB
$(fdisk /dev/$DEVS <<<$'n\np\n\n\n\nw'
) | $DIALOGZEN $TITLEZEN"$TITLETEXT" $TEXT"\n$LOCAL21\n"
sleep 1
GET3PART=`blkid | grep /dev/$DEVS | grep 3: | awk -F " " '{print $1}' | cut -d ":" -f 1`
echo "GET3PART=$GET3PART" >> /tmp/usb-creator/usb-creator-log.txt
$(mkfs.ext4 -L persistence $GET3PART
mount $GET3PART /mnt/part3
echo '/ union' >/mnt/part3/persistence.conf
sync
umount /mnt/part3
umount /mnt/part1
) | $DIALOGZEN $TITLEZEN"$TITLETEXT" $TEXT"\n$LOCAL21\n"
rm -rf /tmp/redir
rm -rf /mnt/part3
rm -rf /mnt/part1
$DIALOG $TITLE"$TITLETEXT" $TEXT"\n$LOCAL6" $OKBUTTON
}

mainmenu () {
DEVS=""
DEVS=`ls -l /dev/disk/by-path/*usb* | grep -v "part" | awk '{print $NF}' | awk -F "/" '{print $NF}'`
echo "DEVS=$DEVS" >> /tmp/usb-creator/usb-creator-log.txt
if [ "$DEVS" != "" ]; then
	for i in $DEVS; do
		USBDRIVESIZE=`grep -m 1 "$i" /proc/partitions | awk '{print $3}'`
		USBDRIVES="$USBDRIVES!$i-$USBDRIVESIZE"
	done
else
	$DIALOG $TITLE"$TITLETEXT" $OKBUTTON $TEXT"\n$LOCAL7"
	exit 0
fi

CHOICES=`$DIALOGFIX $TITLE"$TITLETEXT" $TEXT"$LOCAL22\n -Pearl desktop: 2GB\n -Pearl Special Editions: 4GB\n$LOCAL14" --form --field="$LOCAL8:CB" $USBDRIVES --field="$LOCAL9:FL" --button="$LOCAL10:2" --button="$LOCAL11:1"`
retval="$?"

if [ "$retval" = "1" ]; then
	USBDRIVE=`echo $CHOICES | cut -d "|" -f 1 | cut -d "-" -f 1`
	PICKSOURCE=`echo $CHOICES | cut -d "|" -f 2`
	SOURCESIZE=`ls -s $PICKSOURCE | awk -F " " '{print $1}'`
	FREESIZEKB=`expr $USBDRIVESIZE - $SOURCESIZE`
	FREESIZE=`echo $FREESIZEKB | while read KB dummy;do echo $((KB/1024));done`
	copymenu $USBDRIVE $PICKSOURCE
	echo "USBDRIVE=$USBDRIVE" >> /tmp/usb-creator/usb-creator-log.txt
	echo "PICKSOURCE=$PICKSOURCE" >> /tmp/usb-creator/usb-creator-log.txt
	echo "SOURCESIZE=$SOURCESIZE" >> /tmp/usb-creator/usb-creator-log.txt
	echo "FREESIZEKB=$FREESIZEKB" >> /tmp/usb-creator/usb-creator-log.txt
	echo "FREESIZE=$FREESIZE" >> /tmp/usb-creator/usb-creator-log.txt
else
	exit 0
fi
}

mainmenu
