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

source $BSNG_PREFIX/share/bashstyle-ng/rc/functions/bs-ng-echo

white=$(bs_ng_echo $white)
green=$(bs_ng_echo $green)
orange=$(bs_ng_echo $orange)
iceblue=$(bs_ng_echo $iceblue)
green=$(bs_ng_echo $green)

source $BSNG_PREFIX/share/bashstyle-ng/rc/functions/gconf

dbg_msg()
{
	return 0
}

DATE="$(date +%Y-%m-%d_%I-%M)"

_generate_bashrc()

{
	export BSNG_GCONF_READ=1

	if [[ ! -e $HOME/bashstyle ]]; then
		mkdir -p $HOME/bashstyle
	fi

	echo "#!/bin/bash" > $HOME/bashstyle/bashrc.bs-ng-$DATE
	echo -e "#\n#This config file was generated by BashStyle-NG $BSNG_VERSION at $DATE\n#Use as \$HOME/.bashrc to apply settings\n#" \
		>> $HOME/bashstyle/bashrc.bs-ng-$DATE

	echo "export BSNG_PREFIX=\"$BSNG_PREFIX\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	echo "export BSNG_RC_DIR=\"$BSNG_PREFIX/share/bashstyle-ng/rc/functions/\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	echo "export BSNG_LOCKFILE=\"$HOME/.bashstyle.lock\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	echo "export BSNG_VERSION=\"$BSNG_VERSION\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	echo "export BSNG_GCONF_READ=1" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	echo "source \$BSNG_PREFIX/share/bashstyle-ng/rc/functions/gconf" >> $HOME/bashstyle/bashrc.bs-ng-$DATE

	echo "bsource () {

	for file in \$@; do
		source \$BSNG_PREFIX/share/bashstyle-ng/rc/functions/\$file
	done

}" >> $HOME/bashstyle/bashrc.bs-ng-$DATE

	echo "bsource gconf debug check_opt xunalias ls git exchange xmv xcp xchown xconv random" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	echo "bsource bs_ng_echo showuptime showmem showsystemload showcpuload man2pdf countfiles" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	echo "bsource countprocesses showbatteryload showip showsize showspace showtty systeminfos" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	echo "bsource truncpwd ps" >> $HOME/bashstyle/bashrc.bs-ng-$DATE

	if [[ $BASH_VERSINFO == 4 ]]; then
		echo "bsource xdel" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	fi

	if [[ $(get_key color/enable) == true ]]; then \

		colstyle=$(get_key color/style)

		if [[ $colstyle == bright ]]; then \
			style="01"
		elif [[ $colstyle == normal ]]; then \
			style="0"
		elif [[ $colstyle == inverted ]]; then \
			style="7"
		elif [[ $colstyle == dimmed ]]; then \
			style="02"
		elif [[ $colstyle == underlined ]]; then \
			style="04"
		fi

		echo "export enabcol=true" >> $HOME/bashstyle/bashrc.bs-ng-$DATE

		echo "if [[ $TERM != *xterm* ]]; then" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
		echo "	export enabcol=false" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
		echo "fi" >> $HOME/bashstyle/bashrc.bs-ng-$DATE

		echo "export black=\"\[\033[${style};38;5;0m\]\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
		echo "export red=\"\[\033[${style};38;5;1m\]\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
		echo "export green=\"\[\033[${style};38;5;2m\]\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
		echo "export yellow=\"\[\033[${style};38;5;3m\]\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
		echo "export blue=\"\[\033[${style};38;5;4m\]\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
		echo "export magenta=\"\[\033[${style};38;5;129m\]\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
		echo "export cyan=\"\[\033[${style};38;5;6m\]\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
		echo "export white=\"\[\033[${style};38;5;7m\]\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
		echo "export coldblue=\"\[\033[${style};38;5;33m\]\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
		echo "export smoothblue=\"\[\033[${style};38;5;111m\]\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
		echo "export iceblue=\"\[\033[${style};38;5;45m\]\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
		echo "export turqoise=\"\[\033[${style};38;5;50m\]\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
		echo "export smoothgreen=\"\[\033[${style};38;5;42m\]\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
		echo "export winered=\"\[\033[${style};38;5;637m\]\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
		echo "export brown=\"\[\033[${style};38;5;684m\]\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
		echo "export silver=\"\[\033[${style};38;5;761m\]\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
		echo "export ocher=\"\[\033[${style};38;5;690m\]\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
		echo "export orange=\"\[\033[${style};38;5;714m\]\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
		echo "export purple=\"\[\033[${style};38;5;604m\]\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
		echo "export pink=\"\[\033[${style};38;5;213m\]\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
		echo "export cream=\"\[\033[${style};38;5;5344m\]\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE

		echo -E "usercolor=\"$(get_key color/user)\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
		echo -E "hostcolor=\"$(get_key color/host)\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
		echo -E "datecolor=\"$(get_key color/date)\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
		echo -E "timecolor=\"$(get_key color/time)\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
		echo -E "wdircolor=\"$(get_key color/wdir)\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
		echo -E "fontcolor=\"$(get_key color/font)\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
		echo -E "sepacolor=\"$(get_key color/separatorc)\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
		echo -E "upcolor=\"$(get_key color/uptime)\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
		echo -E "pscolor=\"$(get_key color/ps234c)\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE

	else	echo "enabcol=false" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	fi

	if [[ $(get_key colored_ls) == true ]]; then \
		echo "alias ls=\"ls -CF --color=auto --group-directories-first\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	else    echo "alias ls=\"ls -CF --color=no --group-directories-first\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	fi

	echo "alias ll=\"ls -l --group-directories-first\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	echo "alias la=\"ls -A --group-directories-first\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE

	if [[ $(get_key alias/one) != "" ]]; then \
		echo "alias \"$(get_key alias/one)\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	fi

	if [[ $(get_key alias/two) != "" ]]; then \
		echo "alias \"$(get_key alias/two)\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	fi

	if [[ $(get_key alias/three) != "" ]]; then \
		echo "alias \"$(get_key alias/three)\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	fi

	if [[ $(get_key alias/four) != "" ]]; then \
		echo "alias \"$(get_key alias/four)\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	fi

	if [[ $(get_key alias/five) != "" ]]; then \
		echo "alias \"$(get_key alias/five)\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	fi

	if [[ $(get_key alias/six) != "" ]]; then \
		echo "alias \"$(get_key alias/six)\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	fi

	if [[ $(get_key alias/seven) != "" ]]; then \
		echo "alias \"$(get_key alias/seven)\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	fi

	if [[ $(get_key alias/eight) != "" ]]; then \
		echo "alias \"$(get_key alias/eight)\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	fi

	if [[ $(get_key alias/nine) != "" ]]; then \
		echo "alias \"$(get_key alias/nine)\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	fi

	echo "ps234char=\"$(get_key ps234)\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE

	if [[ $(get_key colored_man) == true ]]; then \
		if	[[ $(get_key man_style) == mostlike ]]; then \
			echo "alias man=\"TERMINFO=$BSNG_PREFIX/share/bashstyle-ng/terminfo TERM=mostlike LESS=C PAGER=less man\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
		elif	[[ $(get_key man_style) == bold ]]; then \
			echo "alias man=\"TERMINFO=/usr/share/bashstyle-ng/terminfo TERM=bold LESS=C PAGER=less man\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
		elif	[[ $(get_key man_style) == nebual ]]; then
			echo "alias man=\"TERMINFO=/usr/share/bashstyle-ng/terminfo TERM=nebula LESS=C PAGER=less man\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
		fi
	fi

	echo "HISTCONTROL=\"$(get_key hist_control)\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	echo "HISTIGNORE=\"$(get_key hist_ignore)\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	echo "FIGNORE=\"$(get_key fignore)\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	echo "CDPATH=\"$(get_key cdpath)\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	echo "FCEDIT=\"$(get_key fcedit)\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	echo "TMOUT=\"$(get_key timeout)\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	echo "HISTSIZE=\"$(get_key history_size)\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	echo "HISTFILESIZE=\"$(get_key history_size)\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE

	if [[ $(get_key path) != "" ]]; then
		echo "PATH=\"$(get_key path)\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	fi

	if [[ $(get_key colored_grep) == true ]]; then \
		echo "GREP_OPTIONS=\"--color=auto;\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
		echo "GREP_COLOR=\"$(get_key color/grep);\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	fi

	if [[ $(get_key shopt/cdspell) == true ]]; then \
		echo "shopt -s cdspell" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	fi

	if [[ $(get_key shopt/cdable_vars) == true ]]; then \
		echo "shopt -s cdable_vars" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	fi

	if [[ $(get_key shopt/checkhash) == true ]]; then \
		echo "shopt -s checkhash" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	fi

	if [[ $(get_key shopt/cmdhist) == true ]]; then \
		echo "shopt -s cmdhist" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	fi

	if [[ $(get_key shopt/force_fignore) == true ]]; then \
		echo "shopt -s force_fignore" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	fi

	if [[ $(get_key shopt/histreedit) == true ]]; then \
		echo "shopt -s histreedit" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	fi

	if [[ $(get_key shopt/no_empty_cmd_completion) == true ]]; then \
		echo "shopt -s no_empty_cmd_completion" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	fi

	if [[ $HISTCONTROL != erasedups && $(get_key shopt/histappend) == true ]]; then
		echo "shopt -s histappend" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	fi

	if [[ $BASH_VERSINFO == 4 ]]; then

	if [[ $(get_key shopt/autocd) == true ]]; then
		echo "shopt -s autocd" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	fi

	if [[ $(get_key shopt/checkjobs) == true ]]; then
		echo "shopt -s checkjobs" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	fi

	if [[ $(get_key shopt/dirspell) == true ]]; then
		echo "shopt -s dirspell" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	fi

	if [[ $(get_key shopt/globstar) == true ]]; then
		echo "shopt -s globstar" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	fi

	fi

	echo "shopt -s checkwinsize" >> $HOME/bashstyle/bashrc.bs-ng-$DATE

	if [[ $(get_key random_style) == true ]]; then \
		source $BSNG_PREFIX/share/bashstyle-ng/rc/settings/random-rc
	else    pstyle=$(get_key prompt_style)
	fi

	if [[ $use_custom_prompt == true ]]; then \
		. $BSNG_PREFIX/share/bashstyle-ng/rc/styles/custom-rc
		cat $HOME/.custom_prompt >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	elif [[ -e $BSNG_PREFIX/share/bashstyle-ng/rc/styles/${pstyle}-rc ]]; then \
		cat $BSNG_PREFIX/share/bashstyle-ng/rc/styles/${pstyle}-rc >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	else	cat $BSNG_PREFIX/share/bashstyle-ng/rc/styles/fallback-rc >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	fi

	echo -E "PS2=\"$pscolor \$ps234char\$fontcolor \"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	echo -E "PS3=\"$pscolor \$ps234char\$ps234char\$fontcolor \"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	echo -E "PS4=\"$pscolor \$ps234char\$ps234char\$ps234char$fontcolor \"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE

	if [[ $(get_key tabrotate) == true ]]; then \
		echo "bind 'TAB:menu-complete'" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
		echo "complete -d -X '.[a-zA-Z0-9]*' cd" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	fi

	if [[ -e $HOME/.custom-rc ]]; then
		cat $HOME/.custom-rc >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	fi

	. $BSNG_PREFIX/share/bashstyle-ng/rc/settings/ls-rc

	echo "unset BSNG_GCONF_READ" >> $HOME/bashstyle/bashrc.bs-ng-$DATE

	echo "export LS_COLORS=\"$LS_COLORS\"" >> $HOME/bashstyle/bashrc.bs-ng-$DATE

	WelcomeMessage=$(get_key welcome_message)

	if [[ $WelcomeMessage != "" && $pstyle != clock-ad && $pstyle != clock ]]; then \
		echo "clear" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
		echo "$WelcomeMessage" >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	fi

	sed -e '/\#\ .*/d' -i $HOME/bashstyle/bashrc.bs-ng-$DATE
	sed -e '/\#\#\#.*/d' -i $HOME/bashstyle/bashrc.bs-ng-$DATE
	sed -e '/\#\t.*/d' -i $HOME/bashstyle/bashrc.bs-ng-$DATE
	sed -e '/\#\!.*/d' -i $HOME/bashstyle/bashrc.bs-ng-$DATE
	sed -e 's/\\\$(truncpwd)/\\w/g' -i $HOME/bashstyle/bashrc.bs-ng-$DATE

	echo -e "${white}Bash configuration saved as: ${iceblue}$HOME/bashstyle/bashrc.bs-ng-$DATE\
	\n${white}copy it to ${green}$HOME/.bashrc${white} to use it. (disable BashStyle in Bashstyle-NG,\
	\nbefore using this config-file - settings will be overwritten else)\n"

	tput sgr0

	unset BSNG_GCONF_READ

}

