#!/bin/sh -ef
# Usage: Sisyphus-setup
#
# Interactively sets up all settings required to work with Sisyphus.

[ -n "$COMFORT_DIR" ] || COMFORT_DIR=/usr/share/comfort
. "$COMFORT_DIR/functions"

# ======================================================================

USAGE=''
DESCRIPTION="
A wizard to set up everything in user environment to develop and
maintain Sisyphus packages."

# Parse command line
. "$COMFORT_DIR/getopt"
[ "$#" -eq 0 ] || show_usage 'Too many arguments'

# ======================================================================
# Formatting functions

. /etc/rc.d/init.d/outformat

POINT_PASS=0
POINT_FAIL=0

echo_pass()
{
	MOVE_TO_COL
	echo -n '[ '
	SETCOLOR_SUCCESS
	echo -n 'PASS'
	SETCOLOR_NORMAL
	echo ' ]'
	POINT_PASS=$(($POINT_PASS+1))
}

echo_fail()
{
	MOVE_TO_COL
	echo -n '[ '
	SETCOLOR_FAILURE
	echo -n 'FAIL'
	SETCOLOR_NORMAL
	echo ' ]'
	POINT_FAIL=$(($POINT_FAIL+1))
}

# ======================================================================
# Misc functions

exit_handler()
{
	local rc=$?
	trap - EXIT
	[ -f "$LOGFILE" ] && rm -f "$LOGFILE"
	[ -f "$TODOFILE" ] && rm -f "$TODOFILE"
	exit $rc
}
trap exit_handler HUP PIPE INT QUIT TERM EXIT

yes_no()
{
	local answer;
	while true; do
		read -p "$1 (y/n)? " answer
		answer=`echo "$answer" | tr A-Z a-z | sed 's/^\(.\).*$/\1/'`
		[ "$answer" == "y" ] || [ "$answer" == "yes" ] && return 0
		[ "$answer" == "n" ] || [ "$answer" == "no" ] && return 1
	done
}

# ======================================================================

# user config:
#
# ~/.ssh/config: git.alt, incoming.alt
# mkdir -p ~/git ~/hasher
# confirm all options in ~/.Sisyphusrc
# add variables to .bash_profile: GIT_COMMITTER_*, GIT_AUTHOR_*
# generate ~/.rpmmacros
#
# root config:
#
# apt sources.list
# hasher-useradd
# tmpfs / ramdisk on ~/hasher

check_config()
{
	LOGFILE=`mktemp -t`
	TODOFILE=`mktemp -t`
	echo 'Checking current configuration...'

	echo -n 'ssh private key'
	if [ -f "$ALT_SSH_KEY" ]; then
		echo_pass
	else
		echo_fail
		echo 'Provide ssh private key in ~/.ssh/id_dsa' >>$LOGFILE
	fi

	echo -n 'ssh config for git.alt'
	if [ -r "$HOME/.ssh/config" ] && grep -qe 'Host[[:space:]]*git.alt' $HOME/.ssh/config; then
		echo_pass
	else
		echo_fail
		echo 'Add git.alt to ~/.ssh/config' >>$LOGFILE
		echo ssh-git >>$TODOFILE
	fi

	echo -n 'ssh config for incoming.alt'
	if [ -r "$HOME/.ssh/config" ] && grep -qe 'Host[[:space:]]*incoming.alt' ~/.ssh/config; then
		echo_pass
	else
		echo_fail
		echo 'Add incoming.alt to ~/.ssh/config' >>$LOGFILE
		echo ssh-incoming >>$TODOFILE
	fi

	echo -n "BUILD_DIR ($BUILD_DIR)"
	if [ -d "$BUILD_DIR" ]; then
		echo_pass
	else
		echo_fail
		echo 'Make directory $BUILD_DIR (BUILD_DIR)' >>$LOGFILE
		echo build-dir >>$TODOFILE
	fi

	echo -n "Hasher setup for user $USER"
	if true; then # GREYFIX
		echo_pass
	else
		echo_fail
		echo 'Add $USER to hasher' >>$LOGFILE
	fi

	echo -n "GIT_HOME_DIR ($GIT_HOME_DIR)"
	if [ -d "$GIT_HOME_DIR" ]; then
		echo_pass
	else
		echo_fail
		echo 'Make directory $GIT_HOME_DIR (GIT_HOME_DIR)' >>$LOGFILE
		echo git-home-dir >>$TODOFILE
	fi

	echo -n "GIT_AUTHOR_NAME"
	if [ -z "$GIT_AUTHOR_NAME" ]; then
		echo_pass
	else
		echo_fail
		echo 'Remove GIT_AUTHOR_NAME environment variable' >>$LOGFILE
	fi

	echo -n "GIT_AUTHOR_EMAIL"
	if [ -z "$GIT_AUTHOR_EMAIL" ]; then
		echo_pass
	else
		echo_fail
		echo 'Remove GIT_AUTHOR_EMAIL environment variable' >>$LOGFILE
	fi

	echo -n "GIT_COMMITTER_NAME"
	if [ -z "$GIT_COMMITTER_NAME" ]; then
		echo_pass
	else
		echo_fail
		echo 'Remove GIT_COMMITTER_NAME environment variable' >>$LOGFILE
	fi

	echo -n "GIT_COMMITTER_EMAIL"
	if [ -z "$GIT_COMMITTER_EMAIL" ]; then
		echo_pass
	else
		echo_fail
		echo 'Remove GIT_COMMITTER_EMAIL environment variable' >>$LOGFILE
	fi

	echo -n "Global ~/.gitconfig user.name"
	if git-repo-config --global --get user.name >/dev/null; then
		echo_pass
	else
		echo_fail
		echo 'Add user.name to global ~/.gitconfig' >>$LOGFILE
		echo 'gitconfig-user-name' >>$TODOFILE
	fi

	echo -n "Global ~/.gitconfig user.email"
	GIT_USER_EMAIL=`git-repo-config --global --get user.email || true`
	if [ -z "$GIT_USER_EMAIL" ]; then
		echo_fail
		echo 'Add user.email to global ~/.gitconfig' >>$LOGFILE
		echo 'gitconfig-user-email' >>$TODOFILE
	elif [ "$GIT_USER_EMAIL" != "$ALT_USER_EMAIL" ]; then
		echo_fail
		echo "Change user.email to '$ALT_USER_EMAIL' in global ~/.gitconfig" >>$LOGFILE
		echo 'gitconfig-user-email' >>$TODOFILE
	else
		echo_pass
	fi

	echo -n "User-wide ~/.Sisyphusrc config"
	if [ -r "$HOME/.Sisyphusrc" ]; then
		echo_pass
	else
		echo_fail
		echo 'Create user-wide ~/.Sisyphusrc config' >>$LOGFILE
		echo 'sisyphusrc' >>$TODOFILE
	fi
}

