#!/bin/bash
#
# Setup a new account with the scripts.  To be run in the directory you want the scripts.
#
# Requires: git, sed
#

REPO="git://github.com/dang/scripts.git"
LNFILES="bashrc cvsrc profile gvimrc inputrc vim"
CPFILES="bash_local vimrc gitconfig"

function die {
	echo "$@"
	exit 1
}

function cmd_exists {
	which $1 > /dev/null 2>&1
	if [ "$?" == "1" ]; then
		die "You don't have $1 installed, sorry"
	fi
}

cmd_exists sed
cmd_exists git

if [ -z "${1}" ]; then
	echo "no directory given"
	die "usage: scripts-setup <directory>"
fi

if [ "$1" == "." ]; then
	SCRIPTDIR=${PWD}
else
	SCRIPTDIR=$1
fi

# Check for conflicts
for i in ${LNFILES} scripts; do
	[ -a "${HOME}/.${i}" ] && die "${HOME}/.${i} exists; not continuing"
done

if [ ! -d "${SCRIPTDIR}/.git" ]; then
	cd $(dirname "${SCRIPTDIR}")
	git clone ${REPO} $(basename ${SCRIPTDIR})
fi
if [ ! -d "${SCRIPTDIR}/.git" ]; then
	echo "Failed to clone scripts repo from ${REPO}"
	exit 2
fi

cd ${HOME}
# Set up .scripts symlink
ln -s ${SCRIPTDIR} .scripts

for FILE in ${CPFILES}; do
	[ -a ".${FILE}" ] || cp ".scripts/${FILE}" ".${FILE}"
done
for FILE in ${LNFILES}; do
	ln -s ".scripts/${FILE}" ".${FILE}"
done

echo "Done."
echo "You need to edit ${HOME}/.bash_local for your your local settings"
