#!/bin/bash

# Written by Sophie (https://github.com/pyjulian)
# So this requires a shitty camera of Aliexpress (https://nl.aliexpress.com/item/1005009443076761.html) I've used this one
# I'll upload a blog sooner or later on my website :)

# So I'm lowk NOT locating the CameraVolume so you will locate it for me :D (It will save once so you don't have to redo the process each time)
getdir() {
	if [ -f ~/.ccuconfig ]; then
		echo $(cat ~/.ccuconfig)
	else
		read -p "CCU (Config): Enter the VolumePath to the camera: " newpath
		if [ -f $newpath/time.txt ]; then
			echo $newpath > ~/.ccuconfig
			echo $newpath
		else
			echo "CCU (Config): '$newpath' doesn't exist (n)or is a valid CCU folder"
			exit 1
		fi
	fi
}

# Block and check for the file only if the user isn't asking for the help page
if [[ ! "$1" == '-h' && ! "$1" == '--help' && ! "$1" == '' ]]; then
	path=$(getdir)
	path=${path/#\~/$HOME}
fi

case "$1" in
	--extract|-e|--extractverbose|-ev|--delete|-d|--deleteverbose|-dv)
		# Insane beast of code
		if [[ $2 && ($3 || "$1" == '--delete' || "$1" == '-d' || "$1" == '--deleteverbose' || "$1" == '-dv') ]]; then
			# if [[ ! -d "$3" && ($1 == '--extract' || $1 == '-e' || $1 == '--extractverbose' || $1 == '-ev') ]]; then
				# echo "CCU ($1): OUTPUT_DIR doesn't exist"
				# exit 1
			# fi

			# Convert tags in valid folders in the most ugly way possible
			# if [[ $2 == '-iva' ]]; then
			# 	tar=('PHOTO' 'VIDEO' 'AUDIO')
			# elif [[ $2 == '-iv' ]]; then
			# 	tar=('PHOTO' 'VIDEO')
			# elif [[ $2 == '-ia' ]]; then
			# 	tar=('PHOTO' 'AUDIO')
			# elif [[ $2 == '-va' ]]; then
			# 	tar=('VIDEO' 'AUDIO')
			# elif [[ $2 == '-i' ]]; then
			# 	tar=('PHOTO')
			# elif [[ $2 == '-v' ]]; then
			# 	tar=('VIDEO')
			# elif [[ $2 == '-a' ]]; then
			# 	tar=('AUDIO')
			# else
			# 	tar=()
			# fi

			# Convert tags in valid folders in the most clean way possible
			tar=()
			[[ $2 == *i* ]] && tar+=('PHOTO')
			[[ $2 == *v* ]] && tar+=('VIDEO')
			[[ $2 == *a* ]] && tar+=('AUDIO')

			# Values and folder if it doesn't exist
			out=${3/#\~/$HOME}
			loop=1
			mkdir -p "$out"

			# Loop through the flag folders to do the action, messages get blocked with Vanquish
			for folder in ${tar[@]}; do
				if [[ "$1" == '--extract' || "$1" == '-e' || "$1" == '--delete' || "$1" == '-d' ]]; then
					echo "CCU ($1): Folder (${tar[loop-1]}) $loop/${#tar[@]}"
				fi

				if [[ "$1" == '--extract' || "$1" == '-e' || "$1" == '--extractverbose' || "$1" == '-ev' ]]; then
					if [[ ${#tar[@]} > 1 ]]; then
						mkdir -p "$out/$folder"
						cp -a "$path/$folder/" "$out/$folder"
					else
						cp -a "$path/$folder/" "$out"	
					fi
				else
					rm "$path"/"$folder"/*
				fi
				((loop+=1))
			done

			echo "CCU ($1): Action finished" # Message for finishing
		else
			echo "CCU ($1): [-iva] [$1] OUTPUT_DIR" # Error message for not using flags
			exit 1
		fi
	;;
	--time|-t)
		# Edits the time
		if [[ $2 ]]; then
			newtime=$2 # Instantly set the time via a tag
		else
			# What this does: Get Current Time > Removes the random Y from it > Reads your Input
			time=$( cat "$path"/time.txt )
			echo -e "CCU ($1): Current time: ${time// Y /} (Format: YYYYMMDDhhmmss)\nEnter nothing to cancel"
			read -p "CCU ($1): Enter the new time (With the format above): " newtime
		fi

		if [[ $newtime ]]; then
			echo "${newtime} Y " > "$path/time.txt" # Add the weird Y byte
			echo "CCU ($1): Time set to $newtime succesfully"
		else
			echo "CCU ($1): TimeEdit canceled"
		fi
	;;
	# Help menu this is self explainatory
	--help|-h|*) cat <<EOD
    ___       ___       ___   
   /\  \     /\  \     /\__\  
  /::\  \   /::\  \   /:/ _/_ 
 /:/\:\__\ /:/\:\__\ /:/_/\__\

 \:\ \/__/ \:\ \/__/ \:\/:/  /
  \:\__\    \:\__\    \::/  / 
   \/__/     \/__/     \/__/  By Sophie :3

CCU (Chinese Camera Utility):
	[-e[v] [-iva] OUTPUT_DIR | -d[v]] Extract or Delete files from the camera
		-v	Vanquish (Hide extra messages)
		Flags:
			-i	Flag to include Images
			-v	Flag to include Videos
			-a	Flag to include Audio
	-t	Edit the camera timestamp
		<timestamp>	Edit the timestamp instantly
	-h	Show this help message

CCU Example Commands:
	ccu -e -iva ~/Pictures/ccu	Extract all media to a folder
	ccu -e -v	Delete all Video files :(
	ccu -t	Open the TimeEdit prompt
	ccu -t 20260101225400	Instantly edit the time to 2026/01/01 22:54:00
EOD
	;;
esac

exit 0 # It works