#!/bin/bash
#
#       /etc/rc.d/init.d/mdservice
#
# Starts the mdserver daemon
#
# chkconfig: 345 95 5
# description: Starts the metadata service daemon mdserver
# processname: amgad

# Source function library.
. /etc/init.d/functions

if test x$GLITE_LOCATION = x ; then
  echo GLITE_LOCATION not set, assuming /usr
  GLITE_LOCATION=/usr
fi

if test x$GLITE_LOCATION_VAR = x ; then 
  echo GLITE_LOCATION_VAR not set, assuming /var
  GLITE_LOCATION_VAR=/var
fi

if test x$GLITE_LOCATION_ETC = x ; then 
  echo GLITE_LOCATION_ETC not set, assuming /etc
  GLITE_LOCATION_ETC=/etc
fi


if test ! -d ${GLITE_LOCATION_VAR}/log ; then
  mkdir -p ${GLITE_LOCATION_VAR}/log
fi

if test ! -x ${GLITE_LOCATION}/bin/amgad ; then
  echo Error: ${GLITE_LOCATION}/bin/amgad does not exist
  exit 0
fi


LD_T=`uname -a | grep x86_64 | head -c 1`

if test x$LD_T = x ; then
  export GLITE_LOCATION_LIB=$GLITE_LOCATION/lib
else
  export GLITE_LOCATION_LIB=$GLITE_LOCATION/lib64
fi

if test x$LD_LIBRARY_PATH = x ; then
  export LD_LIBRARY_PATH=$GLITE_LOCATION_LIB
else
  CHECK_LPATH=`echo $LD_LIBRARY_PATH | grep $GLITE_LOCATION`
  if test x$CHECK_LPATH = x ; then
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$GLITE_LOCATION_LIB
  fi
fi


OPTIONS="-c${GLITE_LOCATION_ETC}/amgad.config -D -l${GLITE_LOCATION_VAR}/log/amgad.log"
[ -e /etc/sysconfig/amgad ] && . /etc/sysconfig/amgad

RETVAL=0

#
#       See how we were called.
#

prog="amgad"

start() {
        # Check if amgad is already running
        if [ ! -f /var/lock/subsys/amgad ]; then
            echo -n "Starting $prog: "
            daemon ${GLITE_LOCATION}/bin/amgad $OPTIONS
            RETVAL=$?
            [ $RETVAL -eq 0 ] && touch /var/lock/subsys/amgad
            echo
	else 
            echo mdservice already running.  
            echo If you are sure that this is not the case, delete /var/lock/subsys/amgad
            echo and try again.
            RETVAL=1
        fi
        return $RETVAL
}

stop() {
        echo -n "Stopping $prog: "
        killproc /${GLITE_LOCATION}/bin/amgad
        RETVAL=$?
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/amgad
        echo
        return $RETVAL
}


restart() {
        stop
        start
}       

reload() {
        restart
}       

status_at() {
        status ${GLITE_LOCATION}/bin/amgad
}

fin() {

  sudo -u postgres dropdb metadata
  RETVAL=$?
  [ $RETVAL -ne 0 ] && echo "Fail to drop DB" 

  sudo -u postgres dropuser arda > /dev/null 2> /dev/null
  RETVAL=$?
  [ $RETVAL -ne 0 ] && echo "Fail to drop user" 

  sed '/^host[ 	]*metadata[ 	]*arda/d' /var/lib/pgsql/data/pg_hba.conf > /tmp/pg_hba.conf
  sed '/^local[ 	]*metadata[ 	]*arda/d' /tmp/pg_hba.conf > /var/lib/pgsql/data/pg_hba.conf 

}

