#!/bin/bash
#
# $Revision: 1.66 $, $Date: 2004/02/09 17:14:30 $
#

. /etc/pldconf/ustawienia

export KERNEL_VER=`uname -r | cut -b1-3`
export PLDCONF_VERSION=0.3.7
export EXEC_PREFIX=/usr/bin
export DATA_DIR=/usr/share/pldconf
export CONF_DIR=/etc/pldconf
export VF_MNTPOINT=/mnt/cdrom 
export VF_EJECT=eject
export PATH="$PATH:/usr/X11R6/bin"

if [ ! $EDITOR ]; then
	if [ "`which vim`" ]; then
		export EDITOR=vim
	else
		echo "Ustaw domylny edytor w pldconf -> Inne"
		exit
	fi
fi

export MODULES_CONFIG=/etc/modules.conf
if [ $KERNEL_VER = 2.6 ]; then
	export MODULES_CONFIG=/etc/modprobe.conf
fi

# Function name:	Number of args:	Description of args:
# dml_buttons():	optional	Ok/Anuluj/Edytuj/Powrt/Pomoc/Przywr/Kod/Log/Wyjcie;
# dml_cancel_check():	1 arg		$1 is file to return to;
# dml_edit_check():	1 arg		$1 is file to edit;
# dml_help_check():	1 arg		arg1 is file to view in less;
# dml_check_the_rest():	1 arg		check if exit|source|ok were pressed, 
#					arg1 is file to return to after run_main();
# dml_recover_check():	2 args		copy $1 (backup file) into $2 (current);
# install_if_missing(): 1 arg		$1 is $packets_to_install;
# print_help():		FIXME;
# line_replace():	3 or 4 args	$1 is string in line before, $2 is line after, $3 is /the/file
#					$4 is optional separator for s/foo/bar/ (take care of $foo=/usr/bin)
#					example call line_replace "/usr/bin/" "/usr/local/bin/" /etc/foo SEPARATOR=%
# backup_file(): 	1 arg	 	backup if not backed up already;
# show_message		1 arg		$1 is the message for the user 
# show_verbose_message	optional	$1 is the the extra message for the user, $2(opt) is file to be displayed
# -----------------------

# Variables list:	Description:
# dml_ok		associated with dml_check_the_rest();
# dml_cancel		associated with dml_cancel_check();
# dml_edit		associated with dml_edit_check();
# dml_help		associated with dml_help_check();
# dml_recover		associated with dml_recover_check();
# dml_source		associated with dml_check_the_rest();
# dml_exit		associated with dml_check_the_rest();
# -----------------------

show_message () {
	echo "$1"
	echo
	if [ $PLDCONF_MESSAGES_MODE = enter ]; then
		read
	else
		sleep $PLDCONF_SLEEP_TIME 
	fi
}

show_verbose_message () {
	if [ "X$PLDCONF_VERBOSE" = "Xyes" ]; then
		eval "$2"
		show_message "$1"
	fi
}

line_replace () {
	if [ ! "`cat $3 | grep \"$1\"`" ]; then 
#The other two cause errors ;) try it against NET -> DNS; try to add new/existing dns servers 
	#if [ -z `grep  \"$1\" $3` ]; then
	#if grep -q \"$1\" $3; then
		show_verbose_message "$3: Dodaj nowy wpis \"$2\""
		echo "$2" >> $3
	else
		SEPARATOR=/; eval $4
		show_verbose_message "$3: Zamiana \"$1\" na \"$2\"   SEPARATOR=$SEPARATOR"
		perl -pi -e "s$SEPARATOR^$1.*$SEPARATOR$2$SEPARATOR" $3
	fi
	show_verbose_message "Po zmianie w $3" "cat $3"
}

backup_file () {
	if [ `id -nu` = root ]; then
		BACKUP_DIR=${CONF_DIR}
	else
		BACKUP_DIR=~/.pldconf
	fi
	if [ ! -f $BACKUP_DIR/`basename $1` ]; then
		show_verbose_message "Backup $1 w $BACKUP_DIR/"
		mkdir -p $BACKUP_DIR
		cp $1 $BACKUP_DIR/
	else
		show_verbose_message "Backup $1 w $BACKUP_DIR/: OK, plik ju istnieje"
	fi
}

