#!/bin/bash
#
# $Revision: 1.31 $, $Date: 2003/11/06 11:28:32 $
#

# 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|log|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;
# variable_problem():	2 args		arg1:"Xvariable_to_check", arg2:error message;
# print_help():		FIXME;
# -----------------------

# 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_log		associated with dml_check_the_rest();
# dml_exit		associated with dml_check_the_rest();
# -----------------------

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>"
			;;
			Wyjcie)
			echo "<button id=dml_exit caption=Wyjcie><br>" 
			;;
		esac
	done
	if [ "$PLDCONF_SHOW_SOURCE_BUTTON" = "yes" ]; then
		echo "<button id=dml_source caption='Poka kod'>"
	fi
	#echo "<button id=dml_log caption=Log>"
}

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

dml_edit_check() {
	if [ "X$dml_edit" = "Xyes" ];then 
		$PLDCONF_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
		echo "Kopiuj $1 $2"
		cp $1 $2
		sleep 2
	fi
}


dml_check_the_rest() {
	#Preparing Log file
	if [ "X$dml_log" = "Xyes" ]; then
		#LOGFILE isn't implemented yet
		#echo "FIXME"; sleep 1; exec $0
		if [ "$PLDCONF_LOG" = "yes" ]; then
			export PLDCONF_LOG="no"
		else
			export PLDCONF_LOG="yes"
		fi
		echo "Logowanie: $PLDCONF_LOG"
		sleep 2
		exec $0
	fi
	if [ "X$dml_source" =  "Xyes" ]; then
		$PLDCONF_EDITOR $0; exec $0
	fi
	if [ "X$dml_exit" = "Xyes" ]; then
		rm $TMPFILE 2>/dev/null; exit
	fi
	if [ "X$dml_ok" = "Xyes" ]; then
		if [ $PLDCONF_LOG = "yes" ]; then
			echo -e "`basename $0` `date`\n" >> $LOG_FILE
			run_main 2>> $LOG_FILE
			less $LOG_FILE
		else
			run_main 2> /dev/null
		fi
		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"
		;;
		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
			echo "Tylko root moe instalowa pakiety..."
			sleep 2
			return 1
		fi
		if [ "X$PLDCONF_ON_ERROR" = "XWARN_ONLY" ]; then
			sleep 1 && return 1
		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 1
			fi
			if [ "$input" = w ]; then
				TMPFILE=`mktemp /tmp/pldconf.XXXXXX` || exit 1
				grep -v PLDCONF_ON_ERROR ${CONF_DIR}/ustawienia > $TMPFILE
				cp $TMPFILE ${CONF_DIR}/ustawienia
				export PLDCONF_ON_ERROR=WARN_ONLY
				echo PLDCONF_ON_ERROR=WARN_ONLY >> ${CONF_DIR}/ustawienia
				rm $TMPFILE 2>/dev/null
				return 1	
			fi
			poldek -vi $packets_to_install && return 0 || sleep 2 && return 1
		fi
	fi
}

variable_problem() {
	if [ "$1" = "X" ]; then	 # "Xvar"="X" -null variable!
		echo "$2"; exit
	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;
EOF
}

export -f  dml_buttons dml_cancel_check dml_edit_check dml_help_check dml_recover_check dml_check_the_rest install_if_missing variable_problem print_help
