#! /bin/bash

#-----------------------------------------------------------------------------
#
## Copyright 2016 Nio Wiklund
#
# GPLv3: GNU GPL version 3
# <http://gnu.org/licenses/gpl.html>.
#
# This is free software: you are free to change and redistribute it.
# There is NO WARRANTY, to the extent permitted by law.

# date        editor   comment
# 2016-02-26  sudodus  created from mk_mkdos in mkusb
# 2016-02-27  sudodus  accepting only mass storage devices as target
# 2016-02-27  sudodus  version 1.1
# ----------------------------------------------------------------------
# 2016-10-19  sudodus  prepared with pff for dus
# 2016-10-19  sudodus  created from restore-pendrive (simplified)
# 2016-11-14  sudodus  dus-restore version 0.0.0
# 2016-12-29  sudodus  version 12.0.0 - mkusb-dus
# 2019-11-16  sudodus  modifications for nvme drives (alongside mmcblk)
#                      version 12.3.8
# 2020-02-07  sudodus  parted set partition type to FAT32
#                      version 12.4.1

version="${0##*/} 12.4.1"

target=
label=
inversvid="\0033[7m"
resetvid="\0033[0m"
#redtext="\0033[31;47m"
redback="\0033[1;37;41m"
greenback="\0033[1;37;42m"

#######################################################################
#######################################################################

function read_pff {

# reading $1
# assigning common variable(s) (declared above)

if [ "${1:0:4}" == "pff:" ]
then
# cat "${1/pff:}"
 for i in action target label
 do
  read $i
 done < "${1/pff:}"
 rm     "${1/pff:}"
# echo "action=$action" # action is a dummy variable in this version
# echo "target=$target"
# echo "label=$label"
else
 echo "no pff input"
fi
}
#######################################################################
#######################################################################

function mk_msdos {

# make an MSDOS partition table and a FAT32 file system

target="$1"
label1="$2"
label1="${label1:0:11}"
label1="${label1^^}"
#echo "Label (name) for the FAT32 partition: $label1"

echo "Trying to unmount partitions if mounted on the target device"
umount "$target"*
df | grep "$target"
if [ $? -eq 0 ]
then
 echo "mk_msdos: could not unmount a partition on the target device"
 exit
fi
echo "--------------------------------------------------------------------------------"
gpt_zap "$target"
sleep 0.5
gpt_fix "$target"
dd if=/dev/zero of="$target" bs=1024 count=1024 2>&1

parted -s "$target" mklabel msdos
sleep 0.5
parted -s "$target" mkpart primary fat32 1048576b 100%
sleep 0.5
parted -s "$target" set 1 boot on  # set boot flag on partition 1
sleep 0.5
if [ "${target:0:11}" == "/dev/mmcblk" ] || [ "${target:0:9}" == "/dev/nvme" ]
then
 mkfs.vfat -v -F 32 -n "$label1" "${target}p1"
else
 mkfs.vfat -v -F 32 -n "$label1" "${target}1"
fi

if [ $? -eq 0 ]
then
 success=true
else
 success=false
fi
sleep 0.5
sync
if $success
then
 /bin/echo -e "$greenback Done :-) $resetvid
$inversvid created MSDOS partition table and FAT file system $resetvid"
else
 /bin/echo -e "$redback Failed :-( $resetvid
 $inversvid mk_msdos: could not create MSDOS partition table and FAT file system $resetvid"
fi
}
########################################################################

function part_info {

target=$1
sync
sleep 0.5
if [ "${target:0:11}" == "/dev/mmcblk" ]
then
 read -p "Unplug and re-plug the flash card to see the correct info ... and press Enter "
#elif [ "$disk_name_type" == "microsoft-windows" ]
#then
# read -p "Unplug and re-plug the target drive to see the correct info ... and press Enter "
fi
dlay=5
echo "Wait $dlay seconds and a little more ..."
sleep $dlay

blkid -c /dev/null "${target}?" > /dev/null
sleep 0.5
partinfo=$(parted -s "$target" print; \
echo " "; \
lsblk -o MODEL,NAME,FSTYPE,LABEL "$target")

echo "$partinfo"
}
########################################################################
########################################################################

function gpt_fix {

# $1 is the target device

echo \
"v
q" \
| gdisk "$1" 2>/dev/null |grep -e 'GPT: damaged' > /dev/null 2>&1
if [ $? -eq 0 ]
then
echo \
"v
x
e
r
d
w
y" \
| gdisk "$1" > /dev/null 2>&1

echo \
"v
q" \
| gdisk "$1" 2>/dev/null |grep -e 'GPT: damaged' -e 'Problem:' > /dev/null 2>&1
if [ $? -eq 0 ]
then
 echo "gpt_fix: failed to fix the GUID partition table (GPT) of $1"
else
 echo "gpt_fix: done :-)"
fi
fi
}
########################################################################
########################################################################

function gpt_zap {

# $1 is the target device

echo \
"
x
z
y
y" \
| gdisk "$1" > /dev/null 2>&1

echo "gpt_zap: done"
}
########################################################################
########################################################################
#
# main program
#
########################################################################
########################################################################

read_pff "$1"

if [ "$target" != "" ]
then
# do it
 mk_msdos "$target" "$label"
# check it
 part_info "$target"
fi
