#!/bin/sh

#
# last update: 14-MAR-2009
#

# CLEX 3.X files
cfg3reg=~/.clexrc
cfg3sys='/etc/clexrc'
cfg3alt='/usr/local/etc/clexrc'

# CLEX 4.0 and 4.1 files
cfg4old=~/.clexcfg
opt4old=~/.clexopt
bm4old=~/.clexbm

# CLEX 4.2+ files
cfg4dir=~/.clex
opt4new="$cfg4dir/options"
cfg4new="$cfg4dir/config"
bm4new="$cfg4dir/bookmarks"

echo 'CLEX configuration utility'
echo '=========================='
echo

if [ -f "$cfg4new" ] ; then
	echo "CLEX configuration file found. Please use the CLEX's"
	echo 'configuration panel to make changes.'
	echo
	echo 'Note: If you want to create a new configuration from scratch, please delete'
	echo "      the file $cfg4new and then run `basename $0` again."
	exit 1
fi

# LIST OF VARIABLES
VARIABLE_LIST='B_PANEL_SIZE C_PANEL_SIZE D_PANEL_SIZE H_PANEL_SIZE
	CMD_F3 CMD_F4 CMD_F5 CMD_F6 CMD_F7 CMD_F8 CMD_F9 CMD_F10 CMD_F11 CMD_F12
	TIME_FMT DATE_FMT LAYOUT_ACTIVE LAYOUT1 LAYOUT2 LAYOUT3
	CMD_LINES DIR2 FRAME KILOBYTE PROMPT QUOTE XTERM_TITLE'

confirmation()
{
	echo -n "$1 (y/N) ? "
	read answer junk
	if [ x"$answer" != x'y' -a x"$answer" != x'Y' ] ; then
		echo "Exiting"
		exit 1
	fi
	echo
}

reset_config() {
	local var

	for var in $COMPLETE_LIST ; do
		unset CLEX_$var
	done
}

read_config() {
	local line var val

	while read line ; do
		if ! echo $line | LC_ALL=C grep -q '^[A-Z][A-Z0-9_]*=.*$' ; then
			continue
		fi
		var=`echo $line | sed -e 's/=.*$//'`
		val=`echo $line | LC_ALL=C sed -e 's/^[A-Z0-9_]*=//'`
		eval CLEX_$var='"$val"'
	done < "$1"
}

convert_config() {
	echo 'Converting data from version 3 to version 4'
	if [ x"$CLEX_FMT_DATE" != x ] ; then
		CLEX_DATE_FMT=`echo $CLEX_FMT_DATE | sed -e 's/d/%d/;s/D/%e/;s/m/%m/;s/M/%b/;s/y/%y/;s/Y/%Y/'`
	fi
	if [ x"$CLEX_FMT_TIME" = x'1' ] ; then
		CLEX_TIME_FMT='%I:%M%p'
	elif [ x"$CLEX_FMT_TIME" = x'2' ] ; then
		CLEX_TIME_FMT='%H:%M'
	fi
	if [ x"$CLEX_ACTIVE_LAYOUT" != x ] ; then
		CLEX_LAYOUT_ACTIVE=`expr $CLEX_ACTIVE_LAYOUT + 1`
	fi
	if [ x"$CLEX_XTERM_TITLE" == x'2' ] ; then
		CLEX_XTERM_TITLE='1'
	fi
	if [ x"$CLEX_LAYOUT1" != x ] ; then
		CLEX_LAYOUT1=`echo $CLEX_LAYOUT1 | sed -e 's/|  /|/'`
	fi
	if [ x"$CLEX_LAYOUT2" != x ] ; then
		CLEX_LAYOUT2=`echo $CLEX_LAYOUT2 | sed -e 's/|  /|/'`
	fi
	if [ x"$CLEX_LAYOUT3" != x ] ; then
		CLEX_LAYOUT3=`echo $CLEX_LAYOUT3 | sed -e 's/|  /|/'`
	fi
}

