#!/bin/bash
#
# install - TurboPrint installation script
#
# must have root permissions
#
# (c) 2000-2002 ZEDOnet
# mail@turboprint.de


TP_BATCH=0
TP_LANGUAGE="en"
TP_NOGUI=0

for i do
	case "$i" in
	--batch ) TP_BATCH=1 ;;
	--nogui ) TP_NOGUI=1 ;;
	--cups=* ) TP_CUPS=${i#*=} ;;
	--language=* ) TP_LANGUAGE=${i#*=} ;;
	--charset=* ) TP_CHARSET=${i#*=} ;;
	--browser=* ) TP_NEWBROWSER="${i#*=}" ;;
	esac
done

# call gui version if x-display is available

if [ $TP_BATCH -eq 0 -a $TP_NOGUI -eq 0 ] ; then
    if [ ! -z $DISPLAY ] ; then
	bin/xtpsetup --setup
	if [ $? -eq 0 ] ; then
		exit 0
	fi
    fi
fi

echo
echo "TurboPrint Installation"
echo "======================="

bin/tpsetup --printkey *.key
result=$?

if [ $TP_BATCH -eq 0 ] ; then
	echo
	echo -n "Installation language (e)nglish or (d)eutsch? "

	read TP_LANGUAGE
	if [ -z $TP_LANGUAGE ] ; then
		TP_LANGUAGE="e"
	fi
	if [ $TP_LANGUAGE != "d" -a $TP_LANGUAGE != "de" ] ; then
		TP_LANGUAGE="en"
		TP_CHARSET="C"
	else
		TP_LANGUAGE="de"
		TP_CHARSET="de_DE"
	fi
fi

if [ $TP_LANGUAGE != "de" ] ; then

TP_CF_NOT_FOUND="Configuration files not found"
TP_CF_NOT_FOUND1="Please change to the installation directory first"
TP_KEYFILE_FOUND="Keyfile found in installation directory."
TP_KEYFILE_FOUND1="Installing TurboPrint full version."
TP_KEYFILE_INCORRECT="Incorrect keyfile found!"
TP_KEYFILE_INCORRECT1="Maybe keyfile wasn't properly saved from email software."
TP_KEYFILE_INCORRECT2="Do you want to continue and install TurboPrint as Free Edition (y/n)?"
TP_KEYFILE_NOT="No keyfile present in installation directory."
TP_KEYFILE_NOT1="Installing TurboPrint Free Edition."
TP_PATH_CONFIG="Configuration files will go to.."
TP_PATH_SHARED="Shared files will go to........."
TP_PATH_BIN="Executable files will go to....."
TP_PATH_LOG="Logfiles files will go to......."
TP_PATH_DOC="Documentation will go to........"
TP_PATH_BROWSER="Browser for documentation......."
TP_CUPS_MODE="Installation for CUPS printing system (TP_CUPS=1)"
TP_LPR_MODE="Installation for LPR/LPRng spooler (TP_CUPS=0)"
TP_PATH_EDIT="(edit system.cfg to change installation paths)"
TP_ASK_BEGIN="Begin installation now (y/n)? "
TP_DOCU="HTML documentation can be found in $TPPATH_DOC/html"
TP_DOCU1="Do you want to read the manual now (y/n)? "
TP_WAIT_BROWSER="Browser will start in a few seconds. Please wait..."

else

TP_CF_NOT_FOUND="Konfigurationsdateien nicht gefunden"
TP_CF_NOT_FOUND1="Bitte wechseln Sie zuerst ins Installationsverzeichnis"
TP_KEYFILE_FOUND="Keyfile im Installationsverzeichnis gefunden."
TP_KEYFILE_FOUND1="TurboPrint Vollversion wird installiert."
TP_KEYFILE_INCORRECT="Keyfile ist nicht in Ordnung!"
TP_KEYFILE_INCORRECT1="Evtl. wurde das Keyfile nicht korrekt aus Ihrem eMail-Programm gespeichert."
TP_KEYFILE_INCORRECT2="Wollen Sie fortfahren und TurboPrint als FreeEdition installieren (y/n)?"
TP_KEYFILE_NOT="Kein Keyfile im Installationsverzeichnis gefunden."
TP_KEYFILE_NOT1="TurboPrint FreeEdition wird installiert."
TP_PATH_CONFIG="Konfigurationsdateien......:"
TP_PATH_SHARED="Shared files (Treiber etc.):"
TP_PATH_BIN="Ausführbare Dateien........:"
TP_PATH_LOG="Logfiles...................:"
TP_PATH_DOC="Dokumentation..............:"
TP_PATH_BROWSER="Browser für HTML-Doku......:"
TP_CUPS_MODE="Installation für CUPS Drucksystem (TP_CUPS=1)"
TP_LPR_MODE="Installation für LPR/LPRng Spooler (TP_CUPS=0)"
TP_PATH_EDIT="(Bitte editieren Sie system.cfg, um diese Pfade zu ändern)"
TP_ASK_BEGIN="Installation jetzt starten (y/n)? "
TP_DOCU="HTML Anleitung ist im Verzeichnis"
TP_DOCU1="Wollen Sie die Anleitung jetzt lesen (y/n)? "
TP_WAIT_BROWSER="Browser wird in einigen Sekunden gestartet. Bitte warten..."

