#!/bin/bash

if [[ -e $HOME/.settings-rc ]]; then
	. $HOME/.settings-rc
fi

lscdopts () {

	whiptail --title "lscd as default" --yesno "\`lscd' is a replacement for \`cd', which immediately displays
the content of the directory gone into.

Do you want to use \`lscd' rather than \`cd' by default?" 12 85

	if [[ $? == "0" ]]; then
		echo alias cd=lscd >> $HOME/.settings-rc
	fi

	whiptail --title "lscd options" --yesno "Regardless of your previous choise \`lscd' does not pass any options to
it's ls call. You may define some for better results, eg: \`--color=auto'.

Do you want to give \`lscd' any ls-options?" 12 85

	if [[ $? == "0" ]]; then
		xlscdopts=$(whiptail --title "Set LSCD_OPTS" --inputbox "Enter desired lscd options:" 12 85 "$LSCD_OPTS" 3>&1 1>&2 2>&3)
		echo LSCD_OPTS=\" $xlscdopts\" >> $HOME/.settings-rc
	fi

}

cdwriter () {

	whiptail --title "writeiso/geniso device" --yesno "The \`writeiso' and \`geniso' functions need to know what device to access.

By default /dev/dvdrw is beeing used, do you want to choose a different device?" 12 85

	if [[ $? == "0" ]]; then
		xcdwriter=$(whiptail --title "set CD_WRITER" --inputbox "Enter the devicenode of your CD/DVD-Writer:" 12 85 "$CD_WRITER" 3>&1 1>&2 2>&3)
		echo CD_WRITER=\"$xcdwriter\" >> $HOME/.settings-rc
	fi

	whiptail --title "geniso error tolerance" --yesno "By default \`geniso' stops at the first read error.

Do you want \`geniso' to continue anyways? [recommended]" 12 85

	if [[ $? == "0" ]]; then
		echo DD_OPTS=\"conv=noerror\" >> $HOME/.settings-rc
	fi

}

git_setup () {

	whiptail --title "git setup" --yesno "Do you want to setup GIT?

You can set the default username and email to use, or wether to have colored output." 12 85

	if [[ $? == "0" ]]; then
		gituser=$(whiptail --title "git user name" --inputbox "Enter your name to be used by GIT:" 12 85 "$GIT_USER_NAME" 3>&1 1>&2 2>&3)
		git config --global user.name "$gituser"
		echo GIT_USER_NAME=\"$gituser\" >> $HOME/.settings-rc

		gitmail=$(whiptail --title "git email" --inputbox "Enter the default email to be used by GIT:" 12 85 "$GIT_USER_EMAIL" 3>&1 1>&2 2>&3)
		git config --global user.email "$gitmail"
		echo GIT_USER_EMAIL=\"$gitmail\" >> $HOME/.settings-rc

		whiptail --title "git key" --yesno "Do you want to set a default key for signing?" 12 85

		if [[ $? == "0" ]]; then
			gitkey=$(whiptail --title "default git signing key" --inputbox "Enter the default key for signing tags & Co for GIT:

eg: supermario@bign.com" 12 85 "$GIT_USER_SIGNINGKEY" 3>&1 1>&2 2>&3)
			git config --global user.signingkey "$gitkey"
			echo GIT_USER_SIGNINGKEY=\"$gitkey\" >> $HOME/.settings-rc
		fi

		whiptail --title "git color" --yesno "Do you want colored output from GIT?" 12 85

		if [[ $? == "0" ]]; then
			git config --global color.branch true
			git config --global color.diff true
			git config --global color.interactive true
			git config --global color.pager true
			git config --global color.status true
			echo GIT_COLOR=\"true\" >> $HOME/.settings-rc
		fi

		whiptail --title "git.gnome.org" --yesno "Do you have a GNOME GIT account?

You will be asked to tell it in the next step.
With this info get_gnome can clone GIT repos with write access." 12 85

		if [[ $? == "0" ]]; then
			get_gnome_user=$(whiptail --title "git.gnome.org account" --inputbox "What's your GNOME GIT Account?:" 12 85 "$GIT_USER_GNOME" 3>&1 1>&2 2>&3)
			echo GIT_USER_GNOME=\"$get_gnome_user\" >> $HOME/.settings-rc
		fi

		whiptail --title "git.xfce.org" --yesno "Do you have a XFCE GIT account?

You will be asked to tell it in the next step.
With this info get_xfce can clone GIT repos with write access." 12 85

		if [[ $? == "0" ]]; then
			get_xfce_user=$(whiptail --title "git.xfce.org account" --inputbox "What's your XFCE GIT Account?:" 12 85 "$GIT_USER_XFCE" 3>&1 1>&2 2>&3)
			echo GIT_USER_XFCE=\"$get_xfce_user\" >> $HOME/.settings-rc
		fi

		giteditor=$(whiptail --title "editor for git" --inputbox "What editor to use for GIT commit messages & Co?:" 12 85 "$GIT_EDITOR" 3>&1 1>&2 2>&3)
		git config --global core.editor "$giteditor"
		echo GIT_EDITOR=\"$giteditor\" >> $HOME/.settings-rc

		whiptail --title "svn compat" --yesno "Do you want some aliases to be defined?
git co [git checkout]
git up [git pull]
git re [git reset --hard HEAD]
git ma [git checkout master]
git who [git shortlog -s --]" 12 85

		if [[ $? == "0" ]]; then
			git config --global alias.co checkout
			git config --global alias.up pull
			git config --global alias.re "reset --hard HEAD"
			git config --global alias.ma "checkout master"
			git config --global alias.who "shortlog -s --"
			echo GIT_ALIASES=\"true\" >> $HOME/.settings-rc
		fi

	fi

}

svn_setup () {

	whiptail --title "svn setup" --yesno "Do you want to setup SVN?

You can set the default username for KDE/Enlightenment accounts." 12 85

	if [[ $? == "0" ]]; then

		whiptail --title "svn.kde.org" --yesno "Do you have a KDE SVN account?

You will be asked to tell it in the next step.
With this info get_kde can checkout SVN repos with write access." 12 85

		if [[ $? == "0" ]]; then
			get_kde_user=$(whiptail --title "svn.kde.org account" --inputbox "What's your KDE SVN Account?:" 12 85 "$SVN_USER_KDE" 3>&1 1>&2 2>&3)
			echo SVN_USER_KDE=\"$get_kde_user\" >> $HOME/.settings-rc
		fi

		whiptail --title "svn.enlightenment.org" --yesno "Do you have an Enlightenment SVN account?

You will be asked to tell it in the next step.
With this info get_e can checkout SVN repos with write access." 12 85

		if [[ $? == "0" ]]; then
			get_e_user=$(whiptail --title "svn.enlightenment.org account" --inputbox "What's your Enlightenment SVN Account?:" 12 85 "$SVN_USER_ENLIGHTENMENT" 3>&1 1>&2 2>&3)
			echo SVN_USER_ENLIGHTENMENT=\"$get_e_user\" >> $HOME/.settings-rc
		fi

	fi

}

repo_info () {

	whiptail --title "reprepro setup" --yesno "Do you want to setup reprepro?

If you have an apt repo managed by reprepro, you can setup the repoadd helper script now." 12 85

	if [[ $? == "0" ]]; then
		repo_home=$(whiptail --title "reprepro repo-home" --inputbox "Enter the full path to the repo base directory:" 12 85 "$REPO_HOME" 3>&1 1>&2 2>&3)
		echo REPO_HOME=\"$repo_home\" >> $HOME/.settings-rc

		repo_dist=$(whiptail --title "reprepro repo-dist" --inputbox "Enter the name of the default dist:" 12 85 "$REPO_DIST" 3>&1 1>&2 2>&3)
		echo REPO_DIST=\"$repo_dist\" >> $HOME/.settings-rc

	fi

}

run_wizard () {

	whiptail --title "BashStyle-NG $BSNG_VERSION First-Run-Wizard" --msgbox "This wizard will guide you through fully setting up your copy
of BashStyle-NG to be able to make use of all shipped functions." 12 85

	whiptail --title "verbosity" --yesno "Do you want to disable the text flying by, when starting bash?" 12 85

	if [[ $? == "0" ]]; then
		gconftool-2 -t bool -s /apps/bashstyle/debug_verbose false
	fi

	whiptail --title "restore" --yesno "Do you want to restore the last working directory when starting bash?" 12 85

	if [[ $? == "0" ]]; then
		gconftool-2 -t bool -s /apps/bashstyle/restorepwd true
	fi

	lscdopts
	cdwriter
	git_setup
	svn_setup
	repo_info

	whiptail --title "BashStyle-NG $BSNG_VERSION First-Run-Wizard" --msgbox "You just finished generating your configuration.
You can find it in $HOME/.settings-rc.

You may re-run this wizard via \`bs-ng-wizard --force'.

To see a list of usefull tips for the bash, run \`bashtips'.
Have Fun!" 12 85

}

if [[ ! -e $HOME/.settings-rc || $1 == "--force" ]]; then
	rm -f $HOME/.settings-rc
	run_wizard
else	exit 0
fi
