#! /bin/bash

#Backup existing screens: changescr -b
#Change Screen: changescrn -c [SCREEN] -n [PATH TO NEW SCREEN]
#Restore original screens: changescrn -r [SCREEN] (use option 'all' to restore all screens at once)
#[SCREEN] options: batteryempty | lowbattery | overheating | poweroff | rebooting | recovery | splash | starting | suspended 

backup="/opt/usr/share/backupscrns"
source="/usr/share/remarkable"

while getopts "r: c: n: :b" opt; do
	case $opt in
		r) #Restores original screen
			case $OPTARG in 
				batteryempty | lowbattery | overheating | poweroff | rebooting | recovery | splash | starting | suspended)
					echo "restoring $OPTARG.png..."
					cp $backup/$OPTARG.png $source >&2
					echo "Done!"
					;;
				all)
					echo "Restoring all screens..."
					cp $backup/*.png $source
					echo "Done!"
					;;
				*)
					echo "Screen Options: batteryempty lowbattery overheating poweroff rebooting recovery splash starting suspended"
					exit 1
					;;
			esac	
			;;

		c) #Specifies which screen to change. Must be used in combination with -n
			SCREEN=$OPTARG
		   case $NEWSCRN in 
		   		"") echo "Please specify location of new screen" 
					echo "Usage: changescrn -c [SCREEN] -n [PATH TO NEW SCREEN]"
					;;
			esac
		   ;;

		n) #Specifies location of new screen.  Must be used in combination with -c
			NEWSCRN=$OPTARG
		   case $SCREEN in 
		   		batteryempty | lowbattery | overheating | poweroff | rebooting | recovery | splash | starting | suspended)
				cp $NEWSCRN $source/$SCREEN.png
				;;
    	     	*)
    	     	echo "You either didn't specify a screen option first or didn't specify a correct screen option"
    	     	echo "Usage: changescrn -c [SCREEN] -n [PATH TO NEW SCREEN]"
    	     	echo "[SCREEN] options: batteryempty | lowbattery | overheating | poweroff | rebooting | recovery | splash | starting | suspended"
    	     	;;
    	     esac
    	   
		   ;;	
		b) #Creates a backup of original screens
			mkdir -p $backup
			cp $source/*.png $backup/
		   ;;
		\?)
			echo "Invalid option: -$OPTARG" >&2
			exit 1
			;;
		:)
			echo "Option -$OPTARG requires and argument." >&2
			echo "Usage: changescrn -c [SCREEN] -n [PATH TO NEW SCREEN]"
			exit 1
			;;
	esac

	
done