_generate_inputrc()
{

	export BSNG_GCONF_READ=1

	if [[ -e $HOME/.inputrc ]]; then
		mv $HOME/.inputrc $HOME/.inputrc.old
	fi

	. $BSNG_PREFIX/share/bashstyle-ng/rc/settings/readline-rc

	if [[ ! -e $HOME/bashstyle ]]; then
		mkdir $HOME/bashstyle
	fi

	echo "# This config file was generated by BashStyle-NG $BSNG_VERSION at $DATE" > $HOME/bashstyle/readline-rc.bs-ng-$DATE
	echo "# Use as \$HOME/.inputrc to apply settings" >> $HOME/bashstyle/readline-rc.bs-ng-$DATE

	cat $HOME/.inputrc >> $HOME/bashstyle/readline-rc.bs-ng-$DATE

	rm -f $HOME/.inputrc

	if [[ -e $HOME/.inputrc.old ]]; then
		mv $HOME/.inputrc.old $HOME/.inputrc
	fi

	echo -e "${white}Readline configuration saved as ${iceblue}$HOME/bashstyle/readline-rc.bs-ng-$DATE\
	\n${white}copy it to ${green}$HOME/.inputrc${white} to use it (disable ReadlineCFG in Bashstyle-NG,\
	\nbefore using this config-file - file will be overwritten else)\n"

	tput sgr0

	unset BSNG_GCONF_READ
}

