#!/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_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_BLANK='\033[0m'      C_FLASH='\033[1;5m'

TOOLKIT_PATH="$(pwd)" # "${HOME}/ps4"

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

echo -e "${C_GRN_BLD}PS4 USB Formatter v1.2 by Berion"
echo -e "${C_YLW}Script for formatting the USB drive to exFAT and creating of all folders."
echo
echo -e "${C_RED_BLD}MBR${C_RED}/${C_RED_BLD}GPT${C_RED} partition table will ${C_RED_BLD}not${C_RED} be created (it is not needed)."
echo -e "File System will start from LBA0 instead LBA1 or LBA2."
echo
echo -e "${C_RED_BLD}Confirm root permissions every time you will be asked for.${C_BLANK}"
echo
read -p "Press enter key to continue. "
echo

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

# sudo apt update
# sudo apt install tree

echo -e "Choose your ${C_YLW_BLD}USB storage${C_BLANK} (type e.g. /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 -e "${C_RED}All data on chosen device will be lost!${C_BLANK}"
echo
read -p "Press any key to continue. "
echo

sudo mkfs.exfat -c 64K -L "PS4" ${DEVICE}

sudo mount ${DEVICE} "${TOOLKIT_PATH}/storage/usb"
cd "${TOOLKIT_PATH}/storage/usb"

NP_ACCOUNT_ID=$(cat "${TOOLKIT_PATH}/keys/account_id.txt")

sudo mkdir -p "PS4/SHARE/Video Clips/"			# Official place for exported game videos (cannot be imported).
sudo mkdir -p PS4/SHARE/Screenshots/			# Official place for exported game screenshots (cannot be imported).
sudo mkdir -p PS4/SHAREFACTORY/MUSIC			# Official place for Share Factory stuff.
sudo mkdir -p PS4/Music								# ?

sudo mkdir -p PS4/SAVEDATA/${NP_ACCOUNT_ID}/	# Official place for saves (PS4 games and official emulators).
sudo mkdir -p PS4/EXPORT/BACKUP/					# Official place for System Backup Utility *.dat archives (inside "<YYYYMMDDHHMM_00>" folder).
sudo mkdir -p PS4/UPDATE/							# Official place for "PS4UPDATE.PUP" firmware update package.

sudo mkdir -p PS4/APOLLO/							# Apollo's place for decrypted PS4 saves.

sync
tree -a
echo
sudo umount -l "${TOOLKIT_PATH}/storage/usb"

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

unset DEVICE
unset NP_ACCOUNT_ID

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