#!/bin/bash
#
# $Revision: 1.70 $, $Date: 2004/02/13 12:55:57 $
#

# 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;
# 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
# 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=%	
#
#   CAREFUL WITH line_replace() !
#
# Be careful about example file below:
# nameserver 192.168.1.100
#
# Say you want to add a new nameserver 192.168.1.1  ->  line_replace "192.168.1.1" "192.168.1.1" /etc/resolv.conf 
# The above call is supposed to	replace old 192.168.1.1 to avoid duplicate entries in /etc/resolv.conf, but 
# 192.168.1.1 matches 192.168.1.100 also - that would cause overwriting 192.168.1.100 into 192.168.1.1 . 
# In such cases use line_replace "192.168.1.1\b" "192.168.1.1" /etc/resolv.conf  (see man perlre for \b)
# 					     ^^
# This aplies to files that may contain strings being substrings of other strings, (i.e. /etc/resolv.conf,
# /etc/hosts): 192.168.1.2 192.168.1.23 192.168.1.230 or ALA_MA_KOTA ALA ALA_MA
#					
# -----------------------
#
# 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();
# -----------------------


. /etc/pldconf/ustawienia || exit

export KERNEL_VER=`uname -r | cut -b1-3`
export PLDCONF_VERSION=0.3.9
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

show_message () {
	echo "$1"
	echo
	if [ $TIMEOUT_MODE -eq 1 ]; then
		sleep $SLEEP_TIME 
	else
		read
	fi
}

show_verbose_message () {
	if [ $VERBOSE_MODE -eq 1 ]; then
		clear
		eval "$2"
		echo; echo
		show_message "$1"
	fi
}

line_replace () {
	if [ ! -f $3 ]; then
		touch $3
		show_verbose_message "Utworzenie brakujcego pliku $3"
	fi
	if [ -z "`cat $3 | grep \"$1\"`" ]; then 
		echo "$2" >> $3
		addon="Nowy wpis"
	else
		SEPARATOR=/; eval $4
		addon="SEPARATOR=$SEPARATOR"
		perl -pi -e "s$SEPARATOR^$1.*$SEPARATOR$2$SEPARATOR" $3
	fi
	show_verbose_message "$3: Po zmianie \"$1\" na \"$2\"    $addon" "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)
		show_message "Zignoruj poniszy komunikat jeeli pakiety s zainstalowane"
		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 [ $FIX_ERRORS = 0 ]; 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 1
			fi
			if [ "$input" = w ]; then
				line_replace "export FIX_ERRORS" "export FIX_ERRORS=0" ${CONF_DIR}/ustawienia
				export FIX_ERRORS=0
				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 

