#!/bin/bash

CONFIGFILE=/etc/linuxmuster/community-feedback.conf

if [ ! -f $CONFIGFILE ]; then 
	touch $CONFIGFILE
fi

confstat1=$(md5sum /etc/linuxmuster/community-feedback.conf | cut -d" " -f1)

# Source debconf library.
. /usr/share/debconf/confmodule

db_version 2.0


db_get linuxmuster-community-feedback/sendstats
db_input high linuxmuster-community-feedback/sendstats || true
db_go
db_get linuxmuster-community-feedback/sendstats
if [ "$RET" ]; then
    SENDSTATS=$RET;
fi

if [ "$RET" = "true" ]; then 

db_get linuxmuster-community-feedback/purpose
db_input high linuxmuster-community-feedback/purpose || true
db_go
db_get linuxmuster-community-feedback/purpose
if [ "$RET" ]; then
    PURPOSE=$RET;
fi


db_get linuxmuster-community-feedback/virtualized
db_input high linuxmuster-community-feedback/virtualized || true
db_go
db_get linuxmuster-community-feedback/virtualized
if [ "$RET" ]; then
    VIRTUALIZED=$RET;
fi

db_get linuxmuster-community-feedback/itsupport
db_input high linuxmuster-community-feedback/itsupport || true
db_go
db_get linuxmuster-community-feedback/itsupport
if [ "$RET" ]; then
    ITSUPPORT=$RET;
fi


db_get linuxmuster-community-feedback/bundesland
db_input high linuxmuster-community-feedback/bundesland || true
db_go
db_get linuxmuster-community-feedback/bundesland
if [ "$RET" ]; then
    BUNDESLAND=$RET;
fi

if [ "$BUNDESLAND" = "Nicht in Deutschland" ]; then 
	db_get linuxmuster-community-feedback/foreigncountry
	db_input high linuxmuster-community-feedback/foreigncountry || true
	db_go
	db_get linuxmuster-community-feedback/foreigncountry
	if [ "$RET" ]; then
	    FOREIGNCOUNTRY=$RET;
	fi

fi

db_get linuxmuster-community-feedback/sendusers
db_input high linuxmuster-community-feedback/sendusers || true
db_go
db_get linuxmuster-community-feedback/sendusers
if [ "$RET" ]; then
    SENDUSERS=$RET;
fi

db_get linuxmuster-community-feedback/sendclients
db_input high linuxmuster-community-feedback/sendclients || true
db_go
db_get linuxmuster-community-feedback/sendclients
if [ "$RET" ]; then
    SENDCLIENTS=$RET;
fi

db_get linuxmuster-community-feedback/sendschoolname
db_input high linuxmuster-community-feedback/sendschoolname || true
db_go
db_get linuxmuster-community-feedback/sendschoolname
if [ "$RET" ]; then
    SENDSCHOOLNAME=$RET;
fi

if [ "$SENDSCHOOLNAME" = true ]; then 
	db_get linuxmuster-community-feedback/schoolname
	db_input high linuxmuster-community-feedback/schoolname || true
	db_go
	db_get linuxmuster-community-feedback/schoolname
	if [ "$RET" ]; then
	    SCHOOLNAME=$RET;
	fi
fi


db_get linuxmuster-community-feedback/sendlocation
db_input high linuxmuster-community-feedback/sendlocation || true
db_go
db_get linuxmuster-community-feedback/sendlocation
if [ "$RET" ]; then
    SENDLOCATION=$RET;
fi

if [ "$SENDLOCATION" = true ]; then 
	db_get linuxmuster-community-feedback/plz
	db_input high linuxmuster-community-feedback/plz || true
	db_go
	db_get linuxmuster-community-feedback/plz
	if [ "$RET" ]; then
	    PLZ=$RET;
	fi
	if [  "x$SCHOOLNAME" != "x" ]; then 
		db_get linuxmuster-community-feedback/shownameinmap
		db_input high linuxmuster-community-feedback/shownameinmap || true
		db_go
		db_get linuxmuster-community-feedback/shownameinmap
		if [ "$RET" ]; then
		    SHOWNAMEINMAP=$RET;
		fi
	fi
fi


fi


# new an deleted values to config
test -z "$SENDSTATS" || grep -Eq '^ *SENDSTATS=' $CONFIGFILE || \
            echo "SENDSTATS=" >> $CONFIGFILE
test -z "$SENDUSERS" || grep -Eq '^ *SENDUSERS=' $CONFIGFILE || \
            echo "SENDUSERS=" >> $CONFIGFILE
test -z "$SENDCLIENTS" || grep -Eq '^ *SENDCLIENTS=' $CONFIGFILE || \
            echo "SENDCLIENTS=" >> $CONFIGFILE
test -z "$SENDLOCATION" || grep -Eq '^ *SENDLOCATION=' $CONFIGFILE || \
            echo "SENDLOCATION=" >> $CONFIGFILE
