#!/bin/bash
#
# install - TurboPrint installation script
#
# must have root permissions
#
# (c) 2000-2011 ZEDOnet GmbH


TP_BATCH=0
TP_LANGUAGE="en"
TP_NOGUI=0
TP_GIMP=1
TP_BJNP=1
TP_GSZEDO=1
TP_STARTMENU=1
TP_DESKTOP=1

for i do
	case "$i" in
	--batch ) 
	{
		echo "STARTED"
		TP_BATCH=1 
	};;
	--nogui ) TP_NOGUI=1 ;;
	--cups=* ) TP_CUPS=${i#*=} ;;
	--language=* ) TP_LANGUAGE=${i#*=} ;;
	--demotype=* ) TP_DEMOTYPE=${i#*=} ;;
	--charset=* ) TP_CHARSET=${i#*=} ;;
	--browser=* ) TP_NEWBROWSER="${i#*=}" ;;
	--user=* ) TP_USER="${i#*=}" ;;
	--display=* ) TP_DISPLAY="${i#*=}" ;;
	--keyfile=* ) TP_KEYFILE="${i#*=}" ;;
	--gimp=* ) TP_GIMP="${i#*=}" ;;
	--bjnp=* ) TP_BJNP="${i#*=}" ;;
# currently always install gszedo due to problems with preinstalled gs 9.25, 9.26
#	--gszedo=* ) TP_GSZEDO="${i#*=}" ;;
	--startmenu=* ) TP_STARTMENU="${i#*=}" ;;
	--desktop=* ) TP_DESKTOP="${i#*=}" ;;
	esac
done

# call gui version if x-display is available

if [ $TP_BATCH -eq 0 -a $TP_NOGUI -eq 0 ] ; then

	###
	# check if Qt5 libraries are missing, ask if should be installed
	###
	GUI_SUCCESS=$(bin/turboprint --test)
	if [ "$GUI_SUCCESS" != "RESULT 0" ] ; then
		echo "Could not find the Qt library version 5.15 on your system."
		echo -n "Do you want to install the Qt library from the TurboPrint installation package? (Y/n)"
		read ANSWER
		if [ -z $ANSWER ] ; then ANSWER="y"; fi
		if [ $ANSWER = "y" -o $ANSWER = "Y" ] ; then
			INSTALL_COMMAND="cp -r $PWD/gplsoftware/qt515zedo /opt"
			echo "Autorization required to execute '$INSTALL_COMMAND' as root"
			pkexec $INSTALL_COMMAND
		fi
	fi
    if [ ! -z $DISPLAY ] ; then
	bin/turboprint --setup
	if [ $? -eq 0 ] ; then
		exit 0
	else
		echo "Could not start X-Windows installation tool."
		echo "make sure that X-Windows is running and accessible."
		echo ""
	fi
    fi
fi 