dml_buttons () {
	for button in $@; do
		case $button in
			Ok)
			echo "<button id=dml_ok caption=Ok>"
			;;
			Anuluj)
			echo "<button id=dml_cancel caption=Anuluj>"
			;;
			Powrt)
			echo "<button id=dml_cancel caption=Powrt>"
			;;
			Edytuj)
			echo "<button id=dml_edit caption=Edytuj>"
			;;
			Pomoc)
			echo "<button id=dml_help caption=Pomoc>"
			;;
			Przywr)
			echo "<button id=dml_recover caption=Przywr>"
			;;
		esac
	done
	echo "<br><button id=dml_source caption='Poka kod'><button id=dml_exit caption=Wyjcie>" 
}

dml_cancel_check() {
	if [ "X$dml_cancel" = "Xyes" ];then 
		exec $1
	fi
}

dml_edit_check() {
	if [ "X$dml_edit" = "Xyes" ];then 
		$EDITOR $1; exec $0
	fi
}

dml_help_check() {
	if [ "X$dml_help" =  "Xyes" ]; then
		$PLDCONF_PAGER $1; exec $0
	fi
}

dml_recover_check(){
	if [ "$dml_recover" = "yes" ]; then
		show_verbose_message "Kopiuj $1 na $2"
		cp $1 $2
	fi
}

dml_check_the_rest() {
	if [ "X$dml_source" =  "Xyes" ]; then
		$EDITOR $0; exec $0
	fi
	if [ "X$dml_exit" = "Xyes" ]; then
		#rm -rf /tmp/pldconf* 2>/dev/null
		exit
	fi
	if [ "X$dml_ok" = "Xyes" ]; then
		run_main 2> /dev/null 
		exec $1
	fi
}

install_if_missing() {
	packets_to_install="$1"
	case $packets_to_install in
		gnome-session)
		packets_to_install="gnome-desktop gnome-themes gnome-session gnome-panel nautilus metacity metacity-themes*"
		;;
		startkde)
		packets_to_install="kdebase"
		;;
		wmaker)
		packets_to_install="WindowMaker"
		;;
		xfwm4)
		packets_to_install="xfwm4 xfce4-panel xfce-utils xfce4-mixer"
		;;
		cups)
		packets_to_install="cups cups-clients cups-filter-pstoraster"
		;;
		alsa)
		packets_to_install="kernel-sound-alsa alsa-utils-init aumix"
		;;
		pcmcia)
		packets_to_install="pcmcia-cs kernel-pcmcia-cs"
		;;
		mail)
		packets_to_install="procmail fetchmail"
		;;
		mcedit)
		packets_to_install="mc"
		;;
	esac

	rpm -q $packets_to_install > /dev/null
	if [ $? -ne 0 ]; then
		echo; echo; echo "Brak przynajmniej jednego z poniszych pakietw:"
		echo "$packets_to_install"; echo 
		if [ `id -nu` != root ]; then
			show_message  "Tylko root moe instalowa pakiety..."
			return 1
		fi
		if [ "X$PLDCONF_ON_ERROR" = "XWARN_ONLY" ]; then
			#LiveCD needs this func to return 0 (success)
			return 0
		else
			echo "enter  -instaluj"
			echo "n      -nie instaluj"
			echo "w      -tylko ostrzegaj, nie naprawiaj (przywracane w pldconf -> Ustawienia)"; echo
			read input
			if [ "$input" = n ]; then
				return 0
			fi
			if [ "$input" = w ]; then
				line_replace "PLDCONF_ON_ERROR" "PLDCONF_ON_ERROR=WARN_ONLY" ${CONF_DIR}/ustawienia
				export PLDCONF_ON_ERROR=WARN_ONLY
				return 0	
			fi
			poldek -vi $packets_to_install && return 0 || return 1
		fi
	fi
}

print_help() {
	clear
	cat << EOF
	Dozwolone opcje:
	-X   -Konfiguracja X;
	-d   -Konfiguracja desktopu;
	-H   -Konfiguracja urzdze;
	-i   -Dodaj/Usu programy;
	-n   -Konfiguracja sieci;
	-b   -Konfiguracja menedera startu;
	-D   -Instalacja PLD;
	-M   -Poczenie z internetem GUI;
	-m   -Poczenie z internetem [operator|help];
	-I   -Inne;
	-v   -Wersja;
	-h   -Opis opcji;

	Wiele z tych opcji ma wasne podopcje, np. 
	pldconf -d -h
EOF
}

export -f  dml_buttons dml_cancel_check dml_edit_check dml_help_check dml_recover_check dml_check_the_rest install_if_missing print_help line_replace backup_file show_message show_verbose_message 