test -z "$VIRTUALIZED" || grep -Eq '^ *VIRTUALIZED=' $CONFIGFILE || \
            echo "VIRTUALIZED=" >> $CONFIGFILE
test -z "$ITSUPPORT" || grep -Eq '^ *ITSUPPORT=' $CONFIGFILE || \
            echo "ITSUPPORT=" >> $CONFIGFILE
test -z "$SENDSCHOOLNAME" || grep -Eq '^ *SENDSCHOOLNAME=' $CONFIGFILE || \
            echo "SENDSCHOOLNAME=" >> $CONFIGFILE
test -z "$BUNDESLAND" || grep -Eq '^ *BUNDESLAND=' $CONFIGFILE || \
            echo "BUNDESLAND=" >> $CONFIGFILE
test -z "$FOREIGNCOUNTRY" || grep -Eq '^ *FOREIGNCOUNTRY=' $CONFIGFILE || \
            echo "FOREIGNCOUNTRY=" >> $CONFIGFILE
test -z "$SCHOOLNAME" || grep -Eq '^ *SCHOOLNAME=' $CONFIGFILE || \
            echo "SCHOOLNAME=" >> $CONFIGFILE
test -z "$CONTACTMAIL" || grep -Eq '^ *CONTACTMAIL=' $CONFIGFILE || \
            echo "CONTACTMAIL=" >> $CONFIGFILE
test -z "$SHOWNAMEINMAP" || grep -Eq '^ *SHOWNAMEINMAP=' $CONFIGFILE || \
            echo "SHOWNAMEINMAP=" >> $CONFIGFILE
test -z "$PLZ" || grep -Eq '^ *PLZ=' $CONFIGFILE || \
            echo "PLZ=" >> $CONFIGFILE
test -z "$PURPOSE" || grep -Eq '^ *PURPOSE=' $CONFIGFILE || \
            echo "PURPOSE=" >> $CONFIGFILE

# write config
cp -a -f $CONFIGFILE $CONFIGFILE.tmp
sed -e "s/^ *SENDSTATS=.*/SENDSTATS=\"$SENDSTATS\"/" \
    -e "s/^ *SENDUSERS=.*/SENDUSERS=\"$SENDUSERS\"/" \
    -e "s/^ *SENDCLIENTS=.*/SENDCLIENTS=\"$SENDCLIENTS\"/" \
    -e "s/^ *SENDLOCATION=.*/SENDLOCATION=\"$SENDLOCATION\"/" \
    -e "s/^ *PLZ=.*/PLZ=\"$PLZ\"/" \
    -e "s/^ *VIRTUALIZED=.*/VIRTUALIZED=\"$VIRTUALIZED\"/" \
    -e "s/^ *ITSUPPORT=.*/ITSUPPORT=\"$ITSUPPORT\"/" \
    -e "s/^ *SENDSCHOOLNAME=.*/SENDSCHOOLNAME=\"$SENDSCHOOLNAME\"/" \
    -e "s/^ *SCHOOLNAME=.*/SCHOOLNAME=\"$SCHOOLNAME\"/" \
    -e "s/^ *SHOWNAMEINMAP=.*/SHOWNAMEINMAP=\"$SHOWNAMEINMAP\"/" \
    -e "s/^ *BUNDESLAND=.*/BUNDESLAND=\"$BUNDESLAND\"/" \
    -e "s/^ *FOREIGNCOUNTRY=.*/FOREIGNCOUNTRY=\"$FOREIGNCOUNTRY\"/" \
    -e "s/^ *PURPOSE=.*/PURPOSE=\"$PURPOSE\"/" \
    < $CONFIGFILE > $CONFIGFILE.tmp
mv -f $CONFIGFILE.tmp $CONFIGFILE

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

db_stop

# Cronjob anlegen...
rm -f /etc/cron.d/linuxmuster-community-feedback
if [ $SENDSTATS = "true" ]; then 
	minute=$(( $RANDOM % 59 ))
	day=$(($(( $RANDOM % 2 )) * 6))
	echo "# Diese Datei wird automatisch erstellt."  > /etc/cron.d/linuxmuster-community-feedback
	echo "# Manuelle Aenderungen werden ueberschrieben!"  >> /etc/cron.d/linuxmuster-community-feedback
	echo "$minute 4 * * $day   root /usr/bin/linuxmuster-community-feedback -u > /dev/null 2>&1" >> /etc/cron.d/linuxmuster-community-feedback
fi 

confstat2=$(md5sum /etc/linuxmuster/community-feedback.conf | cut -d" " -f1)

if [ $SENDSTATS = "true" -a "$confstat2" != "$confstat1" ]; then 
	message=$(/usr/bin/linuxmuster-community-feedback -v)
	dialog --title "Darf ich folgende Daten aktuell halten und senden?" --scrollbar --msgbox "$message" 0 0 
	/usr/bin/linuxmuster-community-feedback -u > /dev/null 2>&1
	clear
fi



exit 0