if false ; then 
#if [ $TP_BATCH -eq 0 -a $TP_NOGUI -eq 0 ] ; then

	###
	# check which libraries are missing, ask if should be installed
	###
	ANY_MISSING=0
	GTK_MISSING=0
	CUPS_MISSING=0
	QT5_MISSING=0
	set -- $(ldd "bin/turboprint" | sed -e 's/\ /:/g;')
	for i do
  	  case "$i" in
	  *cups*not* ) ANY_MISSING=1; CUPS_MISSING=1;;
	  *gtk*not* ) ANY_MISSING=1; GTK_MISSING=1;;
  	  esac
	done
	set -- $(ldd "bin/turboprint-composer" | sed -e 's/\ /:/g;')
	for i do
          case "$i" in
	  *Qt5Widgets*not* ) ANY_MISSING=1; QT5_MISSING=1;;
	  esac
	done
	IS_RPM=0
	IS_DEB=0
	IS_PAC=0
	type dnf >&/dev/null && IS_RPM=1
	type apt-get >& /dev/null && IS_DEB=1
	type pacman >& /dev/null && IS_PAC=1
	#echo "IS_RPM $IS_RPM IS_DEB $IS_DEB IS_PAC $IS_PAC ANY_MISSING $ANY_MISSING"
	if [[ $ANY_MISSING -eq 1 && ( $IS_RPM -eq 1 || $IS_DEB -eq 1 || $IS_PAC -eq 1 ) ]] ; then
		PACKAGE_LIST=""
		echo "Some libraries are missing:"
		if [ $CUPS_MISSING -eq 1 ] ; then 
			echo "cups (printer spooler)"
			PACKAGE_LIST="cups"
		fi
		if [ $GTK_MISSING -eq 1 ] ; then 
			echo "Gtk-2 (graphical user interface)" 
			if [ $IS_RPM -eq 1 ] ; then
				PACKAGE_LIST="$PACKAGE_LIST gtk2"
			else
				if [ $IS_PAC -eq 1 ] ; then
					PACKAGE_LIST="$PACKAGE_LIST gtk2"
				else
					PACKAGE_LIST="$PACKAGE_LIST libgtk2.0-dev"
				fi
			fi
		fi
		if [ $QT5_MISSING -eq 1 ] ; then 
			echo "Qt5 (GUI for TurboPrint Composer)"
			if [ $IS_RPM -eq 1 ] ; then
				PACKAGE_LIST="$PACKAGE_LIST qt5-qtbase"
			else
				if [ $IS_PAC -eq 1 ] ; then
					PACKAGE_LIST="$PACKAGE_LIST qt5-base"
				else
					PACKAGE_LIST="$PACKAGE_LIST qtbase5-dev"
				fi
			fi
		fi
		echo -n "do you want to install them now? (Y/n)"
		read ANSWER
		if [ -z $ANSWER ] ; then ANSWER="y"; fi
		if [ $ANSWER = "y" -o $ANSWER = "Y" ] ; then
			if [ $IS_RPM -eq 1 ] ; then
				INSTALL_COMMAND="dnf -y install $PACKAGE_LIST"
			else 
				if [ $IS_PAC -eq 1 ] ; then
					INSTALL_COMMAND="pacman --noconfirm -S $PACKAGE_LIST"
				else
					INSTALL_COMMAND="apt-get -y install $PACKAGE_LIST"
				fi
			fi
			echo "Autorization required to execute '$INSTALL_COMMAND' as root"
			pkexec $INSTALL_COMMAND
		fi
	fi

    if [ ! -z $DISPLAY ] ; then
	bin/turboprint --setup
	if [ $? -eq 0 ] ; then
		exit 0
	else
		echo "Could not start X-Windows installation tool."
		echo "make sure that X-Windows is running and accessible."
		echo ""
	fi
    fi
fi

echo
echo "TURBOPRINT INSTALLATION"


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