fix_config()
{
	while [ -n "$1" ]; do
		echo "Fixing: $1"
		case "$1" in
		sisyphusrc)
			cp -i $COMFORT_DIR/default/Sisyphusrc $HOME/.Sisyphusrc
			;;
		gitconfig-user-name)
			git-repo-config --global user.name "$ALT_USER_NAME"
			;;
		gitconfig-user-email)
			git-repo-config --global user.email "$ALT_USER_EMAIL"
			;;
		ssh-incoming)
			mkdir -p $HOME/.ssh
			cat >>$HOME/.ssh/config <<EOF

Host	incoming.alt
	HostName cvs.altlinux.org
	User in_${ALT_USER}
	Protocol 2
	ForwardX11 no
	ForwardAgent no
	Compression no
EOF
			;;
		ssh-git)
			mkdir -p $HOME/.ssh
			cat >>$HOME/.ssh/config <<EOF

Host    git.alt
        HostName git.altlinux.org
        Port 222
        User git_${ALT_USER}
EOF
			;;
		build-dir)
			mkdir -p $BUILD_DIR
			;;
		git-home-dir)
			mkdir -p $GIT_HOME_DIR
			;;
		*)
			Fatal "Internal error - unknown TODO found - \"$1\""
			;;
		esac
		shift
	done
}

print_config()
{
	cat <<EOF
Current configuration:

Setting                    Env. variable  Value
-------------------------- -------------  ------------------------------
UNIX Username              USER           $USER
ALT username               ALT_USER       $ALT_USER
GIT repositories dir       GIT_HOME_DIR   $GIT_HOME_DIR
Build server to use        BUILD_SERVER   $BUILD_SERVER
Hasher build dir           BUILD_DIR      $BUILD_DIR
Architecture to build for  ARCH           $ARCH
SSH key                    ALT_SSH_KEY    $ALT_SSH_KEY

GPG name                   %_gpg_name
RPM default packager       %packager

EOF
}

# Print current configuration
cat <<EOF

========================================================================
Welcome to Sisyphus comfort!

This wizard will help setting everything up to develop and maintain
packages in Sisyphus.
========================================================================

EOF

check_config

cat <<EOF

========================================================================
Tests passed: $POINT_PASS / $(($POINT_PASS+$POINT_FAIL))

EOF

if [ "$POINT_FAIL" -eq 0 ]; then
	echo "Congratulations! Everything is ready to work with Sisyphus!"
else
	echo 'Summary of what you have to do:'
	echo
	cat "$LOGFILE"
	yes_no 'Do you want to get it set up automatically' && fix_config `cat $TODOFILE`
fi
