#!/bin/bash

dest_usb=$1
inst_dir=/tmp/install-toolx

reset="[0m"
blink="[5m"
lightred="[1;31m"
lightgreen="[1;32m"
yellow="[1;33m"
lightblue="[1;34m"
lightpurple="[1;35m"
white="[1;37m"

for i in sgdisk mkfs.fat mkfs.ext4 mount tar grub-install; do
  which -s $i
  errorlevel=$?
  if [ $errorlevel = 1 ]; then
    missingfile=$errorlevel
    echo "${lightred}$i ${white}missing${reset}"
  else
    echo "${lightgreen}$i ${lightblue}OK${reset}"
  fi
done

if [ "$missingfile"_ = 1_ ]; then
  exit "$missingfile"
fi

echo -e OK!\\n

mount |grep /dev/"${dest_usb}" > /dev/null; i=$?
if [ "$i" -eq 0 ]; then
  echo "${lightpurple}Disk ${dest_usb} is in use!${reset}"
else
  echo "${blink}${lightred}WARNING!${reset} ${lightpurple}All data on ${lightblue}${dest_usb}${reset}${lightpurple} will be destroy!${reset}"
  read -r -p "${yellow}Do you want to continue? (Y/N) ${reset}" -e choice
fi

makehybrid ()
{
  # destroy the GPT and MBR data structures
  echo "${lightblue}Destroying the GPT and MBR data structures...${reset}"
  sgdisk --zap-all /dev/"${dest_usb}" > /dev/null; i=$?
  if [ "$i" -eq 0 ]; then
    # creating GPT table
    echo "${lightblue}Creating GPT table...${reset}"
    sgdisk --clear /dev/"${dest_usb}" > /dev/null; i=$?
    if [ "$i" -eq 0 ]; then
      # create partitions
      echo "${lightblue}Creating BIOS boot partition...${reset}"
      sgdisk --new=1::+1M --typecode=1:ef02 --change-name=1:"BIOS boot partition" /dev/"${dest_usb}" > /dev/null; i=$?
      if [ "$i" -eq 0 ]; then
        echo "${lightblue}Creating EFI system partition...${reset}"
        sgdisk --new=2::+50M --typecode=2:ef00 --change-name=2:"EFI system partition" /dev/"${dest_usb}" > /dev/null; i=$?
        if [ "$i" -eq 0 ]; then
          echo "${lightblue}Creating Linux partition...${reset}"
          sgdisk --new=3 --change-name=3:"Linux filesystem" /dev/"${dest_usb}" > /dev/null; i=$?
          if [ "$i" -eq 0 ]; then
            # create hybrid MBR, add partitions and place EFI GPT (type 0xEE) last in the table
            echo "${lightblue}Creating hybrid MBR...${reset}"
            sgdisk --hybrid 1:2:3:EE /dev/"${dest_usb}" > /dev/null; i=$?
            if [ "$i" -eq 0 ]; then
              # format the EFI System and linux partition
              echo "${lightblue}Formatting partitions...${reset}"
              mkfs.fat -F32 -i 01234567 /dev/"${dest_usb}"2; i=$?
              mkfs.ext4 -F -L toolx3 -U 3907f767-172e-57ea-a599-d11ed5b875b9 /dev/"${dest_usb}"3 > /dev/null; i=$?
              if [ "$i" -eq 0 ]; then
                # mount linux partition
                echo "${lightblue}Mounting linux partitions...${reset}"
                mkdir ${inst_dir} > /dev/null; i=$?
                if [ "$i" -eq 0 ]; then
                  mount -t ext4 /dev/"${dest_usb}"3 ${inst_dir} > /dev/null; i=$?
                  if [ "$i" -eq 0 ]; then
                    # untar toolx3_amd64.tar.gz to ${inst_dir}
                    echo "${lightblue}Installing file system...${reset}"
                    tar --use-compress-program=pigz --numeric-owner -xf toolx3_amd64.tar.gz --directory=${inst_dir}; i=$?
                    sync
                    if [ "$i" -eq 0 ]; then
                      # mount EFI partition
                      echo "${lightblue}Mounting EFI partitions...${reset}"
                      mount -t vfat /dev/"${dest_usb}"2 ${inst_dir}/boot/efi > /dev/null; i=$?
                      if [ "$i" -eq 0 ]; then
                        # install GRUB for UEFI
                        echo "${lightblue}Installing GRUB for UEFI...${reset}"
                        grub-install --target=x86_64-efi --recheck --boot-directory=${inst_dir}/boot --efi-directory=/tmp/install-toolx/boot/efi --removable /dev/"${dest_usb}" > /dev/null; i=$?
                        if [ "$i" -eq 0 ]; then
                          # install GRUB for BIOS
                          echo "${lightblue}Installing GRUB for BIOS...${reset}"
                          grub-install --target=i386-pc --recheck --boot-directory=${inst_dir}/boot /dev/"${dest_usb}" > /dev/null; i=$?
                          if [ "$i" -eq 0 ]; then
                            # umount all
                            echo "${lightblue}Unmounting all partitions and remooving temporary directory...${reset}"
                            umount ${inst_dir}/boot/efi > /dev/null
                            umount ${inst_dir} > /dev/null
                            rmdir ${inst_dir} > /dev/null
                            echo "${lightgreen}Done.${reset}"
                          else
                            echo "${lightred}Something is wrong!${reset}"
                            exit
                          fi
                        else
                          echo "${lightred}Something is wrong!${reset}"
                          exit
                        fi
                      else
                        echo "${lightred}Something is wrong!${reset}"
                        exit
                      fi
                    else
                      echo "${lightred}Something is wrong!${reset}"
                      exit
                    fi
                  else
                    echo "${lightred}Something is wrong!${reset}"
                    exit
                  fi
                else
                  echo "${lightred}Something is wrong!${reset}"
                  exit
                fi
              else
                echo "${lightred}Something is wrong!${reset}"
                exit
              fi
            else
              echo "${lightred}Something is wrong!${reset}"
              exit
            fi
          else
            echo "${lightred}Something is wrong!${reset}"
            exit
          fi
        else
          echo "${lightred}Something is wrong!${reset}"
          exit
        fi
      else
        echo "${lightred}Something is wrong!${reset}"
        exit
      fi
    else
      echo "${lightred}Something is wrong!${reset}"
      exit
    fi
  else
    echo "${lightred}Something is wrong!${reset}"
    exit
  fi
}

case "${choice}" in
  Y)
    makehybrid
    ;;
  *)
    exit
    ;;
esac
