#!/bin/bash
##################################################################################
## Bashish, a console theme engine
## Copyright (C) 2005 Thomas Eriksson
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
##################################################################################
##
## this file is run the first time bashish is launched by a user
##
## it will create the $HOME/.bashish dir and modify an appropiate rc-file 
## corresponding to the shell set in /etc/passwd
##
###############################################################################

function _bashish_configurehandler
{
test "x$BACTITLE" = x && BACKTITLE="Bashish shell configuration"
TITLE="$BACKTITLE"
which dialog 1>/dev/null 2>/dev/null||errorhandler 5

## whiz bang autodetection code
DEFAULT=bash
for TEST_SHELL in bkiss gkiss rkiss kiss osh bash zsh dash posh dtksh tksh ash sh ksh pdksh ksh93 csh tcsh fish rc es akanga lsh
do
	test ${TEST_SHELL##*/} = ${SHELL##*/} && DEFAULT=${TEST_SHELL##*/}
done
case ${DEFAULT##*/} in
bkiss|gkiss|rkiss) DEFAULT=kiss;;
dash|posh|ksh|pdksh|dtksh|tksh|ksh93) DEFAULT=sh;;
akanga|es) DEFAULT=rc;;
*) DEFAULT=bash;;
esac

## this list is semi-sorted
## it's sorted on popularity (own made-up-statistics) and to some extent relation
dialog  --backtitle "$BACKTITLE" --title "$TITLE" --default-item "$DEFAULT" --menu "Select your primary shell from the list below. Brackets specify the startup-file Bashish will alter." 0 0 0 bash "GNU Bash [~/.bashrc]" zsh "Z Shell [~/.zshrc]" sh "Bourne shell compatible [${ENV-\~/.profile]}]" csh "Berkely C-Shell [~/.cshrc]" tcsh "Enhanced C-Shell [~/.tcshrc]" fish "Friendly Interactive SHell [~/.fish]" rc "Plan/9 shell [~/.rcrc]" lsh "DOS like shell [~/.autoexec]" osh "Old Thomson Shell [~/.oshrc]" kiss "keep it simple shell [~/.kissrc]" 2>$HOME/.bashish/tmp/dialogfe||{
	## clean up
	rm -rf $HOME/.bashish
	exit 1
}
## visual hack for dialog
printf "\033[44m"

read INPUT <$HOME/.bashish/tmp/dialogfe
rm $HOME/.bashish/tmp/dialogfe

_bashish_module sys/autohandler
autohandler ${INPUT}
if test -f "$BASHISHDIR/main/prompt/$AUTOSHELL/autoload";then
	test -f "$AUTORC"||{ TMP_AUTORC="${AUTORC#.}";test -f "/etc/${TMP_AUTORC##*/}"&& cp "/etc/${TMP_AUTORC##*/}" "${AUTORC}";}
	## bash specific test
	test -f "$AUTORC"||{ test -f "/etc/bash${AUTORC##*/}"&& cp "/etc/bash${AUTORC##*/}" "${AUTORC}";}
	test -f "$AUTORC"&&cat "$AUTORC"| grep -v '## line added by bashish' >"${AUTORC}.new"
	mv "$AUTORC.new" "${AUTORC}"
	cat "$BASHISHDIR/main/prompt/$AUTOSHELL/autoload" >>"$AUTORC"
else
	errorhandler 3
fi

}
