#!/bin/bash
#########################################################
#                                                       #
# This is BashStyle-NG 		                        #
#                                                       #
# Licensed under GNU GENERAL PUBLIC LICENSE v3          #
#                                                       #
# Copyright 2007 - 2010 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)"

_rc_echo()
{

	echo -e $@ >> $HOME/bashstyle/bashrc.bs-ng-$DATE

}

_bash_nxrc()
{

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

	rm -f $HOME/bashstyle/bashrc.bs-ng-$DATE

	_rc_echo \#\!/bin/bash
	_rc_echo ""
	_rc_echo \# This configuration was created by BS-NG v$BSNG_VERSION
	_rc_echo \# it was generated at: $DATE, by: $USER
	_rc_echo \# use it as \$HOME/.bashrc or source this file

	_rc_echo ""
	_rc_echo Base setup
	_rc_echo == == == == == == == == == == == == ==
	_rc_echo ""

	_rc_echo export BSNG_PREFIX=$BSNG_PREFIX
	_rc_echo export BSNG_RC_DIR=$BSNG_PREFIX/share/bashstyle-ng/rc/functions/
	_rc_echo export BSNG_LOCKFILE=$HOME/.bashstyle.lock
	_rc_echo export BSNG_VERSION=$BSNG_VERSION
	#_rc_echo export BSNG_GCONF_READ=1

	_rc_echo "\nbsource () {"
	_rc_echo "\n\tfor file in \$@; do"
	_rc_echo "\t\tsource \$BSNG_PREFIX/share/bashstyle-ng/rc/functions/\$file"
	_rc_echo "\tdone\n"
	_rc_echo "}\n"

	_rc_echo bsource gconf debug check_opt xunalias ls git exchange xmv xcp xchown xconv random
	_rc_echo bsource bs_ng_echo showuptime showmem showsystemload showcpuload man2pdf countfiles
	_rc_echo bsource countprocesses showbatteryload showip showsize showspace showtty systeminfos
	_rc_echo bsource truncpwd ps misc

	_rc_echo "\nif [[ \$BASH_VERSINFO == 4 ]]; then"
	_rc_echo "\tbsource xdel"
	_rc_echo "fi\n"

	_rc_echo alias rd=pwd -P
	_rc_echo alias reload=source ~/.bashrc
	_rc_echo ""
	_rc_echo ps234char=\"$(get_key ps234)\"

}

_bash_colors()
{

	_rc_echo ""
	_rc_echo Color configuration
	_rc_echo == == == == == == == == == == == == ==
	_rc_echo ""

	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

		_rc_echo "if [[ \$TERM != *xterm* ]]; then"
		_rc_echo "\texport enabcol=false"
		_rc_echo "else\texport enabcol=true"
		_rc_echo "fi\n"

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

		_rc_echo -E "usercolor=\"$(get_key color/user)\""
		_rc_echo -E "hostcolor=\"$(get_key color/host)\""
		_rc_echo -E "datecolor=\"$(get_key color/date)\""
		_rc_echo -E "timecolor=\"$(get_key color/time)\""
		_rc_echo -E "wdircolor=\"$(get_key color/wdir)\""
		_rc_echo -E "fontcolor=\"$(get_key color/font)\""
		_rc_echo -E "sepacolor=\"$(get_key color/separatorc)\""
		_rc_echo -E "upcolor=\"$(get_key color/uptime)\""
		_rc_echo -E "pscolor=\"$(get_key color/ps234c)\""

	else	_rc_echo export enabcol=false
	fi

	_rc_echo ""
	_rc_echo export LS_COLORS=$LS_COLORS

	if [[ $(get_key colored_man) == true ]]; then \
		_rc_echo ""
		if	[[ $(get_key man_style) == mostlike ]]; then \
			_rc_echo alias man=\"TERMINFO=\$BSNG_PREFIX/share/bashstyle-ng/terminfo TERM=mostlike LESS=C PAGER=less man\"
		elif	[[ $(get_key man_style) == bold ]]; then \
			_rc_echo alias man=\"TERMINFO=\$BSNG_PREFIX/share/bashstyle-ng/terminfo TERM=bold LESS=C PAGER=less man\"
		elif	[[ $(get_key man_style) == nebula ]]; then
			_rc_echo alias man=\"TERMINFO=\$BSNG_PREFIX/share/bashstyle-ng/terminfo TERM=nebula LESS=C PAGER=less man\"
		fi
		_rc_echo ""
	fi

	if [[ $(get_key colored_grep) == true ]]; then \
		_rc_echo GREP_OPTIONS=\"--color=auto\;\"
		_rc_echo GREP_COLOR=\"$(get_key color/grep)\;\"
	fi

}

_bash_aliases()
{

	_rc_echo ""
	_rc_echo Aliases
	_rc_echo == == == == == == == == == == == == ==
	_rc_echo ""

	if [[ $(get_key colored_ls) == true ]]; then \
		_rc_echo alias ls=\"ls -CF --color=auto --group-directories-first\"
	else    _rc_echo alias ls=\"ls -CF --color=no --group-directories-first\"
	fi

	if [[ $(get_key alias/one) != "" ]]; then \
		_rc_echo alias $(get_key alias/one)
	fi

	if [[ $(get_key alias/two) != "" ]]; then \
		_rc_echo alias $(get_key alias/two)
	fi

	if [[ $(get_key alias/three) != "" ]]; then \
		_rc_echo alias $(get_key alias/three)
	fi

	if [[ $(get_key alias/four) != "" ]]; then \
		_rc_echo alias $(get_key alias/four)
	fi

	if [[ $(get_key alias/five) != "" ]]; then \
		_rc_echo alias $(get_key alias/five)
	fi

	if [[ $(get_key alias/six) != "" ]]; then \
		_rc_echo alias $(get_key alias/six)
	fi

	if [[ $(get_key alias/seven) != "" ]]; then \
		_rc_echo alias $(get_key alias/seven)
	fi

	if [[ $(get_key alias/eight) != "" ]]; then \
		_rc_echo alias $(get_key alias/eight)
	fi

	if [[ $(get_key alias/nine) != "" ]]; then \
		_rc_echo alias $(get_key alias/nine)
	fi

}