_generate_vimrc()
{

	export BSNG_GCONF_READ=1

	if [[ -e $HOME/.vimrc ]]; then
		mv $HOME/.vimrc $HOME/.vimrc.old
	fi

	. $BSNG_PREFIX/share/bashstyle-ng/rc/settings/vim-rc

	if [[ ! -e $HOME/bashstyle ]]; then
		mkdir $HOME/bashstyle
	fi

	echo "\" This config file was generated by BashStyle-NG $BSNG_VERSION at $DATE" > $HOME/bashstyle/vim-rc.bs-ng-$DATE
	echo "\" Use as \$HOME/.vimrc to apply settings" >> $HOME/bashstyle/vim-rc.bs-ng-$DATE

	cat $HOME/.vimrc >> $HOME/bashstyle/vim-rc.bs-ng-$DATE

	rm -f $HOME/.vimrc

	if [[ -e $HOME/.vimrc.old ]]; then
		mv $HOME/.vimrc.old $HOME/.vimrc
	fi

	echo -e "${white}Vim configuration saved as: ${iceblue}$HOME/bashstyle/vim-rc.bs-ng-$DATE\
	\n${white}copy it to ${green}$HOME/.vimrc${white} to use it (disable VimCFG in BashStyle-NG,\
	\nbefore using this config-file - file will be overwritten else)\n"

	tput sgr0

	unset BSNG_GCONF_READ
}

