#!/bin/bash

##
## binaryDiv's dotfiles management script
##

# Define root dir for dotfiles management (defaults to $HOME, can be overridden
# with environment variables for debugging)
if [ -z "$DOTFILESROOT" ]; then
	DOTFILESROOT="$HOME"
fi

# Set variables
homedir="$DOTFILESROOT"
dotfilesdir="$DOTFILESROOT/.dotfiles"


# Check for command
if [ $# -eq 0 ]; then
	echo "no command given"
	exit 1
fi

# Fetch command
cmd=$1
shift


# Parse command
case "$cmd" in
	help)
		# help: print usage help
		echo "help: not implemented yet, sorry /o\\"
		exit 1
		;;

	ls)
		# ls: list dotfiles via ls
		echo "$dotfilesdir:"
		ls -lAh --color=auto "$dotfilesdir/"
		;;
	
	git)
		# git: do git commands (forwards all arguments)
		git -C "$dotfilesdir" "$@"
		;;
	
	createlinks)
		# createlinks: create or update links for dotfiles in home dir

		# (Use an extra script for this to separate the management script from
		# user specific dotfiles)
		if [ -f "$dotfilesdir/createlinks.sh" ]; then
			# arguments: home dir and .dotfiles dir
			bash "$dotfilesdir/createlinks.sh" "$homedir" "$dotfilesdir"
		else
			echo "no createlinks.sh found in $dotfilesdir"
			exit 1
		fi
		;;

	*)
		# unknown command
		echo "unknown command '$cmd'"
		exit 1
		;;
esac

