#!/bin/bash
#
# thomas@linuxmuster.net
# 03.03.2015
# GPL v3
#

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

db_version 2.0
db_capb backup

PRIORITY="critical"

# source linuxmuster defaults
. /usr/share/linuxmuster/config/dist.conf || exit 1

# source helperfunctions
. $HELPERFUNCTIONS || exit 1

db_title "$DISTNAME $DISTFULLVERSION"

# source oldvalues if present
if [ -e $OLDVALUES ]; then
  . $OLDVALUES
  oldvalues=yes
fi

if [ "$1" = "--first" ]; then
 # intro
 db_fset linuxmuster-base/intro seen false
 db_input $PRIORITY linuxmuster-base/intro || true
 db_go
fi


# data for certificates and workgroup
for i in country state location schoolname workgroup; do
 while [ -z "$INPUT" ]; do
  db_fset linuxmuster-base/$i seen false
  db_input $PRIORITY linuxmuster-base/$i || true
  db_go
  db_get linuxmuster-base/$i || true
  INPUT="$RET"
  if ! check_string "$INPUT"; then
   unset INPUT
   continue
  fi
  if [ "$i" = "country" ]; then
   [ ${#RET} -ne 2 ] && unset INPUT && continue
   INPUT=`echo $RET | tr a-z A-Z`
   if ! (expr match "$INPUT" '\([ABCDEFGHIJKLMNOPQRSTUVWXYZ][ABCDEFGHIJKLMNOPQRSTUVWXYZ]\)'); then
    unset INPUT
    continue
   fi
  fi
  [ "$oldvalues" = "yes" -a "$i" = "country" -a "$INPUT" != "$country_old" ] && update_fw=yes
  [ "$oldvalues" = "yes" -a "$i" = "state" -a "$INPUT" != "$state_old" ] && update_fw=yes
  [ "$oldvalues" = "yes" -a "$i" = "location" -a "$INPUT" != "$location_old" ] && update_fw=yes
  [ "$oldvalues" = "yes" -a "$i" = "schoolname" -a "$INPUT" != "$schoolname_old" ] && update_fw=yes
  if [ "$i" = "schoolname" ]; then
   db_set shared/organization $INPUT || true
  fi
 done
 unset INPUT
done


# intern network configuration
# servername
while ! validhostname "$SERVERNAME"; do
 db_fset linuxmuster-base/servername seen false
 db_input $PRIORITY linuxmuster-base/servername || true
 db_go
 db_get linuxmuster-base/servername || true
 SERVERNAME="$RET"
done
[ "$oldvalues" = "yes" -a "$SERVERNAME" != "$servername_old" ] && update_fw=yes

# domainname
while ! validdomain "$DOMAINNAME"; do
 db_fset linuxmuster-base/domainname seen false
 db_input $PRIORITY linuxmuster-base/domainname || true
 db_go
 db_get linuxmuster-base/domainname || true
 DOMAINNAME=$RET
done
[ "$oldvalues" = "yes" -a "$DOMAINNAME" != "$domainname_old" ] && update_fw=yes


# intern subnet range
db_fset linuxmuster-base/internsubrange seen false
db_input $PRIORITY linuxmuster-base/internsubrange || true
db_go


# extern server name
while ! validdomain "$SERVEREXTERNNAME"; do
 db_fset linuxmuster-base/serverexternname seen false
 db_input $PRIORITY linuxmuster-base/serverexternname || true
 db_go
 db_get linuxmuster-base/serverexternname || true
 SERVEREXTERNNAME=$RET
 if [ "$SERVEREXTERNNAME" = "" ]; then
  SERVEREXTERNNAME="$SERVERNAME.$DOMAINNAME"
  db_set linuxmuster-base/serverexternname $SERVEREXTERNNAME || true
  break
 fi
done


# firewall
db_fset linuxmuster-base/fwconfig seen false
db_input $PRIORITY linuxmuster-base/fwconfig || true
db_go
db_get linuxmuster-base/fwconfig || true
fwconfig=$RET
[ "$oldvalues" = "yes" -a "$fwconfig" != "$fwconfig_old" ] && update_fw=yes


# opsi support, not necessary to do this here, done by linuxmuster-opsi
#db_fset linuxmuster-base/opsi seen false
#db_input $PRIORITY linuxmuster-base/opsi || true
#db_go
#db_get linuxmuster-base/opsi || true
#opsi=$RET


# smtp relay
while ! validdomain "$SMTPRELAY"; do
 db_fset linuxmuster-base/smtprelay seen false
 db_input $PRIORITY linuxmuster-base/smtprelay || true
 db_go
 db_get linuxmuster-base/smtprelay || true
 SMTPRELAY=$RET
 if [ "$SMTPRELAY" = "" ]; then break; fi
done


# subnetting
db_fset linuxmuster-base/subnetting seen false
db_input $PRIORITY linuxmuster-base/subnetting || true
db_go
db_get linuxmuster-base/subnetting || true
subnetting=$RET

# admin passwords
if [ "$1" = "--first" ]; then

 # administrator
 STATE=1
 while [ "$STATE"  != 0 ]; do

  case "$STATE" in

   1)
    db_fset linuxmuster-base/adminpw seen false
    db_input $PRIORITY linuxmuster-base/adminpw || true
    db_go
    db_get linuxmuster-base/adminpw || true
    [ -n "$RET" ] && STATE=2
    unset RET
    ;;

   2)
    db_fset linuxmuster-base/adminpw_confirm seen false
    db_input $PRIORITY linuxmuster-base/adminpw_confirm || true
    db_go
    db_get linuxmuster-base/adminpw_confirm || true
    if [ -n "$RET" ]; then
     CONFIRM=$RET
     db_get linuxmuster-base/adminpw || true
     if [ "$RET" = "$CONFIRM" ]; then
      STATE=0
     else
      STATE=1
      db_set linuxmuster-base/adminpw "" || true
     fi
     unset RET
     unset CONFIRM
     db_set linuxmuster-base/adminpw_confirm "" || true
    fi
    ;;

  esac

 done