init() {

if [ -f /etc/odbc.ini ]; then
AMGA_ODBC=`grep "PSQL" /etc/odbc.ini | head -c 4`
fi

if [ -z $AMGA_ODBC ]; then
cat >> /etc/odbc.ini <<EOT

[PSQL]
Driver          = PostgreSQL_AMGA
Database        = metadata
Servername      = localhost
Port            = 5432
User            = arda
ReadOnly        = No
EOT
fi

if [ -f /etc/odbcinst.ini ]; then
AMGA_ODBCINST=`grep "PostgreSQL_AMGA" /etc/odbcinst.ini | head -c 10`
fi

if [ -z $AMGA_ODBCINST ]; then
cat >> /etc/odbcinst.ini <<EOT

[PostgreSQL_AMGA]
Description     = ODBC for AMGA PostgreSQL
FileUsage       = 1
CPTimeout       = 120
CPReuse         =
Debug           = 0
Threading       = 1
CommLog         = 0
DontDLClose     = 1
EOT

OS_PLAT=`uname -a | grep x86_64 | head -c 1`

if [ -z $OS_PLAT ]; then
cat >> /etc/odbcinst.ini <<EOT
Driver          = /usr/lib/psqlodbc.so
EOT
else
cat >> /etc/odbcinst.ini <<EOT
Driver          = /usr/lib64/psqlodbc.so
EOT
fi

fi


POSTGRES_SERVER=`rpm -qa | grep postgresql-server | head -c 1`
if [ -z $POSTGRES_SERVER ]; then
  yum -y install postgresql-server
fi


echo "restarting postgresql"
/etc/init.d/postgresql stop
/etc/init.d/postgresql start


POSTGRES_HBA=`grep "local[ 	]*metadata[ 	]*arda[ 	]*trust"  /var/lib/pgsql/data/pg_hba.conf | head -c 4`
if [ -z $POSTGRES_HBA ]; then
  sed -e 's/# TYPE[ \t]*DATABASE[ \t]*USER[ \t]*CIDR-ADDRESS[ \t]*METHOD/# TYPE\tDATABASE\tUSER\tCIDR-ADDRESS\tMETHOD \nlocal\tmetadata\tarda\ttrust \nhost\tmetadata\tarda\t127\.0\.0\.1\/32\ttrust/' /var/lib/pgsql/data/pg_hba.conf > /tmp/pg_hba.conf.old
  RETVAL=$?
  [ $RETVAL -ne 0 ] && echo "Fail to create pg_hba.conf" && return $RETVAL
  sudo -u postgres cp -f /tmp/pg_hba.conf.old /var/lib/pgsql/data/pg_hba.conf >/dev/null 2>/dev/null
fi

echo "restarting postgresql"
/etc/init.d/postgresql stop
/etc/init.d/postgresql start

DBNAME="metadata"
psql -q -U arda $DBNAME -c "select count(*) from masterindex" > /dev/null 2> /dev/null
RETVAL=$?
if [ $RETVAL -ne 0 ]; then 
  sudo -u postgres createuser -S -d -r arda > /dev/null 2> /dev/null
  
  sudo -u postgres createdb -O arda $DBNAME
  RETVAL=$?
  [ $RETVAL -ne 0 ] && echo "Fail to create DB" && return $RETVAL

  sudo -u postgres createlang plpgsql $DBNAME
  RETVAL=$?
  [ $RETVAL -ne 0 ] && echo "Fail to create DB" && return $RETVAL

  psql -q -U arda $DBNAME < $GLITE_LOCATION/share/doc/glite-amga-server/createInitialPG.sql > /dev/null 2> /dev/null
  RETVAL=$?
  [ $RETVAL -ne 0 ] && echo "Fail to insert metadata" && return $RETVAL

fi

echo_success
echo "Initializing AMGA : "
return 0
}

case "$1" in
start)
        start
        ;;
stop)
        stop
        ;;
init)
        init
        ;;
fin)
        fin
        ;;
reload|restart)
        restart
        ;;
condrestart)
        if [ -f /var/lock/subsys/amgad ]; then
            restart
        fi
        ;;
status)
        status_at
        ;;
*)
        echo $"Usage: $0 {start|stop|restart|condrestart|status|init|fin}"
        exit 1
esac

exit $?
exit $RETVAL
