#!/bin/bash
clear

C_RED_BLD='\033[1;31m' C_YLW_BLD='\033[1;93m' C_GRN_BLD='\033[1;32m' C_BLU_BLD='\033[1;94m' C_CYN_BLD='\033[1;36m' C_WHT_BLD='\033[1;37m'	C_GRY_BLD='\033[1;30m'
C_RED='\033[0;91m'	  C_YLW='\033[0;93m'		 C_GRN='\033[0;92m'		C_BLU='\033[0;94m'	  C_CYN='\033[0;36m'		 C_WHT='\033[0;37m'		C_GRY='\033[0;30m'
C_BLANK='\033[0m'      C_FLASH='\033[1;5m'

TOOLKIT_PATH="$(pwd)" # "${HOME}/ps4"
TIME_STAMP=$(date +%Y%m%d%H%M%S)

function function_device() {
	echo
	echo -e "Choose your ${C_YLW_BLD}disk from PS4${C_BLANK} (type eg: /dev/sdx) from below device list:"
	echo
	lsblk -p -o MODEL,NAME,SIZE,LABEL,FSTYPE,MOUNTPOINT
	echo
	read DEVICE
	echo -e "OK, so you've chosen: \"${C_YLW_BLD}${DEVICE}${C_BLANK}\"."
	echo
	}

function function_continue() {
	echo
	read -p "Press enter key to continue. "
	echo
	}

function function_exit() {
	echo
	read -p "Press enter key to exit script. "
	exit
	}

function function_exit_2() {
	read -p "You didn't chose propper option... Press enter key to exit script. "
	exit
	}

# ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

echo -e "${C_GRN_BLD}PS4 HDD Tasker v1.1 by Berion"
echo -e "${C_YLW}Script for various disk related tasks. Most are ${C_RED_BLD}DANGEROUS${C_YLW} for unaware users!"
echo
echo -e "${C_RED_BLD}Confirm root permissions every time you are asked!${C_BLANK}"
echo

# ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

echo -e " ${C_WHT_BLD}1.${C_BLANK} Check disk condition."
echo -e " ${C_WHT_BLD}2.${C_BLANK} Perform Secure Erase."
echo -e " ${C_WHT_BLD}3.${C_BLANK} Backup or restore GPT."
echo -e " ${C_WHT_BLD}4.${C_BLANK} Clear UPDATE partition."
echo