fi


# opsi password
if [ "$1" = "--modify" -a -n "$opsiip" = "true" ]; then

 STATE=1
 while [ "$STATE"  != 0 ]; do

  case "$STATE" in

   1)
    db_fset linuxmuster-base/opsipw seen false
    db_input $PRIORITY linuxmuster-base/opsipw || true
    db_go
    db_get linuxmuster-base/opsipw || true
    [ -n "$RET" ] && STATE=2
    unset RET
    ;;

   2)
    db_fset linuxmuster-base/opsipw seen false
    db_input $PRIORITY linuxmuster-base/opsipw_confirm || true
    db_go
    db_get linuxmuster-base/opsipw_confirm || true
    if [ -n "$RET" ]; then
     CONFIRM=$RET
     db_get linuxmuster-base/opsipw || true
     if [ "$RET" = "$CONFIRM" ]; then
      STATE=0
     else
      STATE=1
      db_set linuxmuster-base/opsipw "" || true
     fi
     unset RET
     unset CONFIRM
     db_set linuxmuster-base/opsipw_confirm "" || true
    fi
    ;;

  esac

 done

fi # opsi password


# firewall password
if [ "$1" = "--first" -o -n "$update_fw" ]; then
 if [ "$fwconfig" != "custom" ]; then

  STATE=1
  while [ "$STATE"  != 0 ]; do

   case "$STATE" in

    1)
     db_fset linuxmuster-base/ipcoppw seen false
     db_input $PRIORITY linuxmuster-base/ipcoppw || true
     db_go
     db_get linuxmuster-base/ipcoppw || true
     [ -n "$RET" ] && STATE=2
     unset RET
     ;;

    2)
     db_fset linuxmuster-base/ipcoppw_confirm seen false
     db_input $PRIORITY linuxmuster-base/ipcoppw_confirm || true
     db_go
     db_get linuxmuster-base/ipcoppw_confirm || true
     if [ -n "$RET" ]; then
      CONFIRM=$RET
      db_get linuxmuster-base/ipcoppw || true
      if [ "$RET" = "$CONFIRM" ]; then
       STATE=0
      else
       STATE=1
       db_set linuxmuster-base/ipcoppw "" || true
      fi
      unset RET
      unset CONFIRM
      db_set linuxmuster-base/ipcoppw_confirm "" || true
     fi
     ;;

   esac

  done

 fi
fi # firewall password


# assign nic to intern interface
assign_nics ; RC="$?"

# important: close debconf database
db_stop

exit "$RC"
