#!/bin/bash
#########################################################
# 							#
# This is BashStyle-NG  				#
#							#
# Licensed under GNU GENERAL PUBLIC LICENSE v3    	#
#							#
# Copyright 2007 - 2011 Christopher Bratusek		#
#							#
#########################################################
. gettext.sh
TEXTDOMAIN="nx-rc"

if [[ $0 == *gdm* || $0 == *kdm* ]]; then
	dbg_msg $"BashStyle-NG: Started from GDM -- exiting"
else

export BSNG_RUNNING="true"

touch $HOME/.bashstyle.lock

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

ERR_MSG=""

BSNG_INTERN=( internal/misc internal/gconf internal/logging )
BSNG_FUNCS=( functions/admin functions/conversion functions/infos
             functions/media functions/misc functions/random
	     functions/utils functions/versioning )

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

if [[ $verbose != true ]]; then

	spin(){

		until [ ! -e $HOME/.bashstyle.lock ]
		do
			echo -ne "l      \b\b\b\b\b\b\b";sleep .08;
			echo -ne "lo     \b\b\b\b\b\b\b";sleep .08;
			echo -ne "loa    \b\b\b\b\b\b\b";sleep .08;
			echo -ne "load   \b\b\b\b\b\b\b";sleep .08;
			echo -ne "loadi  \b\b\b\b\b\b\b";sleep .08;
			echo -ne "loadin \b\b\b\b\b\b\b";sleep .08;
			echo -ne "loading\b\b\b\b\b\b\b";sleep .08;
			echo -ne " oading\b\b\b\b\b\b\b";sleep .08;
			echo -ne "  ading\b\b\b\b\b\b\b";sleep .08;
			echo -ne "   ding\b\b\b\b\b\b\b";sleep .08;
			echo -ne "    ing\b\b\b\b\b\b\b";sleep .08;
			echo -ne "     ng\b\b\b\b\b\b\b";sleep .08;
			echo -ne "      g\b\b\b\b\b\b\b";sleep .08;
		done

	}

	spin & 2>&1>/dev/null
fi

export BSNG_LOGFILE=$HOME/.bashstyle.log
dbg_log rm -f $BSNG_LOGFILE
dbg_log touch $BSNG_LOGFILE

dbg_log source $BSNG_RC_DIR/styles/null-rc

dbg_msg $"BashStyle-NG Starting"
dbg_msg $"BashStyle-NG Bash Major Version:" $(echo ${BASH_VERSINFO[0]})
dbg_msg $"BashStyle-NG Bash Minor Version:" $(echo ${BASH_VERSINFO[1]})
dbg_msg $"BashStyle-NG Bash Patch Level:" $(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
dbg_log alias reload="source ~/.bashrc"

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

if [[ $(get_key readline/use_readlinecfg) == true ]]; then \
	dbg_msg $"BashStyle-NG Loading:" $"ReadlineCFG"
	dbg_log source $BSNG_RC_DIR/settings/readline-rc
fi

if [[ $(get_key vim/use_vimcfg) == true ]]; then \
	dbg_msg $"BashStyle-NG Loading:" $"VimCFG"
	dbg_log source $BSNG_RC_DIR/settings/vim-rc
fi

if [[ $(get_key nano/use_nanocfg) == true ]]; then \
	dbg_msg $"BashStyle-NG Loading:" $"NanoCFG"
	dbg_log source $BSNG_RC_DIR/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

if [[ $(get_key color/enable) == true ]]; then \
	dbg_log source $BSNG_RC_DIR/settings/color-rc
fi

if [[ $(get_key dircolors/enable) == true ]]; then \
	dbg_msg $"BashStyle-NG Loading:" $"LSCOLORS"
	dbg_log source $BSNG_RC_DIR/settings/ls-rc
fi

dbg_log source $BSNG_RC_DIR/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 \
	dbg_log source $BSNG_RC_DIR/settings/man-rc
fi

dbg_log source $BSNG_RC_DIR/settings/options-rc

dbg_log source $BSNG_RC_DIR/settings/shopt-rc

shopt -s checkwinsize

if [[ -e $HOME/.bashstyle.wizard ]]; then \
	dbg_msg $"BashStyle-NG Loading:" $"Fist-Run-Wizard Settings"
	dbg_log source $HOME/.bashstyle.wizard
	if [[ $enabcol != true ]]; then
		LSCD_OPTS+=" --color=no"
	fi
fi

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

if [[ $(get_key random_style) == true ]]; then \
	dbg_msg $"BashStyle-NG Setting:" $"Style Random"
	dbg_log source $BSNG_RC_DIR/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"
	dbg_log source $BSNG_RC_DIR/styles/custom-rc
elif [[ -e $BSNG_RC_DIR/styles/${pstyle}-rc ]]; then
	dbg_msg $"BashStyle-NG Setting:" $"Style" $pstyle
	dbg_log source $BSNG_RC_DIR/styles/${pstyle}-rc
else
	dbg_msg $"BashStyle-NG Setting:" $"Style Fallback"
	dbg_log source $BSNG_RC_DIR/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 color/shell) == true && $enabcol == true ]]; then
	dbg_msg $"BashStyle-NG Starting:" $"ColorShell"
	dbg_log source $BSNG_RC_DIR/functions/colorshell
fi

if [[ $(get_key tabrotate) == true ]]; then \
	dbg_msg $"BashStyle-NG Setting:" $"Tab-Rotation" $"On"
	dbg_log bind 'TAB:menu-complete'
	dbg_log 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 \
	dbg_log trap 'printf %s "$PWD" > $HOME/.lastpwd' EXIT
	if [[ -e $HOME/.lastpwd && -d "$(cat $HOME/.lastpwd)" ]]; then \
		dbg_log cd "$(cat $HOME/.lastpwd)"
	fi
fi

if [[ $(which bs-ng-wizard 2>/dev/null) == "" ]]; then
	export PATH="@PREFIX@/share/bashstyle-ng/rc/bin/:$PATH"
fi

if [[ -e $HOME/.bookmarks ]]; then
	dbg_msg $"BashStyle-NG Loading:" $"Bookmarks"
	dbg_log . $HOME/.bookmarks
else	touch $HOME/.bookmarks
fi

rm $HOME/.bashstyle.lock

if [[ $verbose != true ]]; then
	sleep 1.5
fi

if [[ $USER_BIRTHDAY == $(date +%m-%d) && ! -e ~/.bday ]]; then
	cat $BSNG_RC_DIR/ascii/birthday
elif [[ $WelcomeMessage != "" ]]; then \
	dbg_msg $"BashStyle-NG Setting:" $"Welcome Message"
	dbg_log $WelcomeMessage
fi

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

bs-ng-wizard
dbg_finalize
unset BSNG_RUNNING

fi
