#!/bin/bash
#
# Copyright (C) 2014 - 2018 x-odos GnbH
#

if [ $# -ne 2 ]
then
    echo "Usage: format-usb-drive <device-name> <Stick=1 Backup=0>"
    exit 1
fi

USB_DRIVE=$1 
PARTITION=${USB_DRIVE}1
STICK=$2
DRIVENAME=${USB_DRIVE##*\/}
NOW=`date +'%d.%m.%Y - %H:%M:%S'`

# Make sure device is a USB drive
if ! udevadm info --query=path --name $USB_DRIVE | grep usb > /dev/null 2>&1
then
    echo "$USB_DRIVE is not a USB device"
    echo '{"format":"error", "time":"'$NOW'", "status":"not a USB device"}' > /var/log/format_usb.json
    exit 3
fi

# Count Partitions
NOPART=$(/bin/lsblk | /bin/grep $DRIVENAME | /bin/sed '/^$/d'| /bin/awk '{print NR}'| /bin/sort -nr| sed -n '1p')
NOPART=$[${NOPART}-1]

# Make sure drive is not mounted
i=0
while [[ $i -le $NOPART ]]
do
	i=$[$i+1]
	#echo "stopping auto mount for this drive"
	#sudo /bin/systemctl stop usb-mount@${DRIVENAME}${i}.service
	if /bin/mount | /bin/grep ${USB_DRIVE}${i} > /dev/null
	then
		echo "${USB_DRIVE}${i} is mounted; unmounting..."
   		echo '{"format":"error", "time":"'$NOW'", "status":"mounted"}' > /var/log/format_usb.json
   		j=0
   		sudo /bin/umount -l ${USB_DRIVE}${i}
   		while /bin/mount | /bin/grep ${USB_DRIVE}${i} > /dev/null
		do 
			j=$[$j+1]
			sudo /bin/umount -l ${USB_DRIVE}${i}
	 		if /bin/mount | /bin/grep ${USB_DRIVE}${i} > /dev/null
			then
    			echo '{"format":"error", "time":"'$NOW'", "status":"trying unmount - '$j'"}' > /var/log/format_usb.json
				echo "trying unmount - $j"
				sleep 10	
			fi	
		done
		sleep 5
	fi
done

# Partition the drive using parted
echo "Cleaning drive..."
echo '{"format":"cleaning", "time":"'$NOW'", "status":"Cleaning Drive"}' > /var/log/format_usb.json
sgdisk --zap-all --clear --mbrtogpt $USB_DRIVE > /dev/null 
if [[ $STICK != 1 ]]; then
	sgdisk -N 1 -c 1:"x-odos_Backup" -t 1:0700 $USB_DRIVE > /dev/null 
else
	sgdisk -N 1 -c 1:"x-odos" -t 1:0700 $USB_DRIVE > /dev/null 
fi
if [ $? -ne 0 ]
then
    echo "sgdisk failed!"
    echo '{"format":"cleaning", "time":"'$NOW'", "status":"Cleaning failed!"}' > /var/log/format_usb.json

    exit 4
fi

# Format the drive with an NTFS filesystem

echo "Formatting drive with NTFS..."
echo '{"format":"cleaning", "time":"'$NOW'", "status":"Formatting"}' > /var/log/format_usb.json
if [[ $STICK != 1 ]]; then
	#sudo mkfs -t exfat -n "x-odos Backup" $PARTITION > /dev/null
	sudo mkntfs -f --label "x-odos Backup" $PARTITION > /dev/null
else
	sudo mkntfs -f --label "x-odos" $PARTITION > /dev/null
fi 
if [ $? -ne 0 ]
then
    echo "mkntfs failed!"
    echo '{"format":"cleaning", "time":"'$NOW'", "status":"Formatting failed!"}' > /var/log/format_usb.json
    exit 5
fi

sleep 2
#ntfsfix $PARTITION > /dev/null

# Mount the drive
if [[ $STICK != 1 ]] 
then 
	# Mount the drive
	while /bin/mount | /bin/grep $PARTITION > /dev/null
	do 
		sudo /bin/umount -l $PARTITION
		sleep 2
	done
	rm -rf /backup
	mkdir -p /backup 
	sudo /bin/mount $PARTITION /backup
	if [ $? -ne 0 ]; then
  		echo "mount failed!"
  		echo '{"format":"mounting", "time":"'$NOW'", "status":"Drive mount failed!"}' > /var/log/format_usb.json
   		exit 6
   	fi
	# Create a x-odos.txt file on the drive
	cp -r /fusb/* /backup	
	echo "This is a x-odos drive created on $(date)" > /backup/x-odos.txt
	
	# Unmount the drive when done
	umount -l /backup
	echo '{"format":"finished", "time":"'$NOW'", "status":"Preparing Drive finished"}' > /var/log/format_usb.json
fi 
# Done

parted $USB_DRIVE print
