#!/bin/bash
#########################################################
# 							#
# This is BashStyle-NG  				#
#							#
# Licensed under GNU GENERAL PUBLIC LICENSE v3    	#
#							#
# Copyright 2007 - 2010 Christopher Bratusek		#
#							#
#########################################################

if [[ $0 == *gdm* ]]; then
	dbg_msg BashStyle-NG: Started during GDM-Session-Init -- exiting
else

export BSNG_GCONF_READ=1

export BSNG_PREFIX=@PREFIX@
export BSNG_RC_DIR=$BSNG_PREFIX/share/bashstyle-ng/rc/
export BSNG_LOCKFILE=$HOME/.bashstyle.lock
export BSNG_VERSION=@VERSION@
export BSNG_DOCS=$BSNG_PREFIX/share/doc/bashstyle-ng/index.html

ERR_MSG=""

BSNG_FUNCS=(internal admin conversion dirinfo infos media misc random utils versioning)

for RC in ${BSNG_FUNCS[@]}; do
	source $BSNG_RC_DIR/functions/$RC
done

if [[ $(get_key debug) == true ]]; then \
	eval BSNG_LOGFILE=$(get_key debug_log)
	rm -f $BSNG_LOGFILE
	touch $BSNG_LOGFILE
fi

source $BSNG_RC_DIR/styles/null-rc

dbg_msg BashStyle-NG Starting
dbg_msg BashStyle-NG Bash Major: $(echo ${BASH_VERSINFO[0]})
dbg_msg BashStyle-NG Bash Minor: $(echo ${BASH_VERSINFO[1]})
dbg_msg BashStyle-NG Bash Patch: $(echo ${BASH_VERSINFO[2]})
dbg_msg BashStyle-NG Version: $BSNG_VERSION
dbg_msg BashStyle-NG Prefix: $BSNG_PREFIX
dbg_msg BashStyle-NG Debugging: Enabled
dbg_msg BashStyle-NG Logfile: $BSNG_LOGFILE

dbg_msg BashStyle-NG Setting: Alias reload=source ~/.bashrc
alias reload="source ~/.bashrc"

dbg_msg BashStyle-NG Setting: Alias rd=pwd -P
alias rd="pwd -P"

if [[ $(get_key readline/use_readlinecfg) == true ]]; then \
	dbg_msg BashStyle-NG Loading: ReadlineCFG
	source $BSNG_PREFIX/share/bashstyle-ng/rc/settings/readline-rc
fi

if [[ $(get_key vim/use_vimcfg) == true ]]; then \
	dbg_msg BashStyle-NG Loading: VimCFG
	source $BSNG_PREFIX/share/bashstyle-ng/rc/settings/vim-rc
fi

if [[ $(get_key nano/use_nanocfg) == true ]]; then \
	dbg_msg BashStyle-NG Loading: NanoCFG
	source $BSNG_PREFIX/share/bashstyle-ng/rc/settings/nano-rc
fi

if [[ $(get_key use_bashstyle) == false ]]; then \
	dbg_msg BashStyle-NG: Exiting
else
	dbg_msg BashStyle-NG Loading: BashStyle-NG
fi

fi

if [[ $(get_key color/enable) == true ]]; then \
	source $BSNG_PREFIX/share/bashstyle-ng/rc/settings/color-rc
fi

if [[ $(get_key dircolors/enable) == true ]]; then \
	dbg_msg BashStyle-NG Loading: LSCOLORS
	source $BSNG_PREFIX/share/bashstyle-ng/rc/settings/ls-rc
fi

source $BSNG_PREFIX/share/bashstyle-ng/rc/settings/alias-rc

dbg_msg BashStyle-NG Setting: ps2 ps3 ps4 char $(get_key ps234)
ps234char=$(get_key ps234)

if [[ $(get_key colored_man) == true ]]; then \
	source $BSNG_PREFIX/share/bashstyle-ng/rc/settings/man-rc
fi

source $BSNG_PREFIX/share/bashstyle-ng/rc/settings/options-rc

source $BSNG_PREFIX/share/bashstyle-ng/rc/settings/shopt-rc

shopt -s checkwinsize

if [[ -e $HOME/.settings-rc ]]; then \
	dbg_msg BashStyle-NG Loading: Fist-Run-Wizard Settings
	source $HOME/.settings-rc
fi

if [[ -e $HOME/.custom-rc ]]; then \
	dbg_msg BashStyle-NG Loading: Custom Settings
	source $HOME/.custom-rc
fi

if [[ $(get_key random_style) == true ]]; then \
	dbg_msg BashStyle-NG Setting: Style Random
	source $BSNG_PREFIX/share/bashstyle-ng/rc/settings/random-rc
else
	pstyle=$(get_key prompt_style)
fi

use_custom_prompt=$(get_key use_custom_prompt)

if [[ $use_custom_prompt == true ]]; then \
	dbg_msg BashStyle-NG Setting: Style Custom
	source $BSNG_PREFIX/share/bashstyle-ng/rc/styles/custom-rc
elif [[ -e $BSNG_PREFIX/share/bashstyle-ng/rc/styles/${pstyle}-rc ]]; then
	dbg_msg BashStyle-NG Setting: Style $pstyle
	source $BSNG_PREFIX/share/bashstyle-ng/rc/styles/${pstyle}-rc
else
	dbg_msg BashStyle-NG Setting: Style Fallback
	source $BSNG_PREFIX/share/bashstyle-ng/rc/styles/fallback-rc
fi

dbg_msg BashStyle-NG Setting: PS2 PS3 PS4
PS2="$pscolor $ps234char$fontcolor "
PS3="$pscolor $ps234char$ps234char$fontcolor "
PS4="$pscolor $ps234char$ps234char$ps234char$fontcolor "

if [[ $(get_key tabrotate) == true ]]; then \
	dbg_msg BashStyle-NG Setting: Tab-Rotation On
	bind 'TAB:menu-complete'
	complete -d -X '.[a-zA-Z0-9]*' cd
else	dbg_msg BashStyle-NG Setting: Tab-Rotation Off
fi

WelcomeMessage=$(get_key welcome_message)

if [[ $(get_key restorepwd) == true ]]; then \
	trap 'printf %s "$PWD" > $HOME/.lastpwd' EXIT
	if [[ -e $HOME/.lastpwd && -d "$(cat $HOME/.lastpwd)" ]]; then \
		cd "$(cat $HOME/.lastpwd)"
	fi
fi

if [[ $WelcomeMessage != "" ]]; then \
	dbg_msg BashStyle-NG Setting: Welcome Message
	clear
	$WelcomeMessage
fi

if [[ $ERR_MSG != "" ]]; then
	echo -e "${eyellow}\n\nImportant informations:"
	echo -e "${eorange}${ERR_MSG}\n"
fi

unset BSNG_GCONF_READ

if [[ ! -e $HOME/.settings-rc ]]; then
	bs-ng-wizard
	reload
fi

if [[ -e $HOME/.bookmarks ]]; then
	. $HOME/.bookmarks
else	touch $HOME/.bookmarks
fi
