#!/bin/bash

# CopyCat is free software: you can redistribute it and/or modify
# it under the terms of WTFP Public License This program is distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY.

PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin:/usr/local/bin:/usr/local/sbin
# Added for translation purposes
# i18n - Internationalisation - Internationalisierung
VERSION=2.0.3
export TEXTDOMAIN=copycat_$VERSION
export TEXTDOMAINDIR="/usr/share/locale"
# variable for the gui
ICON="/usr/share/icons/hicolor/48x48/apps/copycat.svg"
EICON="/usr/share/copycat/icons/error.svg"
WICON="/usr/share/copycat/icons/warning.svg"
CANCEL="/usr/share/copycat/icons/cancel.svg"
OK="/usr/share/copycat/icons/ok.svg"
TITLE="CopyCat"
#set -x
run_as_root() {
#-----------------------------------------------
# run as root
#-----------------------------------------------
PID=`ps -eaf | grep yad | grep -v grep | awk '{print $2}'`
if [[ "" !=  "$PID" ]]; then
  echo "killing $PID"
  kill -9 $PID > /dev/null 2>&1 &
fi
if [ `id -u` -eq 0 ]; then
exec $@
else
`yad --title="CopyCat" --width=400 \
--height=100 --fixed --image=$EICON \
--window-icon=$EICON --center \
--text=$"\n\You need to be root to run CopyCat!" \
--button=Ok!$OK:"1"`
if [ "$?" = "1" ] ; then
exit 1
 fi
fi
rm -f /tmp/copycat >/dev/null 2>&1

NVME=$(grep -o nvme /etc/mtab| awk 'NR==1{print $0}')
if [[ $NVME = "$NVME" ]]
then
lsblk | grep -o sd[a-z] | tail -n1 |awk '$0="/dev/"$0' > /tmp/copycat
sleep 0.50
USBdevice=$(grep -o sd[a-z] /proc/partitions |tail -n1)
lsblk -do VENDOR,MODEL,SIZE /dev/$USBdevice |sed -e 's/ \+/ /g'| awk 'NR == 2' >> /tmp/copycat
else
lsblk | grep -o sd[b-z] | tail -n1 |awk '$0="/dev/"$0' > /tmp/copycat
sleep 0.50
USBdevice=$(grep -o sd[a-z] /proc/partitions |tail -n1)
lsblk -do VENDOR,MODEL,SIZE /dev/$USBdevive |sed -e 's/ \+/ /g'| awk 'NR == 2' >> /tmp/copycat
fi
if [ -s "/tmp/copycat" ]; then
exec $@
else
USERS=$(who |sed 's/ .*//'|head -n1)
su $USERS -c /usr/share/copycat/info_notify.py 2>/dev/null
pkill -f /usr/bin/copycat >/dev/null 2>&1
sleep 1
pkexec /usr/bin/copycat 2>/dev/null
exit 1
  fi
# Check if the user remouted the usb key
CHMOUNT=$(grep -o "0B" /tmp/copycat)
if [ "$CHMOUNT" = "" ] ; then
exec $@
else
`yad --title="CopyCat" --width=450 --height=100 --fixed --center --image=$WICON \
--window-icon=$WICON --center \
--text=$"\n\Remount the USB Device so it can be detected?\n<span color=\"#e07b39\">Continue only when the USB Device has been Inserted.</span>" \
--button=Cancel!$CANCEL:"1"`
if [ $? = 1 ] ; then
pkill -SIGKILL persistance >/dev/null 2>&1
exit
   fi
    fi
}
#-----------------------------------------------------------------------------
set_variables() {
DRIVES=""                           # The drive after the USB is plugged in
Part_Num=""                         # The partition number
ISO_FILE=""                         # The selected ISO file for the installation
JOB=""                              # Used to get ADD and LIVE variables from user
ADD=""                              # This is true if an additional OS is being installed
MPnt="/media/root/CopyCat"          # The mount point for the target partition (ext4)
NAME=""                             # The chosen name for the OS to be installed
SIZE1=""                            # The free space on the selected partition
SIZE2=""                            # The space required for the selected OS
Install_To=""                       # The partition selected for the installation
Type=""                             # Check the FS to ensure it is usable for the install
isolin_full=""                      # The full path and filename of the isolinx or renamed directory
PID=$$                              # Allows to kill the script on error.
sqfs_dir=""                         # The path only of the sqfs file
sqfs_full=""                        # Full path and name of the sqfs file
sqfs_name=""                        # The name only of the sqfs file ie. livecd
efi_Part=""                         # The EFI partition base name e.g  /dev/sdk1
efi_Mnt="/media/root/CopyCat.$RANDOM"    # The mount point for the EFI partition.
}
#-----------------------------------------------------------------------------------
if [ ! -d "/media/root/CopyCat" 2>/dev/null ]; then
mkdir -p "/media/root/CopyCat"
fi
create_mounts() {
mkdir $efi_Mnt  # Create the directory for the EFI mount
}
get_device() {
# Get usb mounted device
usbinfo=$(cat /tmp/copycat | awk 'NR == 2')
usbdevice=$(cat /tmp/copycat | awk 'NR == 1')
DRIVES="$usbdevice"
Install_To="$usbdevice"2""
FMT_BOOT=$(yad --window-icon=$ICON --center --title="$TITLE" \
--width=500 --height=90 --fixed --image=$ICON \
--entry \
--column=$"" \
--text="" \
$"Install  iso image to $usbinfo (  $usbdevice  )" \
--button=Cancel!$CANCEL:"1" \
--button=Ok!$OK:"0")
if [ $? = "1" ]; then
umount /media/*/*
rm -rf /media/root/* > /dev/null 2>&1
rm -f /tmp/copycat >/dev/null 2>&1
pkill -SIGKILL persistance >/dev/null 2>&1
fi |
if [ $? = "3" ]; then
$(yad --width=375 --height=100 --fixed --center \
--window-icon=/usr/share/copycat/icons/info-help.svg \
--image=/usr/share/copycat/icons/help.png \
--title='Copycat' \
--button=Close!$CANCEL)
umount /media/root/*
rm -rf /media/root/* > /dev/null 2>&1
rm -f /tmp/copycat >/dev/null 2>&1
pkill -SIGKILL persistance >/dev/null 2>&1
else
if [ -n "${FMT_BOOT}" ]; then
case $FMT_BOOT in
"Install  iso image to $usbinfo (  $usbdevice  )")
;;
esac
fi
  fi
usbinfo=$(cat /tmp/copycat | awk 'NR == 2')
usbdevice=$(cat /tmp/copycat | awk 'NR == 1')
install_usb="$usbdevice"
if [ "$DRIVES" = "$DRIVES" ]; then
Install_To="$usbdevice"2""
echo "$Install_To"
else
yad --width=375 --height=100 --fixed --center \
--text=$"\nNo usb key mounted on system!\nMount the usb key to continue setup" \
--window-icon=$EICON \
--image=$EICON \
--title='Error' \
--button=Close!$CANCEL:1
umount -q $MPnt
rm -rf $MPnt.*
pkill -SIGKILL persistance > /dev/null 2>&1
exit 0
 fi
usbdevice=$(cat /tmp/copycat | awk 'NR == 1')
sleep 1
Install_To="$usbdevice"2""
echo "$Install_To"
Install_To=$(echo $Install_To | sed 's/[|].*//'); echo $Install_To
umount -q $Install_To
Part_Num=$(echo "$Install_To" | cut -c9-10 )
}
get_device_type() {
FDISK_GET=$(dmesg | tail -8 | grep -i "Attached SCSI " | grep -i " disk" | grep -i sd[a-z] | sed 's|^.*\[||' | sed 's|\].*||' | sed 's|s|/dev/s|')
if [ $FDISK_GET = "" 2>/dev/null ]; then
FDISK_GET="$install_usb"
fi
DOS=""
GPT=""
DOS=$(fdisk -l $FDISK_GET | grep "dos" | sed 's| ||g'| cut -d : -f2)
GPT=$(fdisk -l $FDISK_GET | grep "gpt" | sed 's| ||g'| cut -d : -f2)
}
get_uuid() {
if [ "$DRIVES" = "$DRIVES" ]; then
exec
else
yad --width=375 --height=100 --fixed --center \
--text=$"\nInstallation Setup Failed!\nReinsert the usb key to continue" \
--window-icon=$EICON \
--image=$EICON \
--title='Error' \
--button=Close!$CANCEL:1
umount -q $MPnt
rm -rf $MPnt.*
pkill -SIGKILL persistance > /dev/null 2>&1
exit 0
fi
# Get the UUID of partition
PART_UUID=$(lsblk -l -n -o UUID "$Install_To")
if [ "$PART_UUID" == "" ] ; then
exit 0
 fi
}
#------------------------------
# Write a label for the USB drive
write_label() {
if [ "$DRIVES" = "$DRIVES" ]; then
#------------------------------
LABEL="CopyCat"
echo ""
#------------------------------
else
yad --width=450 --height=100 --fixed --center \
--text=$"\nReinsert the key mounted on system!\nRemount the usb key to continue" \
--window-icon=$EICON \
--image=$EICON \
--title='Error' \
--button=Close!$CANCEL:1
umount -q $MPnt
rm -rf $MPnt.*
exit 0
 fi
}
get_iso() {
#------------------------------
# Get the iso image
#------------------------------
SELECTED=$(cat /tmp/selected | awk 'NR == 1')
iso_image=$(grep -E .iso /tmp/selected| awk '{print $NF}' FS=/)
USER=$(who |sed 's/ .*//'|head -n1)
ISO_FILE=$(yad --file --filename=/home/$USER --center --file-filter="*.iso" \
--window-icon=$ICON \
--image=$ICON --height=550 \
--width=725 --on-top --title="CopyCat" \
--text=$"<b>Select <span color=\"#e07b39\">$iso_image</span> to Install with $usbinfo $usbdevice</b>" \
--button=Cancel!$CANCEL:2 \
--button=Select!$OK)
if [[ $ISO_FILE == $SELECTED ]]
then
echo $ISO_FILE
mkdir -p /tmp/iso
mount $ISO_FILE /tmp/iso -o loop
else
TMP_FILE=$(cat /tmp/selected)
yad --window-icon=$ICON --width=500 --height=125 --fixed --center \
--image=$EICON \
--title=$"CopyCat" --text=$"
<b>Check if usb device has been properly formatted.</b>
Check if the usb device is properly mounted.
Check the name of the iso in the selected path.
<span color=\"#e07b39\">$iso_image</span>
Restart the program if you want to continue." \
--button=Cancel!$CANCEL:1
umount -q $MPnt
rm -rf $MPnt.*
pkill -SIGKILL persistance > /dev/null 2>&1
exit 0
  fi
if [ $? = "2" ]; then
yad --window-icon=$EICON --width=425 --height=125 --fixed --center \
--image=$EICON \
--title=$"CopyCat" --text=$"
     <b>No File Selected or Available</b>!

 Restart the program if you want to continue" \
--button=Cancel!$CANCEL:5
if [ $? = "5" ]; then
umount -q $MPnt
rm -rf $MPnt.*
pkill -SIGKILL persistance > /dev/null 2>&1
exit 0
fi
 fi
}
mount_part() {
mount $Install_To $MPnt
if [ "$?" != "0" ] ; then
yad --window-icon=$EICON --width=450 --height=125 --fixed --center \
--image=$EICON \
--title=$"Error" --text=$"
     <b>A Mounting Error has Occured</b>

     Seems the USB key has not been mounted properly?

         Please check about this mounting error." \
--button=Cancel!$CANCEL:1
if [ $? = 1 ]; then
umount -q $MPnt
rm -rf $MPnt.*
pkill -SIGKILL persistance > /dev/null 2>&1
exit 0
fi
 fi
}
get_name() {
while [[ ! "$NAME" =~ [a-zA-Z0-9] ]]; do
NAME=$(yad --window-icon=$ICON --center --list --height=400 --width=525 --title="$TITLE" --text=$"
<b>Select a Debian $OSNAME Distro Name</b>
<span color=\"#e07b39\">Highlight a name and select <b>OK</b> to accept it</span>" --editable --column $"- Suggested Names -
Highlight entry to edit name. Press Enter twice to apply your edit.
Custom edits must not be left blank or with spaces." "CINNAMON" "GNOME" "PLASMA" "MATE" "XFCE" "LXDE" "LXQT" \
--button=Cancel!$CANCEL:1 \
--button=OK!$OK:0)
if [ "$?" = "0" ] ; then
echo "$NAME"
fi
# Replace spaces with _    Leading spaces are omitted.
NAME=$(echo "$NAME" | sed 's/[|].*//')
if [ "$?" != "0" ] ; then
yad --window-icon=$EICON --height=150 --center --width=500 \
--image=$EICON --title=$"Error" --text=$"
                         <b>CANCEL</b>

     You have chosen to cancel this application.

        We will now abort the installation.

  Thank you for trying CopyCat" \
--button=Cancel!$CANCEL:1
if [ "$?" = "1" ] ; then
umount -q $MPnt
rm -rf $MPnt.*
exit 0
fi
 fi
if [ "$NAME" = "" ]
then
yad --window-icon=$EICON --center --width=400 --height=125 --fixed \
--image=$EICON --title=$"Error" --text=$"
		<b>An Error has Occured</b>

  You failed to correctly enter or select a name for the OS.

                      Please try again." \
--button=Quit!$CANCEL:1 \
--button=OK!$OK:0
if [ "$?" = "1" ] ; then
umount -q $MPnt
rm -rf $MPnt.*
pkill -SIGKILL persistance >/dev/null 2>&1
exit 0
fi
else
if [ "$ADD" = "yes" ]
then
if [ -d $MPnt/$NAME ]
then
yad --window-icon=$EICON --center --width=450 \
--image=$EICON --title="$TITLE" --text=$"
		<b>An Error has Occured</b>

     You have selected an existing name for the OS.

                      Please try again." \
--button=Quit!$CANCEL:1 \
--button=OK!$OK:0
if [ "$?" = "0" ] ; then
NAME=""
else
umount -q $MPnt
rm -rf $MPnt.*
pkill -SIGKILL persistance > /dev/null
exit 0
fi
fi
 fi
  fi
done
 }
get_size() {
# Check we have sufficient space
SIZE1=$(df -B M --output=avail $MPnt | sed '1d' | cut -d 'M' -f1)
SIZE2=$(du -B M $ISO_FILE | cut -d 'M' -f1)
SIZE2=$(($SIZE2+10))
if [ $SIZE2 -gt $SIZE1 ]; then
unmount_part
yad --window-icon=$EICON --width=475 --height=125 --fixed --on-top --center \
--image=$EICON \
--title=$"Error" --text=$"
	          <b>A Fatal Error has Occured</b>

There is Insufficient Room for the Installation.

                      This application will now abort!" \
--button=Close!$CANCEL:1
if [ $? = 1 ]; then
umount -q $MPnt
rm -rf $MPnt.*
pkill -SIGKILL persistance > /dev/null
exit 0
fi
 fi
}
get_size2() {
#-----------------------------------------------
# Check have sufficient space for Extracted Files
#------------------------------------------------
SIZE1=$(df -B M --output=avail $MPnt | sed '1d' | cut -d 'M' -f1)
SIZEA=$(du -B M -s "$sqfs_full" | cut -d 'M' -f1)
if [ "$?" != "0" ] ; then
yad --window-icon=$EICON --width=475 --height=125 --fixed --on-top \
--center --image=$EICON \
--title=$"Error" --text=$"
	          <b>Cannot find your files</b>

       An ERROR has occurred so aborting operation.

          Please give feedback about this error.

                            Thank you!" \
--button=Close!$CANCEL:1
if [ $? = 1 ]; then
umount -q $MPnt
rm -rf $MPnt.*
pkill -SIGKILL persistance > /dev/null
exit 0
fi
  fi
SIZEB=$(du -B M -s "$isolin_full" | cut -d 'M' -f1)
if [ "$?" != "0" ] ; then
yad --window-icon=$EICON --width=475 --height=125 --fixed --on-top \
--center --image=$EICON \
--title=$"Error" --text=$"
	          <b>Cannot find your files</b>

       An ERROR has occurred so aborting operation.

          Please give feedback about this error.

                            Thank you!" \
--button=Close!$CANCEL:1
if [ $? = 1 ]; then
umount -q $MPnt
rm -rf $MPnt.*
pkill -SIGKILL persistance >/dev/null 2>&1
exit 0
fi
 fi
SIZE2=$(($SIZEA+$SIZEB+10))
if [ $SIZE2 -gt $SIZE1 ]; then
unmount_part
yad --window-icon=$EICON --width=475 --height=125 --fixed --on-top \
--center --image=$EICON \
--title=$"Error" --text=$"
	          <b>An Fatal Error has Occured</b>

There is Insufficient Room for the Installation to Proceed.
You need either a larger device or else choose a smaller
OS to fit on this device.
            This application will now abort!" \
--button=Close!$CANCEL:1
if [ $? = 1 ]; then
umount -q $MPnt
rm -rf $MPnt.*
pkill -SIGKILL persistance >/dev/null 2>&1
exit 0
fi
 fi
}
wipe_part() {
rm -rf $MPnt.*
efi_wipe=$(fdisk -l $FDISK_GET -o Device,Type | grep EFI | tr -s ' ' | cut -d ' ' -f1)
if [ "$efi_wipe" != "" ]; then
rm -rf $MPnt.*
fi
}
copy_iso_files() {
if [ "$?" != "0" ] ; then
yad --window-icon=$EICON --width=475 --height=125 --fixed \
--on-top --center --image=$EICON \
--title=$"Error" --text=$"
	          <b>An Error has Occured</b>

       Unable to create a directory on your partition.

           Please give feedback about this error.


                             Thank you!" \
--button=Close!$CANCEL:1
if [ $? = 1 ]; then
umount -q $MPnt
rm -rf $MPnt.*
pkill -SIGKILL persistance >/dev/null 2>&1
exit 0
fi
 fi
#------------------------------
# Copy iso to USB Key
#------------------------------
# show progress
USB_device="$install_usb"
iso_name=$(echo "$NAME" | sed 's/[|].*//')
    touch $MPnt/.temp
    source=$ISO_FILE
    sourcesize=$(du -m -s "$source" | cut -f1)
    target=$MPnt/.temp
    targetsize=$(du -m -s $target | cut -f1)
    cp_target=$MPnt
    cp_sourcesize=$(du -m -s $source | cut -f1)
    cp_targetsize=$(du -m -s $MPnt | cut -f1)
     rsync -av "/tmp/iso"/ "$MPnt"/ &
    dd if=$source of=$target oflag=nocache &
    sleep 0.1;
    while [[ $cp_targetsize -lt $cp_sourcesize ]]; do
    sleep 0.1;
    cp_targetsize=$(du -m -s $target | cut -f1)
    percent=$(("$cp_targetsize"*110/$cp_sourcesize))
    echo $percent 2>/dev/null
    done | yad --window-icon=$ICON --image=$ICON \
    --on-top --width=550 --height=100 --center --fixed --progress \
    --title="$TITLE" --text=$"<b>Creating the Operating System on the Device</b>

    <span color=\"#e07b39\">Installing  $OSNAME $iso_name  to $usbinfo ( $usbdevice )</span>" \
    --auto-close --button=Cancel!$CANCEL:"5"
    if [ $? = "5" ]; then
    killall "rsync" >/dev/null 2>&1
    killall "dd" >/dev/null 2>&1
    sleep 0.50
    umount /tmp/iso
    rm -rf /tmp/iso  > /dev/null 2>&1
    rm -rf /media/root/CopyCat > /dev/null 2>&1
    rm -f /tmp/{selected,copycat} > /dev/null 2>&1
    sleep 1
    umount {$Install_To,$MPnt,$MPnt.*}
    rm -rf {$MPnt.*,$MPnt} > /dev/null 2>&1
    su $USERS -c /usr/share/copycat/cancel_notify.py
    rm -f /tmp/{selected,copycat} > /dev/null 2>&1
    killall "persistence" 2>/dev/null
    killall "copycat" 2>/dev/null
    exit
     fi
 # Change name of ISO files
if [ "$ADD" != "yes" ]
then
stat $MPnt/EFI 2>/dev/null
if [ "$?" = "0" ] ; then
exec $@
fi
 fi
}
copy_extracted_files() {
if [ "$?" != "0" ] ; then
yad --window-icon=$EICON --width=475 --height=125 --fixed \
--on-top --center --image=$EICON \
--title=$"Error" --text=$"
	          <b>An Error has Occured</b>

    Unable to create a directory on your partition.

           Please give feedback about this error.


                             Thank you!" \
--button=Close!$CANCEL:1
if [ $? = 1 ]; then
umount -q $MPnt
rm -rf $MPnt.*
pkill -SIGKILL persistance >/dev/null 2>&1
exit 0
fi
 fi
# Added the EFI directory to list
if [ "$ADD" != "yes" ]
then
stat $sqfs_dir/EFI
if [ "$?" = "0" ] ; then
cp -r $sqfs_dir/EFI $MPnt/EFI
fi
 fi
}
permissions() {
chmod -R 0777 $MPnt
wait
}
unmount_part() {
#-------------------------------------------
# Fix all iso's files to boot proper
#-------------------------------------------
if [ ! -d $MPnt/boot/grub ]
then
mkdir -p $MPnt/boot/grub
fi
# Mint Debian live fix
if [ -f $MPnt/live/initrd.lz ]
then
mv -f $MPnt/live/initrd.lz $MPnt/live/initrd.img
fi
# Mint with casper fix
if [ -f $MPnt/preseed/linuxmint.seedboot ]
then
mv -f $MPnt/casper/initrd.lz $MPnt/casper/initrd
 fi
# Debian live fix
if [ -d $MPnt/dists/bullseye ]
then
mv -f $MPnt/live/vmlinuz-* $MPnt/live/vmlinuz
mv -f $MPnt/live/initrd.img-* $MPnt/live/initrd.img
fi
# Kde Neon fix
#------------------------------
if [ -d "$MPnt/boot/grub/themes/breeze" ]
then
mv -f $MPnt/boot/grub/unicode.pf2 $MPnt/boot/grub/font.pf2
fi
#-------------------------------------------
# Usu fix
#------------------------------
if [ -d "$MPnt/leaflet" ]
then
mv -f $MPnt/casper/vmlinuz.efi $MPnt/casper/vmlinuz
mv -f $MPnt/casper/initrd.lz $MPnt/casper/initrd
fi
#------------------------------
# Zorin fix
#------------------------------
if [ -d "$MPnt/boot/grub/themes/zorin" ]
then
mv -f $MPnt/casper/initrd.lz4 $MPnt/casper/initrd
fi
#------------------------------
# Nitrux fix
#------------------------------
if [ -d "$MPnt/boot/grub/themes/nitrux" ]
then
mv -f $MPnt/boot/grub/fonts/unicode.pf2 $MPnt/boot/grub/font.pf2
mv -f $MPnt/boot/kernel $MPnt/casper/vmlinuz
mv -f $MPnt/boot/initramfs $MPnt/casper/initrd
fi
#------------------------------
# Elementary fix
#------------------------------
if [ -d "$MPnt/boot/grub/elementary-theme" ]
then
mv -f $MPnt/boot/vmlinuz $MPnt/casper/vmlinuz
fi
#------------------------------
# PopOS fix
#------------------------------
find $MPnt/ -type d -name "casper_*" -execdir bash -c 'mv {} tmp' \;
sleep 1
if [ -f "$MPnt/tmp/vmlinuz.efi" ]
then
mv -f $MPnt/tmp/vmlinuz.efi $MPnt/casper/vmlinuz
mv -f $MPnt/tmp/initrd.gz $MPnt/casper/initrd
fi
#------------------------------
# Deepin fix
#------------------------------
if [ -f "$MPnt/deepin-boot-maker.exe" ]
then
mv -f $MPnt/isolinux/isolinux.bin $MPnt/isolinux/syslinux.bin
mv -f $MPnt/live/initrd-*.lz $MPnt/live/initrd.img
fi
#------------------------------
# Peppermint fix missing font.pf2
#------------------------------
if [ -f "$MPnt/boot/grub/unicode.pf2" ]
then
if [ ! -f "$MPnt/boot/grub/font.pf2" ]
then
cp -rf $MPnt/boot/grub/unicode.pf2 $MPnt/boot/grub/font.pf2
fi
 fi
#--------------------------------------
# Copy the boot theme everytime
cp -rf "/usr/share/copycat/theme" $MPnt
#---------------------------------------
# Mint/Ubuntu derivatives fix
if [ -f "$MPnt/casper/initrd.gz" ]
then
mv -f $MPnt/casper/initrd.gz  $MPnt/casper/initrd
fi
if [ -f "$MPnt/casper/initrd.lz" ]
then
mv -f $MPnt/casper/initrd.lz  $MPnt/casper/initrd
fi
# Sparky fix
if [ -f "$MPnt/boot/grub/sparky.cfg" ]
then
rm -f $MPnt/boot/grub/sparky.cfg
cat >> $MPnt/boot/grub/sparky.cfg <<EOF
set timeout=5
if loadfont /boot/grub/font.pf2 ; then
  set gfxmode=1024x768
  terminal_output gfxterm
fi
if background_image /boot/grub/grub.png; then
  set color_normal=light-gray/black
  set color_highlight=white/black
else
  set menu_color_normal=cyan/blue
  set menu_color_highlight=white/blue
fi
export isoboot
menuentry " $NAME Persistence" {
    set gfxpayload=keep
    linux   /live/vmlinuz boot=live persistence quiet splash isoboot intel_iommu=igfx_off
    initrd  /live/initrd.img
}
EOF
fi
#---------------------------------------------
# Check for DOS EFI and Install the bootloader
#---------------------------------------------
mount $Install_To /mnt
if [[ "$DOS" = "dos" ]]
then
grub-install --target=i386-pc --root-directory=/mnt --recheck $usbdevice > /dev/null 2>&1
else
grub-install --target=x86_64-efi --recheck $usbdevice > /dev/null 2>&1
 fi
}
finished() {
rm -f $MPnt/.temp > /dev/null 2>&1
umount {$Install_To,$MPnt}
umount /tmp/iso
rm -rf /tmp/iso > /dev/null 2>&1
rm -rf {/media/root,/mnt/boot,$MPnt,/tmp/copycat} > /dev/null 2>&1
rm -f /tmp/{selected,copycat} > /dev/null 2>&1
# Show finished dialog
iso_name=$(echo "$NAME" | sed 's/[|].*//')
yad --window-icon=$ICON --width=475 --image=$ICON \
--center --on-top --height=100 --fixed --title="$TITLE" --text=$"
<b><span color=\"#e07b39\">  $OSNAME $iso_name Installation is Complete!</span></b>\n \
You may Safely Remove the USB device now.\n" --button=Close!$CANCEL:"1"
if [ "$?" = "1" ];then
exit 0
fi
}
abort() {
if [ "$Install_To" != "" ]; then
umount -q $Install_To
fi
rm -rf $MPnt.*
kill $PID
exit
}
prepare_for_install() {
# Select EFI Partition
efi_Part=""
efi_Part=$(fdisk -l $FDISK_GET -o Device,Type | grep EFI | tr -s ' ' | cut -d ' ' -f1)
# Check for presence of bios_grub (BIOS boot) partition
bios_grub=$(fdisk -l $FDISK_GET -o Device,Type | grep BIOS | tr -s ' ' | cut -d ' ' -f1)
# Only do the following IF the EFI partition exists
if [ "$efi_Part" != "" ]; then
mount $efi_Part $efi_Mnt
   if [ "$ADD" = "no" ]
then
rm -rf $MPnt.*
mkdir -p $efi_Mnt/EFI
# Insert test to ensure we have EFI directory on the EFI partition
if [ "$ADD" = "yes" ]
then
    stat "$efi_Mnt/EFI" 2>/dev/null
if [ "$?" != "0" ]
then
mkdir -p $efi_Mnt/EFI
fi
 fi
  fi
   fi &
#------------------------------
# Ubuntu-based Elementary
# With /casper
#------------------------------
if [ -d "$MPnt/boot/grub/elementary-theme" ]
then
#------------------------------
# Create a new grub.cfg file
#------------------------------
rm -f $MPnt/boot/grub/grub.cfg
mkdir -p $MPnt/boot/grub
cat >> $MPnt/boot/grub/grub.cfg <<EOF
set timeout=5
insmod ext2
insmod all_video
insmod font
insmod png
insmod jpeg
if loadfont "/boot/grub/unicode.pf2"
then
insmod gfxterm
set gfxmode=1024x768
set gfxpayload=keep
terminal_input console
terminal_output gfxterm
fi
set theme="/theme/theme.txt"
export theme

menuentry " $NAME Persistence" {
linux  /boot/vmlinuz boot=casper persistent initrd=/casper/initrd quiet splash intel_iommu=igfx_off fsck.mode=skip noprompt --
initrd /boot/initrd
}
EOF
fi
#------------------------------
# Ubuntu-based PopOS
# With /casper
#------------------------------
if [ -d "$MPnt/boot/grub/themes/pop" ]
then
mv -f $MPnt/casper_* $MPnt/casper
fi
sleep 1
if [ -d "$MPnt/boot/grub/themes/pop" ]
then
#------------------------------
# Create a new grub.cfg file
#------------------------------
rm -f $MPnt/boot/grub/grub.cfg
mkdir -p $MPnt/boot/grub
cat >> $MPnt/boot/grub/grub.cfg <<EOF
set timeout=5
insmod ext2
insmod all_video
insmod font
insmod png
insmod jpeg
if loadfont "/boot/grub/font.pf2"
then
insmod gfxterm
set gfxmode=1024x768
set gfxpayload=keep
terminal_input console
terminal_output gfxterm
fi
set theme="/theme/theme.txt"
export theme

menuentry " $NAME Persistence" {
linux  /casper/vmlinuz boot=casper persistent initrd=/casper/initrd quiet splash intel_iommu=igfx_off fsck.mode=skip noprompt --
initrd /casper/initrd
}
EOF
fi
#------------------------------
# Ubuntu-based Nitrux
# With /casper
#------------------------------
if [ -d "$MPnt/boot/grub/themes/nitrux" ]
then
rm -f $MPnt/boot/grub/grub.cfg
mkdir -p $MPnt/boot/grub
cat >> $MPnt/boot/grub/grub.cfg <<EOF
set timeout=5
insmod ext2
insmod all_video
insmod font
insmod png
insmod jpeg
if loadfont "/boot/grub/fonts/unicode.pf2"
then
insmod gfxterm
set gfxmode=1024x768
set gfxpayload=keep
terminal_input console
terminal_output gfxterm
fi
set theme="/theme/theme.txt"
export theme

menuentry " $NAME Persistence" {
linux /boot/kernel boot=casper persistent initrd=/casper/initrd quiet splash intel_iommu=igfx_off fsck.mode=skip noprompt --
initrd /boot/initramfs
}
EOF
fi
#------------------------------
# Debian-based Deepin
# With /live
#------------------------------
if [ -f "$MPnt/deepin-boot-maker.exe" ]
then
rm -f $MPnt/boot/grub/grub.cfg
mkdir -p $MPnt/boot/grub
cat >> $MPnt/boot/grub/grub.cfg <<EOF
set timeout=5
insmod ext2
insmod all_video
insmod font
insmod png
insmod jpeg
if loadfont "/boot/grub/font.pf2"
then
insmod gfxterm
set gfxmode=1024x768
set gfxpayload=keep
terminal_input console
terminal_output gfxterm
fi
set theme="/theme/theme.txt"
export theme

menuentry " $NAME Persistence" {
  linux  /live/vmlinuz boot=live components splash quiet intel_iommu=igfx_off persistence union=overlay
  initrd /live/initrd.img
}
EOF
fi
#------------------------------
# Ubuntu Releases: Trisquel OS
# With /casper
#------------------------------
if [ -d $MPnt/dists/nabia ]
then
rm -f $MPnt/EFI/BOOT/grub.cfg
mkdir -p $MPnt/EFI/BOOT/
cat >> $MPnt/EFI/BOOT/grub.cfg <<EOF
set timeout=5
insmod ext2
insmod all_video
insmod font
insmod png
insmod jpeg
if loadfont "EFI/BOOT/font.pf2"
then
insmod gfxterm
set gfxmode=1024x768
set gfxpayload=keep
terminal_input console
terminal_output gfxterm
fi
set theme="/theme/theme.txt"
export theme

menuentry " $NAME Persistence" {
linux  /casper/vmlinuz boot=casper persistent initrd=/casper/initrd quiet splash intel_iommu=igfx_off fsck.mode=skip noprompt --
initrd /casper/initrd
}
EOF
 fi
 #------------------------------
# Ubuntu-based Freespire
# With /casper
#------------------------------
if [ -f $MPnt/preseed/custom.seed ]
then
#------------------------------
# Create a new grub.cfg file
#------------------------------
rm -f $MPnt/boot/grub/grub.cfg
mkdir -p $MPnt/boot/grub
cat >> $MPnt/boot/grub/grub.cfg <<EOF
set timeout=5
insmod ext2
insmod all_video
insmod font
insmod png
insmod jpeg
if loadfont "/boot/grub/font.pf2"
then
insmod gfxterm
set gfxmode=1024x768
set gfxpayload=keep
terminal_input console
terminal_output gfxterm
fi
set theme="/theme/theme.txt"
export theme

menuentry " $NAME Persistence" {
linux  /casper/vmlinuz boot=casper persistent initrd=/casper/initrd quiet splash intel_iommu=igfx_off fsck.mode=skip noprompt --
initrd /casper/initrd
}
EOF
 fi
#------------------------------
# Ubuntu-based Mint
# With /casper
#------------------------------
if [ -f $MPnt/preseed/linuxmint.seed ]
then
#------------------------------
# Create a new grub.cfg file
#------------------------------
rm -f $MPnt/boot/grub/grub.cfg
mkdir -p $MPnt/boot/grub
cat >> $MPnt/boot/grub/grub.cfg <<EOF
set timeout=5
insmod ext2
insmod all_video
insmod font
insmod png
insmod jpeg
if loadfont "/boot/grub/font.pf2"
then
insmod gfxterm
set gfxmode=1024x768
set gfxpayload=keep
terminal_input console
terminal_output gfxterm
fi
set theme="/theme/theme.txt"
export theme

menuentry " $NAME Persistence" {
linux  /casper/vmlinuz boot=casper persistent initrd=/casper/initrd quiet splash intel_iommu=igfx_off fsck.mode=skip noprompt --
initrd /casper/initrd
}
EOF
 fi
#------------------------------
# Debian-based MX Linux
# With /live
#------------------------------
if [ -d "$MPnt/antiX" ]
then
#------------------------------
# Create a new grub.cfg file
#------------------------------
rm -f $MPnt/boot/grub/grub.cfg
mkdir -p $MPnt/boot/grub
cat >> $MPnt/boot/grub/grub.cfg <<EOF
set timeout=5
insmod ext2
insmod all_video
insmod font
insmod png
insmod jpeg
if loadfont "/boot/grub/fonts/font.pf2"
then
insmod gfxterm
set gfxmode=1024x768
set gfxpayload=keep
terminal_input console
terminal_output gfxterm
fi
set theme="/theme/theme.txt"
export theme

menuentry " $NAME Persistence" {
  linux  /antiX/vmlinuz persist persist_all quiet intel_iommu=igfx_off
  initrd /antiX/initrd.gz
}
EOF
fi
#------------------------------
# Debian-based ISO's
# With /live
#------------------------------
if [ -d "$MPnt/d-i" ]
then
if [ -d "$MPnt/live" ]
then
rm -f $MPnt/boot/grub/grub.cfg
mkdir -p $MPnt/boot/grub
#------------------------------
# Create a new grub.cfg file
#------------------------------
cat >> $MPnt/boot/grub/grub.cfg <<EOF
set timeout=5
insmod ext2
insmod all_video
insmod font
insmod png
insmod jpeg
if loadfont "/boot/grub/font.pf2"
then
insmod gfxterm
set gfxmode=1024x768
set gfxpayload=keep
terminal_input console
terminal_output gfxterm
fi
set theme="/theme/theme.txt"
export theme

menuentry " $NAME Persistence" {
linux /live/vmlinuz boot=live quiet splash persistence intel_iommu=igfx_off username=guest apparmor=0
initrd /live/initrd.img
}
EOF
   fi
    fi
#------------------------------
# Other Debian-based ISO's
# With /live
#------------------------------
if [ -d "$MPnt/live" ]
then
if [ -f "$MPnt/live/vmlinuz" ]
then
rm -f $MPnt/boot/grub/grub.cfg
mkdir -p $MPnt/boot/grub
#------------------------------
# Create a new grub.cfg file
#------------------------------
cat >> $MPnt/boot/grub/grub.cfg <<EOF
set timeout=5
insmod ext2
insmod all_video
insmod font
insmod png
insmod jpeg
if loadfont "/boot/grub/font.pf2"
then
insmod gfxterm
set gfxmode=1024x768
set gfxpayload=keep
terminal_input console
terminal_output gfxterm
fi
set theme="/theme/theme.txt"
export theme

menuentry " $NAME Persistence" {
linux /live/vmlinuz boot=live quiet splash persistence intel_iommu=igfx_off username=guest apparmor=0
initrd /live/initrd.img
}
EOF
   fi
    fi
 #------------------------------
# Ubuntu Releases
# With /casper
#------------------------------
if [ -f $MPnt/boot/memtest86+.bin ]
then
if [ ! -d $MPnt/live ]
then
if [ -d $MPnt/casper ]
then
rm -f $MPnt/boot/grub/grub.cfg
mkdir -p $MPnt/boot/grub
cat >> $MPnt/boot/grub/grub.cfg <<EOF
set timeout=5
insmod ext2
insmod all_video
insmod font
insmod png
insmod jpeg
if loadfont "/boot/grub/fonts/unicode.pf2"
then
insmod gfxterm
set gfxmode=1024x768
set gfxpayload=keep
terminal_input console
terminal_output gfxterm
fi
set theme="/theme/theme.txt"
export theme

menuentry " $NAME Persistence" {
linux  /casper/vmlinuz boot=casper persistent initrd=/casper/initrd quiet splash intel_iommu=igfx_off fsck.mode=skip noprompt --
initrd /casper/initrd
}
EOF
fi
 fi
  fi
permissions
}
usb_install() {
set_variables
get_device
get_device_type
create_mounts
write_label
get_uuid
mount_part
get_name
if [ "$LIVE" != "yes" ]
then
get_iso
fi
	(
        echo ; sleep 1
if [ "$ADD" = "yes" ]; then
stat "$MPnt/boot/grub/grub.cfg"
 fi
if [ "$ADD" != "yes" ]
then
wipe_part
fi
if [ "$LIVE" != "yes" ]
then
get_size
copy_iso_files
else
get_size2
copy_extracted_files
fi
prepare_for_install
unmount_part
echo "Install Complete" 2>/dev/null
 )
finished
# Eject the device
eject $DRIVES
}
# Main Program
run_as_root
usb_install
rm -rf {$MPnt,$MPnt.*} >/dev/null 2>&1
exit 0


