#!/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-2025 Paweł Pijanowski under the GPL V2
# Last update 2025/10/24 by pavroo

# get default's locale file
DEFLOCDIR="/usr/share/sparky/sparky-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 fi`" != "" ]; then
. $DEFLOCDIR/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/sparky/persistent/sparky48.png --image=/usr/share/icons/sparky/persistent/sparky48.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/sparky/persistent/sparky48.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$LOCAL13\n$LOCAL14"
#$LOCAL12 $FREESIZE MB\n
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/SparkyLinux/Persistence/' $2 > sparkylinux-persistent.iso) | $DIALOGZEN $TITLEZEN"$TITLETEXT" $TEXT"\n$LOCAL15"
sleep 1
if [ ! -f /tmp/redir/sparkylinux-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`
UMDEVS3=`mount | grep $1 | grep 3 | awk '{print $1}'`
if [ "$UMDEVS3" != "" ]; then
	umount "$UMDEVS3"
	sleep 1
fi
#UMDEVS1=`mount | grep $1 | awk '{print $1}'`
UMDEVS1=`mount | grep $1 | 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/sparkylinux-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`
#MOUNT1=`ls -l /dev/disk/by-path/*usb* | awk '{print $NF}' | awk -F "/" '{print $NF}' | grep 1`
#echo "MOUNT1=/dev/$MOUNT1" >> /tmp/usb-creator/usb-creator-log.txt
#mount -t iso9660 "/dev/$MOUNT1" /mnt/part1
#3mount -t iso9660 "/dev/sdb1" /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
sleep 1
umount /dev/$DEVS
umount "$UMDEVS1"
sleep 1
# check again and umount
UMDEV0=`mount | grep $DEVS | grep 3 | awk '{print $1}'`
if [ "$UMDEV0" != "" ]; then
	umount /dev/$DEVS
	umount "$UMDEVS1"
	sleep 1
fi
$(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" | grep -v "usbv" | awk '{print $NF}' | awk -F "/" '{print $NF}'`
DEVS=`ls -l /dev/disk/by-id/*usb* | grep -v "usbv" | grep -v "part" | 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 -Sparky desktop: 2GB\n -Sparky Special Editions: 4GB\n$LOCAL14\n\n$LOCAL24\n" --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`
	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
	#cho "FREESIZE=$FREESIZE" >> /tmp/usb-creator/usb-creator-log.txt
	NOSPARKY=`echo $PICKSOURCE | grep sparky`
	if [ "$NOSPARKY" = "" ]; then
		$DIALOG $OKBUTTON $TITLE"$TITLETEXT" $TEXT"\n$LOCAL25\n"
		exit 0
	fi
	OLDSPARKY2022=`echo $PICKSOURCE | grep sparkylinux-2022`
	OLDSPARKY60=`echo $PICKSOURCE | grep sparkylinux-6.0`
	OLDSPARKY61=`echo $PICKSOURCE | grep sparkylinux-6.1`
	OLDSPARKY62=`echo $PICKSOURCE | grep sparkylinux-6.2`
	OLDSPARKY63=`echo $PICKSOURCE | grep sparkylinux-6.3`
	OLDSPARKY64=`echo $PICKSOURCE | grep sparkylinux-6.4`
	OLDSPARKY65=`echo $PICKSOURCE | grep sparkylinux-6.5`
	OLDSPARKY5=`echo $PICKSOURCE | grep sparkylinux-5`
	OLDSPARKY4=`echo $PICKSOURCE | grep sparkylinux-4`

	if [ "$OLDSPARKY2022" != "" ]; then
		$DIALOG $OKBUTTON $TITLE"$TITLETEXT" $TEXT"\n$LOCAL26\n"
		echo "EXITISO=sparkylinux-2022" >> /tmp/usb-creator/usb-creator-log.txt
		exit 0
	elif [ "$OLDSPARKY60" != "" ]; then
		$DIALOG $OKBUTTON $TITLE"$TITLETEXT" $TEXT"\n$LOCAL26\n"
		echo "EXITISO=sparkylinux-6.0" >> /tmp/usb-creator/usb-creator-log.txt
		exit 0
	elif [ "$OLDSPARKY61" != "" ]; then
		$DIALOG $OKBUTTON $TITLE"$TITLETEXT" $TEXT"\n$LOCAL26\n"
		echo "EXITISO=sparkylinux-6.1" >> /tmp/usb-creator/usb-creator-log.txt
		exit 0
	elif [ "$OLDSPARKY62" != "" ]; then
		$DIALOG $OKBUTTON $TITLE"$TITLETEXT" $TEXT"\n$LOCAL26\n"
		echo "EXITISO=sparkylinux-6.2" >> /tmp/usb-creator/usb-creator-log.txt
		exit 0
	elif [ "$OLDSPARKY63" != "" ]; then
		$DIALOG $OKBUTTON $TITLE"$TITLETEXT" $TEXT"\n$LOCAL26\n"
		echo "EXITISO=sparkylinux-6.3" >> /tmp/usb-creator/usb-creator-log.txt
		exit 0
	elif [ "$OLDSPARKY64" != "" ]; then
		$DIALOG $OKBUTTON $TITLE"$TITLETEXT" $TEXT"\n$LOCAL26\n"
		echo "EXITISO=sparkylinux-6.4" >> /tmp/usb-creator/usb-creator-log.txt
		exit 0
	elif [ "$OLDSPARKY65" != "" ]; then
		$DIALOG $OKBUTTON $TITLE"$TITLETEXT" $TEXT"\n$LOCAL26\n"
		echo "EXITISO=sparkylinux-6.5" >> /tmp/usb-creator/usb-creator-log.txt
		exit 0
	elif [ "$OLDSPARKY5" != "" ]; then
		$DIALOG $OKBUTTON $TITLE"$TITLETEXT" $TEXT"\n$LOCAL26\n"
		echo "EXITISO=sparkylinux-5" >> /tmp/usb-creator/usb-creator-log.txt
		exit 0
	elif [ "$OLDSPARKY4" != "" ]; then
		$DIALOG $OKBUTTON $TITLE"$TITLETEXT" $TEXT"\n$LOCAL26\n"
		echo "EXITISO=sparkylinux-4" >> /tmp/usb-creator/usb-creator-log.txt
		exit 0
	else
		copymenu $USBDRIVE $PICKSOURCE
	fi
else
	exit 0
fi
}

mainmenu