fi

# get dirs

if [ ! -e system.cfg ] ; then
	echo "$TP_CF_NOT_FOUND"
	echo "$TP_CF_NOT_FOUND1"
	exit 1
fi

echo ""

# search for keyfile

if [ ! -z $result ] ; then
 if [ $result -eq 0 ] ; then
	echo "$TP_KEYFILE_FOUND"
	echo "$TP_KEYFILE_FOUND1"
 else
    if [ $result -eq 1 ] ; then
	echo "$TP_KEYFILE_INCORRECT"
	echo "$TP_KEYFILE_INCORRECT1"
	if [ $TP_BATCH -eq 0 ] ; then
		echo -n "$TP_KEYFILE_INCORRECT2"
		read ANSWER
		if [ -z $ANSWER ] ; then
			exit 0
		fi
		if [ $ANSWER != "y" ] ; then
			exit 0
		fi
	fi
    else
	echo "$TP_KEYFILE_NOT"
	echo "$TP_KEYFILE_NOT1"
    fi
 fi
fi

eval $(cat system.cfg)

# override help browser if set by command line

if [ ! -z $TP_NEWBROWSER ] ; then
	TPBIN_BROWSER="$TP_NEWBROWSER"
fi

# check if CUPS is present if not set by command line

if [ -z $TP_CUPS ] ; then
	if [ -e $TPPATH_CUPSDRIVER ] ; then
		TP_CUPS=1
	else
		TP_CUPS=0
	fi
fi
if [ $TP_CUPS -eq 1 ] ; then
	TP_MODE=$TP_CUPS_MODE
else
	TP_MODE=$TP_LPR_MODE
fi

echo
echo "$TP_MODE"
echo "$TP_PATH_CONFIG $TPPATH_CONFIG"
echo "$TP_PATH_SHARED $TPPATH_SHARE"
echo "$TP_PATH_BIN $TPPATH_BIN"
echo "$TP_PATH_LOG $TPPATH_LOG"
echo "$TP_PATH_DOC $TPPATH_DOC"
echo "$TP_PATH_BROWSER $TPBIN_BROWSER"
echo
echo "$TP_PATH_EDIT"
echo
if [ $TP_BATCH -eq 0 ] ; then
	echo -n "$TP_ASK_BEGIN"
	read ANSWER
	if [ -z $ANSWER ] ; then
		exit 0
	fi
	if [ $ANSWER != "y" ] ; then
		exit 0
	fi
fi

# call static installation subroutine which is also called for RPM creation

lib/install-static --tgz

# call non-static installation subroutine
# which is also called during RPM installation (%post)

lib/install-post --tgz --cups=$TP_CUPS --language=$TP_LANGUAGE --charset=$TP_CHARSET --browser=$TPBIN_BROWSER

# call installation subroutine which prints some user information

lib/install-info --tgz --cups=$TP_CUPS --language=$TP_LANGUAGE

# install keyfile if present

if [ $result -eq 0 ] ; then
	tpsetup --install *.key
fi

echo
echo "$TP_DOCU $TPPATH_DOC/html"

if [ $TP_BATCH -eq 0 ] ; then
	echo -n "$TP_DOCU1"
	read ANSWER
	if [ -z $ANSWER ] ; then
		exit 0
	fi
	if [ $ANSWER != "y" ] ; then
		exit 0
	fi
	echo
	echo "$TP_WAIT_BROWSER"
	$TPBIN_BROWSER "$TPPATH_DOC/html/turboprint.html" &
fi

exit 0
