#!/usr/bin/env bash

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

# Description:
# This script returns the current status of the git repository.

# 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"

set_flags() {
	while [ $# -gt 0 ]; do
		case "$1" in
		-h | --help)
			echo "g-s - show git status"
			echo " "
			echo "g-s [options]"
			echo " "
			echo "options:"
			echo "-h, --help          show brief help"
			exit 0
			;;
		*)
			echo ""
			echo "${RED}Unknown option:${NC} $1"
			exit 1
			;;
		esac
		shift
	done
}

main() {
	set_flags "$@"
	validate_dependencies git figlet lolcat
	print_banner
	print_message "${BLUE}Fetching git status...${NC}" 1

	local current_branch
	get_current_branch current_branch

	if ! git -c color.ui=always status 2>&1 | indent; then
		print_message "${PROMPT}Failed to get git status. Are you in a git repository?${NC}"
		return 1
	else
		print_message "${GREEN}Git status fetched successfully.${NC}" 0
		return 0
	fi
}


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