case $TP_LANGUAGE in
de)
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_LIB="Skriptdateien..............:"
TP_PATH_LOG="Logfiles...................:"
TP_PATH_DOC="Dokumentation..............:"
TP_PATH_BROWSER="Browser für PDF-Handuch....:"
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="PDF Handbuch ist im Verzeichnis"
TP_DOCU1="Wollen Sie die Anleitung jetzt lesen (y/n)? "
TP_WAIT_BROWSER="Browser wird in einigen Sekunden gestartet. Bitte warten..."
TP_INSTALLINGFILES="Programmdateien werden kopiert..."
TP_DONE="fertig"
;;
es)
TP_CF_NOT_FOUND="No se han encontrado los archivos de configuración"
TP_CF_NOT_FOUND1="Por favor, cambie primero al directorio de instalación"
TP_KEYFILE_FOUND="Se ha encontrado el archivo de clave en el directorio de instalación."
TP_KEYFILE_FOUND1="Instalando la versión completa de TurboPrint."
TP_KEYFILE_INCORRECT="¡Se ha encontrado un archivo de clave incorrecto!"
TP_KEYFILE_INCORRECT1="Quizá el archivo de c¡lave no haya sido correctamente guardado desde el programa de correo."
TP_KEYFILE_INCORRECT2="¿Desea continuar e instalar la versión Free Edition de TurboPrint (s/n)?"
TP_KEYFILE_NOT="No se ha encontrado archivo de clave en el directorio de instalación."
TP_KEYFILE_NOT1="Instalando TurboPrint Free Edition."
TP_PATH_CONFIG="Los archivos de configuración se guardarán en.."
TP_PATH_SHARED="Los archivos compartidos se guardarán en......."
TP_PATH_BIN="Los archivos ejecutables se guardarán en......."
TP_PATH_LIB="Los archivos de script se guardarán en........."
TP_PATH_LOG="Los archivos de log se guardarán en............"
TP_PATH_DOC="La documentación se guardará en................"
TP_PATH_BROWSER="Explorador para la documentación..............."
TP_CUPS_MODE="Installation for CUPS printing system (TP_CUPS=1)"
TP_LPR_MODE="Instalaciçon para LPR/LPRng spooler (TP_CUPS=0)"
TP_PATH_EDIT="(editar system.cfg para cambiar las rutas de instalación)"
TP_ASK_BEGIN="¿Comenzar ahora la instalación (s/n)? "
TP_DOCU="La documentación PDF puede encontrarse en $TPPATH_DOC"
TP_DOCU1="¿Desea leer ahora el manual (s/n)? "
TP_WAIT_BROWSER="El explorador se abrirá en unos segundos. Por favor, espere..."
TP_INSTALLINGFILES="Copiando archivos de programa..."
TP_DONE="hecho"
;;
*)
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_LIB="Scripts files will go to........"
TP_PATH_LOG="Logfiles files will go to......."
TP_PATH_DOC="Documentation will go to........"
TP_PATH_BROWSER="Browser for PDF manual.........."
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="PDF mnaual can be found in $TPPATH_DOC"
TP_DOCU1="Do you want to read the manual now (y/n)? "
TP_WAIT_BROWSER="Browser will start in a few seconds. Please wait..."
TP_INSTALLINGFILES="Copying program files..."
TP_DONE="done"
;;
esac

# get dirs

if [ ! -e system.cfg ] ; then
	echo "$TP_CF_NOT_FOUND"
	echo "$TP_CF_NOT_FOUND1"
	echo "RESULT 1"
	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)
mkdir -p "$TPPATH_LOG/turboprint"
chmod 755 "$TPPATH_LOG/turboprint"
LOGFILE="$TPPATH_LOG/turboprint/install.log"
echo "#######################################################" >> "$LOGFILE"
echo "setup started $(date)" >> "$LOGFILE"

if [ $TP_KEYFILE ] ; then
	bin/tpsetup --printkey "$TP_KEYFILE" >> "$LOGFILE" 2>&1
else
	bin/tpsetup --printkey *.tpkey >> "$LOGFILE" 2>&1
fi
result=$?

# 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_LIB $TPPATH_FILTERS"
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
echo $TP_INSTALLINGFILES
echo "PROGRESS 10"
lib/install-static --tgz
echo $TP_DONE
echo "PROGRESS 30"

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

lib/install-post --tgz --cups=$TP_CUPS --language=$TP_LANGUAGE --demotype=$TP_DEMOTYPE --charset=$TP_CHARSET --browser=$TPBIN_BROWSER --user=$TP_USER --display=$TP_DISPLAY --gimp=$TP_GIMP --bjnp=$TP_BJNP --startmenu=$TP_STARTMENU --desktop=$TP_DESKTOP --gszedo=$TP_GSZEDO 2>> "$LOGFILE"

echo "PROGRESS 60"

# install keyfile if present

if [ $result -eq 0 ] ; then
	if [ $TP_KEYFILE ] ; then
		bin/tpsetup --install "$TP_KEYFILE" >> "$LOGFILE" 2>&1
	else
		bin/tpsetup --install *.tpkey >> "$LOGFILE" 2>&1
	fi
fi

# call installation subroutine which prints some user information

lib/install-info --tgz --cups=$TP_CUPS --language=$TP_LANGUAGE --demotype=$TP_DEMOTYPE 2>> "$LOGFILE"

echo
echo "$TP_DOCU $TPPATH_DOC"

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/TurboPrint_Manual.pdf" &
fi

echo "PROGRESS 100"
echo "RESULT 0"
exit 0