while :; do
read MENU
case $MENU in

	1) # S.M.A.R.T. reading:
		echo
		echo -e "${C_YLW_BLD}Task for reading disk S.M.A.R.T. report (SHORT test only)."
		echo
		echo -e "${C_WHT_BLD}Purpose:${C_BLANK}"
		echo -e " ∙ Checking storage media health status."
		echo
		echo -e "${C_WHT_BLD}Requirements:${C_BLANK}"
		echo -e " ∙ Disk Decryption         ${C_GRN_BLD}doesn't matter${C_BLANK}"
		echo -e " ∙ File Systems Mounting   ${C_GRN_BLD}doesn't matter${C_BLANK}"
		echo -e " ∙ Write Permission        ${C_GRN_BLD}doesn't matter${C_BLANK}"
		echo
		echo -e "${C_WHT_BLD}Additional Note:${C_BLANK}"
		echo -e " ∙ Full log file will be saved in:"
		echo -e "   ${C_YLW}${TOOLKIT_PATH}/logs/${C_YLW_BLD}smart_<time stamp>.txt${C_BLANK}"
		function_continue
		function_device
		sudo smartctl -A ${DEVICE}
		sudo smartctl -a ${DEVICE} >> "${TOOLKIT_PATH}/logs/smart_${TIME_STAMP}.txt"
	 # sudo smartctl -x ${DEVICE} >> "${TOOLKIT_PATH}/logs/smart_${TIME_STAMP}.txt"
		break;;

	2) # Secure Erase:
		echo
		echo -e "${C_YLW_BLD}Task for erasing ALL data from disk."
		echo
		echo -e "${C_WHT_BLD}Purpose:${C_BLANK}"
		echo -e " ∙ Deleting all data, including removing DCO/HPA/AMAC."
		echo -e " ∙ Speeding up writing on SSD after long usage."
		echo
		echo -e "${C_WHT_BLD}Requirements:${C_BLANK}"
		echo -e " ∙ Disk Decryption         ${C_GRN_BLD}no${C_BLANK}"
		echo -e " ∙ File Systems Mounting   ${C_YLW_BLD}n/a${C_BLANK}"
		echo -e " ∙ Write Permission        ${C_YLW_BLD}n/a${C_BLANK}"
		function_continue
		function_device
		echo -e "${C_RED_BLD}IF${C_BLANK} device status is ${C_RED_BLD}FROZEN${C_BLANK}, put computer in sleep and then wake him up."
		echo -e "Frozen disks cannot be sanitized."
		echo
	 # sudo hdparm -I ${DEVICE} | grep --colour=auto -i frozen
		sudo hdparm -I ${DEVICE} | grep -i -A 9 SECURITY:
		echo
		echo -e "${C_RED_BLD}If${C_RED} disk is connected via ${C_RED_BLD}USB adapter${C_RED}, keep in mind that many USB controllers"
		echo -e "do not support ATAPI completely. Error like: \"${C_RED_BLD}ERASE_PREPARE: Invalid argument${C_RED}\""
		echo -e "imply that ${C_RED_BLD}Security Erase${C_RED} could not be started."
		echo
		echo -e "${C_RED_BLD}It is last moment to quit by closing this window. Or else, any data recovery"
		echo -e "will be IMPOSSIBLE on chosen device!${C_BLANK}"
		function_continue

	 # Do NOT use Master switch:
		sudo hdparm --user-master u --security-set-pass PS4 ${DEVICE}
	 # sudo hdparm --user-master m --security-set-pass PS4 ${DEVICE}

	 # Security Erase:
		sudo hdparm --user-master u --security-erase PS4 ${DEVICE}
	 # sudo hdparm --user-master m --security-erase PS4 ${DEVICE}

	 # Enhanced Security Erase (not supported by all drives):
	 # sudo hdparm --user-master u --security-erase-enhanced PS4 ${DEVICE}
	 # sudo hdparm --user-master m --security-erase-enhanced PS4 ${DEVICE}

	 # Sanitize/Scramble (both not supported by all drives):
	 # sudo hdparm --sanitize-status ${DEVICE}
	 # sudo hdparm --yes-i-know-what-i-am-doing --sanitize-block-erase ${DEVICE}
	 # sudo hdparm --yes-i-know-what-i-am-doing --sanitize-crypto-scramble ${DEVICE}
	 # sudo hdparm --sanitize-status ${DEVICE}

	 # Drive will be locked if SE/ESE/SAN not completed, unlock and try again:
	 # sudo hdparm --user-master u --security-unlock PS4 ${DEVICE}
	 # sudo hdparm --user-master m --security-unlock PS4 ${DEVICE}
	 # sudo hdparm --user-master u --security-disable PS4 ${DEVICE}
	 # sudo hdparm --user-master m --security-disable PS4 ${DEVICE}
		break;;

	3) # GPT:
		echo
		echo -e "${C_YLW_BLD}Task for making or restoring GPT partition table."
		echo
		echo -e "${C_WHT_BLD}Purpose:${C_BLANK}"
		echo -e " ∙ Backup/Restore GPT in case of overwriting or corruption."
		echo
		echo -e "${C_WHT_BLD}Requirements:${C_BLANK}"
		echo -e " ∙ Disk Decryption         ${C_GRN_BLD}no${C_BLANK}"
		echo -e " ∙ File Systems Mounting   ${C_YLW_BLD}n/a${C_BLANK}"
		echo -e " ∙ Write Permission        ${C_YLW_BLD}n/a${C_BLANK}"
		function_continue
		function_device
		echo -e " ${C_WHT_BLD}A.${C_BLANK} Backup GPT."
		echo -e " ${C_WHT_BLD}B.${C_BLANK} Restore GPT."
		while :; do
		read MENU_SUB
		case $MENU_SUB in
			A | a)
				echo
				sudo dd if=${DEVICE} of=${TOOLKIT_PATH}/storage/hdd/fix_gpt_internal.bin bs=1M count=1
				break;;
			B | b)
				echo
				sudo dd if=${TOOLKIT_PATH}/storage/hdd/fix_gpt_internal.bin of=${DEVICE} conv=notrunc
				break;;
			*)
				function_exit_2
				break;;
		esac
		done
		break;;

	4) # UPDATE clearing:
		echo
		echo -e "${C_YLW_BLD}Task for clearing out UPDATE partition."
		echo
		echo -e "${C_WHT_BLD}Purpose:${C_BLANK}"
		echo -e " ∙ Fixing boot loop caused by FAT32 corruption."
		echo
		echo -e "${C_WHT_BLD}Requirements:${C_BLANK}"
		echo -e " ∙ Disk Decryption         ${C_RED_BLD}yes${C_BLANK}"
		echo -e " ∙ File Systems Mounting   ${C_RED_BLD}no${C_BLANK}"
		echo -e " ∙ Write Permission        ${C_RED_BLD}yes${C_BLANK}"
		function_continue
		echo -e " ${C_WHT_BLD}A.${C_BLANK} Check FAT32 condition (no changes)."
		echo -e " ${C_WHT_BLD}B.${C_BLANK} Fix FAT32 file system and delete all files."
		echo
		while :; do
		read MENU_SUB
		case $MENU_SUB in
			A | a)
				echo
				sudo dosfsck -l -t -v -V /dev/mapper/ps4hdd_25
				break;;
			B | b)
				echo
			 #	sudo rm -rfv ${TOOLKIT_PATH}/storage/hdd/update/*
				sudo rm -rfv ${TOOLKIT_PATH}/storage/hdd/update/ps4upd~1.pup/*
				sudo rm -rfv ${TOOLKIT_PATH}/storage/hdd/update/ps4upd~1.tem/*
				sudo umount -l ${TOOLKIT_PATH}/storage/hdd/update
				sudo dosfsck -a -l -t -w -v -V /dev/mapper/ps4hdd_25
				break;;
			*)
				function_exit_2
				break;;
		esac
		done
		break;;

	*)
		function_exit_2
		break;;
esac
done

# ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

unset DEVICE
unset MENU
unset MENU_SUB
unset TIME_STAMP

echo -e ${C_WHT_BLD}
read -p "All done. Press enter key to exit. "
