#!/bin/bash
#
# postinstall - a little script to install stuff over the net...
#
# Copyright (C) 2004 Ilja Gerhardt <ilja@so36.de>
#
# This source code is free software; you can redistribute it and/or
# modify it under the terms of the GNU Public License as published 
# by the Free Software Foundation; either version 2 of the License,
# or (at your option) any later version.
#
# This source code is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# Please refer to the GNU Public License for more details.
#
# You should have received a copy of the GNU Public License along with
# this source code; if not, write to:
# Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

DIALOG="dialog"
[ -n "$DISPLAY" ] && [ -x /usr/bin/Xdialog ] && DIALOG="Xdialog"

PREFIX=postinst://
AGENT="StreamBOX 1.1"

URL=`echo $1 | sed s#$PREFIX#https://#g`
PAKET=`echo $1 | sed s#.*\/##g`

###############
# getting the strings in relation to the settings

[ -f /etc/sysconfig/i18n ] && . /etc/sysconfig/i18n

case "$LANGUAGE" in
de|at|ch)
TI=" StreamBOX Postinstall "
BT="StreamBOX"
M01="StreamBOX Postinstall versucht '$PAKET' auf diesem Computer zu installieren\n\nIch versuche jetzt die Pakete über das Internet zu holen\nInstallation erlauben?"
P01="FEHLER - Die Unterschrift für das Paket '$PAKET' stimmt leider nicht... OK zum Beenden."
M02="Das Paket '$PAKET' wird nicht installiert - OK zum Beenden"
M03="Sollen wir das Script einfach so ausführen [Ja] ? Oder soll es noch angezeigt werden [Nein]?"
M04="Soll das soeben angezeigte Script wirklich ausgeführt werden?"
M05="Die Pakete für '$PAKET' wurden aus dem Internet geholt\n Jetzt installieren?"
M06="'$PAKET' wurde erfolgreich installiert"
;;
*)
TI=" StreamBOX Postinstall "
BT="StreamBOX"
M01="Postinstall tries to install '$PAKET' on your computer - ok?\n\nI'll try to fetch the packages from the net\nAllow?!"
P01="ERROR - Signatur of '$PAKET' is unfortunately wrong"
M02="'$PAKET' won't be installed - goodbye"
M03="Just execute the script, even without showing it?"
M04="Should the script really be executed?"
M05="The packages for '$PAKET' were fetched, the signature was checked - Install now?"
M06="'$PAKET' was installed successfully"
;;
esac

##############
# print start dialog
$DIALOG --title "$TI" --backtitle "$BT" --yesno "$M01" 0 0
value=$?
if [ $value != 0 ]
then 
	$DIALOG --title "$TI" --backtitle "$BT" --msgbox "$M02" 0 0
	exit 0
fi

SCRIPT=`tempfile 2>/dev/null` || tempfile=/tmp/test$$

wget $URL.sh -U "$AGENT" -O $SCRIPT.sh
wget $URL.sh.sig -U "$AGENT" -O $SCRIPT.sh.sig

##############
# Check signature

$DIALOG --title "$TI" --backtitle "$BT" --yesno "$M03" 0 0
        value=$?
        if [ $value != 0 ]
        then
                nedit $SCRIPT.sh
		ASKAGAIN=1;
	else
		ASKAGAIN=0;
        fi

##############
# Ask again
if [ $ASKAGAIN == 1 ]
then
	$DIALOG --title "$TI" --backtitle "$BT" --yesno "$M04" 0 0
	value=$?
	if [ $value != 0 ]
	then
		$DIALOG --title "$TI" --backtitle "$BT" --msgbox "$M02" 0 0
		exit 0
	fi
fi

gpgv $SCRIPT.sh.sig
value=$?

if [ $value == 0 ]
then 
		sh $SCRIPT.sh
else
	$DIALOG --colors --title "$TI" --backtitle "$BT" --msgbox "$P01" 0 0
	exit 1
fi

##############
# print goodbye dialog

rm -f $SCRIPT.sh
rm -f $SCRIPT.sh.sig

$DIALOG --title "$TI" --backtitle "$BT" --msgbox "$M06" 0 0


