#!/bin/sh -e

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

db_version 2.0
db_capb backup

# Reset passwords if reconfigure
if [ "$1" = "reconfigure" ] ; then
	db_reset dotlrn/dba_password
	db_reset dotlrn/dba_confirm
	db_reset dotlrn/dbu_password
	db_reset dotlrn/dbu_confirm	
fi

STATE=1
while [ "$STATE"  != 0 -a "$STATE" != 10 ]
  do
  case "$STATE" in
      1)
	  db_input critical dotlrn/db_host || true
	  if db_go; then
	      db_get dotlrn/db_host || true
	      if [ ! -z "$RET" ]; then
		  STATE=2
	      fi
              pg_host="$RET"
	  else
	      STATE=1
	  fi
	  ;;

      2)
	  db_input critical dotlrn/dba_name || true
	  if db_go; then
	      db_get dotlrn/dba_name || true
	      if [ ! -z "$RET" ]; then
		  STATE=3
	      fi
	  else
	      STATE=1
	  fi
	  ;;

      3)  
	  db_input critical dotlrn/dba_password || true
	  if db_go; then
	      db_get dotlrn/dba_password || true
	      STATE=5
	  else
	      STATE=2
	  fi
	  ;;

      4) 
	  db_input critical dotlrn/dbu_name || true
	  if db_go; then
	      db_get dotlrn/dbu_name || true
	      if [ ! -z "$RET" ]; then
		  STATE=5
	      fi
	  else
	      STATE=2
	  fi
	  ;;

      5)  
	  db_input critical dotlrn/dbu_password || true
	  if db_go; then
	      db_get dotlrn/dbu_password || true
	      STATE=6
	  else
	      STATE=4
	  fi
	  ;;

      6)  
	  db_input critical dotlrn/dbu_confirm || true
	  if db_go; then
	      db_get dotlrn/dbu_confirm || true
	      CONFIRM="$RET"
	      db_get dotlrn/dbu_password || true
	      if [ A"$RET" != A"$CONFIRM" ]; then
		  STATE=7
	      else
                  # Check if host is localhost to change 
                  # the postgresql access permissions properly.
                  if [ "$pg_host" = "localhost" ]; then
                      STATE=8 
                  else
                      STATE=9
                  fi
	      fi
	  else
	      STATE=4
	  fi
	  ;;

      7)
	  db_input critical dotlrn/mismatch || true
	  db_go
	  STATE=4
	  ;;


      8) db_input critical dotlrn/pg_grant_access || true
          db_go
          STATE=9
          ;;
         
      9)
	  db_input critical dotlrn/create_tables || true
	  db_go
	  STATE=10
	  ;;
  esac
done