_bash_options()
{

	_rc_echo ""
	_rc_echo Misc options
	_rc_echo == == == == == == == == == == == == ==
	_rc_echo ""

	_rc_echo HISTCONTROL=\"$(get_key hist_control)\"
	_rc_echo HISTIGNORE=\"$(get_key hist_ignore)\"
	_rc_echo FIGNORE=\"$(get_key fignore)\"
	_rc_echo CDPATH=\"$(get_key cdpath)\"
	_rc_echo FCEDIT=\"$(get_key fcedit)\"
	_rc_echo TMOUT=\"$(get_key timeout)\"
	_rc_echo HISTSIZE=\"$(get_key history_size)\"
	_rc_echo HISTFILESIZE=\"$(get_key history_size)\"

	if [[ $(get_key path) != "" ]]; then
		_rc_echo PATH=\"$(get_key path)\"
	fi

}

_bash_shopts()
{

	_rc_echo ""
	_rc_echo Shopts
	_rc_echo == == == == == == == == == == == == ==
	_rc_echo ""

	if [[ $(get_key shopt/cdspell) == true ]]; then \
		_rc_echo shopt -s cdspell
	fi

	if [[ $(get_key shopt/cdable_vars) == true ]]; then \
		_rc_echo shopt -s cdable_vars
	fi

	if [[ $(get_key shopt/checkhash) == true ]]; then \
		_rc_echo shopt -s checkhash
	fi

	if [[ $(get_key shopt/cmdhist) == true ]]; then \
		_rc_echo shopt -s cmdhist
	fi

	if [[ $(get_key shopt/force_fignore) == true ]]; then \
		_rc_echo shopt -s force_fignore
	fi

	if [[ $(get_key shopt/histreedit) == true ]]; then \
		_rc_echo shopt -s histreedit
	fi

	if [[ $(get_key shopt/no_empty_cmd_completion) == true ]]; then \
		_rc_echo shopt -s no_empty_cmd_completion
	fi

	if [[ $HISTCONTROL != erasedups && $(get_key shopt/histappend) == true ]]; then
		_rc_echo shopt -s histappend
	fi

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

		if [[ $(get_key shopt/autocd) == true ]]; then
			_rc_echo shopt -s autocd
		fi

		if [[ $(get_key shopt/checkjobs) == true ]]; then
			_rc_echo shopt -s checkjobs
		fi

		if [[ $(get_key shopt/dirspell) == true ]]; then
			_rc_echo shopt -s dirspell
		fi

		if [[ $(get_key shopt/globstar) == true ]]; then
			_rc_echo shopt -s globstar
		fi

	fi

	_rc_echo shopt -s checkwinsize

}

_bash_prompt()
{

	_rc_echo ""
	_rc_echo Bash prompts
	_rc_echo == == == == == == == == == == == == ==
	_rc_echo ""

	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 \
		cat $HOME/.custom_prompt | sed -e '/###.*/d' -e '/# /d' -e '/#\t/d' -e '/#\!/d' \
			>> $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 \
			| sed -e '/###.*/d' -e '/# /d' -e '/#\t/d' -e '/#\!/d' \
				>> $HOME/bashstyle/bashrc.bs-ng-$DATE
	else	cat $BSNG_PREFIX/share/bashstyle-ng/rc/styles/fallback-rc \
			| sed -e '/###.*/d' -e '/# /d' -e '/#\t/d' -e '/#\!/d' \
				$HOME/bashstyle/bashrc.bs-ng-$DATE
	fi

	_rc_echo ""
	_rc_echo -E PS2=\"\$pscolor \$ps234char\$fontcolor \"
	_rc_echo -E PS3=\"\$pscolor \$ps234char\$ps234char\$fontcolor \"
	_rc_echo -E PS4=\"\$pscolor \$ps234char\$ps234char\$ps234char\$fontcolor \"

}

_bash_finalize()
{

	_rc_echo ""
	_rc_echo Final settings
	_rc_echo == == == == == == == == == == == == ==
	_rc_echo ""

	if [[ $(get_key tabrotate) == true ]]; then \
		_rc_echo bind \'TAB:menu-complete\'
		_rc_echo complete -d -X \'.[a-zA-Z0-9]*\' cd
	fi

	if [[ -e $HOME/.custom-rc ]]; then
		cat $HOME/.custom-rc | sed -e '/#\!/d' >> $HOME/bashstyle/bashrc.bs-ng-$DATE
	fi

	#_rc_echo "unset BSNG_GCONF_READ"

	WelcomeMessage=$(get_key welcome_message)

	if [[ $WelcomeMessage != "" && $pstyle != clock-ad && $pstyle != clock ]]; then \
		_rc_echo "clear"
		_rc_echo "$WelcomeMessage"
	fi

}

_generate_bashrc()

{
	export BSNG_GCONF_READ=1

	_bash_nxrc
	_bash_colors
	_bash_aliases
	_bash_options
	_bash_shopts
	_bash_prompt
	_bash_finalize

	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
