#!/usr/bin/env bash

# Author: Santhosh Siva
# Date Created: 03-08-2025

# Description:
# A script to clear your working directory by stashing changes.
# The stashed changes is tagged with the current date and time.

# Resolve script directory (works with symlinks for npm global install)
SOURCE="${BASH_SOURCE[0]}"
while [ -L "$SOURCE" ]; do
  DIR="$(cd -P "$(dirname "$SOURCE")" && pwd)"
  SOURCE="$(readlink "$SOURCE")"
  [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
done
SCRIPT_DIR="$(cd -P "$(dirname "$SOURCE")" && pwd)"
source "$SCRIPT_DIR/utils"

# Default Values
tag_message=

set_flags() {
	while [ $# -gt 0 ]; do
		case "$1" in
		-h | --help)
			echo "g-rmf - clear your working directory by stashing changes"
			echo " "
			echo "g-rmf [options]"
			echo " "
			echo "options:"
			echo "-h, --help                                                  show brief help"
			echo "--message MESSAGE, --message=MESSAGE, -m=MESSAGE, -m MESSAGE   specify custom message for stash"
			exit 0
			;;
		-m=* | --message=*)
			tag_message="${1#*=}"
			;;
		-m | --message)
			shift
			if [ $# -gt 0 ]; then
				tag_message="$1"
			else
				echo ""
				echo "${RED}Error: No message specified.$NC"
				exit 1
			fi
			;;
		*)
			echo "${RED}Unknown option:${NC} $1"
			exit
			;;
		esac
		shift
	done
}

main() {
	set_flags "$@"
	validate_dependencies git figlet lolcat
	print_banner
	stash_changes true 1 "$tag_message"
	sync_submodules 2
}


# Only run main if script is executed directly (not sourced)
if [ "${BASH_SOURCE[0]}" = "${0}" ]; then
	main "$@"
	exit 0
fi
