#!/bin/sh
#set -x

DATA_FILE=/usr/share/pgsql-testdb/pgdata.sql
PGUSER=postgres
CMDLN_ARGS="$@" # Command line arguments for this script
export CMDLN_ARGS

# Run this script as root if not already.
chk_root () {
  if [ ! $( id -u ) -eq 0 ]; then
    echo "Please enter root's password."
    exec su -c "${0} ${CMDLN_ARGS}" # Call this prog as root
    exit ${?}  # sice we're 'execing' above, we wont reach this exit
               # unless something goes wrong.
  fi
}
chk_root

confirm_install () {
  while read -e -p "About to initialize Postgresql dabase for Mono testing.
This is destructive and will destroy databases currently defined.
You may want to back up /var/lib/pgsql/data first.

Proceed? (y/N) " ANSWER; do
    ANSWER="${ANSWER:-n}"
    ANSWER="${ANSWER/#[Yy]*/y}"
    ANSWER="${ANSWER/#[Nn]*/n}"
    echo $ANSWER
    if [ "$ANSWER" == "y" ]; then 
      return 0
    elif [ "$ANSWER" == "n" ]; then
      echo Exiting\!
      exit 0
    fi
  done
}
echo
confirm_install
echo

RUNNING=true
/etc/init.d/postgresql status
[ $? -gt 0 ] && RUNNING=false

if ! $RUNNING; then
  /etc/init.d/postgresql start
fi

echo "Initializing databases...please hold."
#su - $PGUSER -c "psql -f $DATA_FILE" > /dev/null 2>&1
pushd . > /dev/null 2>&1
cd /usr/share/mono/asp.net/data/
ls | while read FILE; do
  pushd . > /dev/null 2>&1
  cd $FILE
  if [ -f init ]; then
    ./init > /dev/null 2>&1
  fi
  popd > /dev/null 2>&1
done
popd > /dev/null 2>&1

if ! $RUNNING; then
  /etc/init.d/postgresql stop
else
  /etc/init.d/postgresql restart
fi
echo "Done."

echo "Modifying /var/lib/pgsql/data/pg_hba.conf."
if ! grep "^host[[:space:]]*all[[:space:]]*test[[:space:]]*127.0.0.1/32[[:space:]]*md5$" /var/lib/pgsql/data/pg_hba.conf /var/lib/pgsql/data/pg_hba.conf > /dev/null 2>&1 ; then
   cp -v /var/lib/pgsql/data/pg_hba.conf /var/lib/pgsql/data/pg_hba.conf.bak
   sed -i -e 's@^host[[:space:]]*all[[:space:]]*all[[:space:]]*127.0.0.1/32[[:space:]]*ident.*$@host    all         test        127.0.0.1/32       md5@' /var/lib/pgsql/data/pg_hba.conf
fi
echo "Done."

confirm_mono_x_config () {
   echo
   while read -e -p "Is the SLES Mono Extension installed? (y/N)" ANSWER; do
      ANSWER="${ANSWER:-n}"   # Default answer
      ANSWER="${ANSWER/#[Yy]*/y}"
      ANSWER="${ANSWER/#[Nn]*/n}"
      echo $ANSWER
      if [ "$ANSWER" == "y" ]; then 
	 sed -i -e 's@/usr/bin/mod-mono-server.*"@/opt/novell/mono/bin/mod-mono-server2"@' /etc/apache2/conf.d/*conf
	 return 0
      elif [ "$ANSWER" == "n" ]; then
	 return 0
      fi
   done
}

confirm_apache_conf_install () {
   echo
   while read -e -p "Do you want the apache conf files for installed ASP.net apps
installed for use with Apache? (Y/n)" ANSWER; do
      ANSWER="${ANSWER:-y}"   # Default answer
      ANSWER="${ANSWER/#[Yy]*/y}"
      ANSWER="${ANSWER/#[Nn]*/n}"
      echo $ANSWER
      if [ "$ANSWER" == "y" ]; then 
	 find /usr/share/mono/asp.net/conf/ -type f -iname "*conf" | xargs -i cp -v "{}" /etc/apache2/conf.d/
	 confirm_mono_x_config
	 return 0
      elif [ "$ANSWER" == "n" ]; then
	 return 0
      fi
   done
}
echo
confirm_apache_conf_install
echo