_generate_nanorc()
{

	export BSNG_GCONF_READ=1

	if [[ -e $HOME/.nanorc ]]; then
		mv $HOME/.nanorc $HOME/.nanorc.old
	fi

	. $BSNG_PREFIX/share/bashstyle-ng/rc/settings/nano-rc

	if [[ ! -e $HOME/bashstyle ]]; then
		mkdir $HOME/bashstyle
	fi

	echo "# This config file was generated by BashStyle-NG $BSNG_VERSION at $DATE" > $HOME/bashstyle/nano-rc.bs-ng-$DATE
	echo "# Use as \$HOME/.nanorc to apply settings" >> $HOME/bashstyle/nano-rc.bs-ng-$DATE

	cat $HOME/.nanorc >> $HOME/bashstyle/nano-rc.bs-ng-$DATE

	rm -f $HOME/.nanorc

	if [[ -e $HOME/.nanorc.old ]]; then
		mv $HOME/.nanorc.old $HOME/.nanorc
	fi

	echo -e "${white}Nano configuration saved as: ${iceblue}$HOME/bashstyle/nano-rc.bs-ng-$DATE\
	\n${white}copy it to ${green}$HOME/.nanorc${white} to use it (disable NanoCFG in BashStyle-NG,\
	\nbefore using this config-file - file will be overwritten else)\n"

	tput sgr0

	unset BSNG_GCONF_READ
}

case $1 in
	*bash)
		_generate_bashrc
	;;

	*readline)
		_generate_inputrc
	;;

	*vim)
		_generate_vimrc
	;;

	*nano)
		_generate_nanorc
	;;

	*all)
		_generate_bashrc
		_generate_inputrc
		_generate_vimrc
		_generate_nanorc
	;;

	*)

		echo -e "\n${white}Usage:\n"
		echo -e "${orange}rcgenerator${white} |${green} --bash ${iceblue}[generate standalone BASH config]\
		\n${orange}rcgenerator${white} |${green} --readline ${iceblue}[generate standalone Readline config]\
		\n${orange}rcgenerator${white} |${green} --vim ${iceblue}[generate standalone VIm config]\
		\n${orange}rcgenerator${white} |${green} --nano ${iceblue}[generate standalone Nano config]\
		\n${orange}rcgenerator${white} |${green} --all ${iceblue}[all above at once]\n" | column -t
		echo ""

	;;
esac