detect_programs() {
	local opt var val prog arg

	unset PROG3 PROG4 PROG5 PROG6 PROG7 PROG8 PROG9
	ARG3='$f'
	ARG4='$f'
	if cp -- "$0" /dev/null >/dev/null 2>&1 ; then
		echo "End of options mark '--' is supported"
		opt=' --'
		PROG3='more'
		PROG4='vi'
		PROG5='cp -ir'	; ARG5='$f $2'
		PROG6='mv'		; ARG6='$f $2'
		PROG7='mkdir'	; ARG7=''
		PROG8='rm'		; ARG8='$f'
		PROG9='lpr'		; ARG9='$f'
	else
		echo "End of options mark '--' is NOT supported"
		opt=''
	fi

	if [ x"$CLEX_CMD_F3" = x ] ; then
		if [ x"$PAGER" != x ] ; then
			PROG3="$PAGER"
		elif less -V  >/dev/null 2>&1; then
			PROG3='less'
		fi
	fi
	echo "Pager program: $PROG3"

	if [ x"$CLEX_CMD_F4" = x ] ; then
		if [ x"$EDITOR" != x ] ; then
			PROG4="$EDITOR"
		elif vim --version  >/dev/null 2>&1; then
			PROG4='vim'
		fi
	fi
	echo "Text editor: $PROG4"

	for cmd in 3 4 5 6 7 8 9 ; do
		var=CLEX_CMD_F$cmd
		eval val="\$$var"
		if [ x"$val" != x ] ; then
			continue
		fi
		eval prog="\$PROG$cmd"
		if [ x"$prog" = x ] ; then
			continue
		fi
		eval arg="\$ARG$cmd"
		eval $var='"$prog$opt $arg"'
	done
}
 
print_config() {
	local var val

	for var in $VARIABLE_LIST ; do
		eval val="\$CLEX_$var"
		if [ x"$val" != x ] ; then
			echo $var=$val
		fi
	done
}

umask 022
if [ ! -d "$cfg4dir" ] ; then
	if ! mkdir "$cfg4dir" ; then
		echo 'Error: could not create ./clex subdirectory in your home directory'
		exit 1
	fi
fi

# convert from 4.0 or 4.1
if [ -f "$cfg4old" ] ; then
	echo 'moving the configuration file to new location'
	mv "$cfg4old" "$cfg4new"
fi
if [ -f "$opt4old" ] ; then
	echo 'moving the options file to new location'
	mv "$opt4old" "$opt4new"
fi
if [ -f "$bm4old"  ] ; then
	echo 'moving the bookmarks file to new location'
	mv "$bm4old"   "$bm4new"
fi
if [ -f "$cfg4new" ] ; then
	if [ x"$CLEX" != x ] ; then
		echo "Please restart CLEX"
	fi
	exit 0
fi

reset_config

# convert from 3.X
clex3filelist=''
for file in "$cfg3sys" "$cfg3alt" ; do
	if [ -f "$file" ] ; then
		echo 'System-wide configuration file from previous version 3 found'
		read_config "$file"
		clex3filelist="$clex3filelist $file"
	fi
done
if [ -f "$cfg3reg" ] ; then
	echo 'Personal configuration file from previous version 3 found'
	read_config "$cfg3reg"
	clex3filelist="$clex3filelist $cfg3reg"
fi
if [ x"$clex3filelist" != x ] ; then
	convert_config
fi

detect_programs

echo
echo '=== BEGIN =================='
print_config
echo '=== END ===================='
echo
confirmation 'Save this configuration'

{
	echo '#'
	echo '# CLEX configuration file'
	echo '#'
	print_config 
} > "$cfg4new"
if [ "$?" -ne 0 ] ; then
	echo 'Error saving configuration'
	exit 1
fi
echo "Configuration saved to $cfg4new"
if [ x"$clex3filelist" != x ] ; then
	echo "You might now want to delete the old configuration file(s):$clex3filelist"
fi
if [ x"$CLEX" != x ] ; then
	echo "Please restart CLEX"
fi
exit 0
