#!/usr/bin/env bash
#
# Copyright (C) 2003 VA Linux Systems Japan, K.K.
#
# LICENSE NOTICE
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#

#
# Usage:
#    update-failover <configure|remove|sanity>
#

# $Id: update-failover,v 1.23 2004/10/13 11:45:18 taru Exp $

set -e

eval `ultrapossum-config init`

tmp=`tempfile`
trap "rm -f $tmp; eval `ultrapossum-config term`" 0

configure_mon() {
  monid="MON"
  if test "x$TYPE" = "xmaster" && test "x$BACKUP" != "x"; then
    progress "Configuring mon... "
    add_startmark "##" $monid > $ULTRAPOSSUM_MON_CF
    $SHAREDIR/sedwrap \
       < $ULTRAPOSSUM_MON_CFIN >> $ULTRAPOSSUM_MON_CF
    add_endmark "##" $monid >> $ULTRAPOSSUM_MON_CF
    add_end_vaconf $MONCF $ULTRAPOSSUM_MON_CF $monid
    progress "Configuring mon... done"

    if test "x$MONUSER" != "xroot"; then
      progress "Registering $MONUSER as sudoers... "
      add_startmark "##" $monid > $tmp
      echo "$MONUSER ALL = (ALL) NOPASSWD: $MODULEDIR/server/startup" >> $tmp
      echo "$MONUSER ALL = (ALL) NOPASSWD: $MODULEDIR/failover/statuscpy" >> $tmp
      add_endmark "##" $monid >> $tmp
      add_end_vaconf $SUDOERS $tmp $monid
      chmod 440 $SUDOERS
      progress "Registering $MONUSER as sudoers... done"
    else
      remove_mon_sudoers
    fi

  else
    remove_mon
  fi
}

remove_mon_sudoers()
{
  if test -f "$SUDOERS"; then
    strip_vaconf $SUDOERS $monid
    chmod 440 $SUDOERS
  fi
}

remove_mon()
{
  remove_mon_sudoers
  if test -f "$MONCF"; then strip_vaconf $MONCF $monid; fi
  /bin/rm -f $ULTRAPOSSUM_MON_CF
}

configure_heartbeat() {
  if test "x$TYPE" = "xmaster" && test "x$BACKUP" != "x"; then
    progress "Configuring heartbeat... "
    strip_vaconf $HACF "HACF"

    # ha.cf configuration
    add_startmark "##" "HACF" > $tmp
    for n in $MASTER $BACKUP
    do
      if egrep "^node $n" $HACF > /dev/null; then
        :
      else
        echo "node $n" >> $tmp
      fi
    done
    add_endmark "##" "HACF" >> $tmp
    add_end_vaconf $HACF $tmp "HACF"

    # haresources configuration
    if test "x$ULTRAPOSSUMMARK" = "xULTRAPOSSUM"; then
      add_startmark "##" "HARESOURCES" > $tmp
      if include "$BACKUP" "$SYNCBACKUPS"; then
        echo "$MASTER syncbackup $VIRTUAL $ULTRAPOSSUMSERVICES" >> $tmp
      else
        echo "$MASTER $VIRTUAL $ULTRAPOSSUMSERVICES" >> $tmp
      fi
      add_endmark "##" "HARESOURCES" >> $tmp
      add_end_vaconf $HARESOURCES $tmp "HARESOURCES"
    fi
    if include "$BACKUP" "$SLURPDSLAVES"; then
      install -d $NFSDIR/$RPLDIR/$BACKUP
      install -d $NFSDIR/$RPLDIR/$MASTER
      install -d $NFSDIR/$RPLDIR/backup
      touch $NFSDIR/$RPLDIR/backup/slurpd.status
    fi
    progress "Configuring heartbeat... done"
  else
    if test -f $HACF; then strip_vaconf $HACF "HACF"; fi
    if test -f $HARESOURCES; then strip_vaconf $HARESOURCES "HARESOURCES"; fi
  fi
}

sanity_fstab() {
  if test `uname` = "SunOS"; then
    :
  else
    if test "x$BACKUP" != "x" &&
    	! grep "$NFSDEVICE" $FSTAB 2> /dev/null | grep "$NFSDIR" > /dev/null
    then
      if test "x$NFSDIR" = "x" || test "x$NFSDEVICE" = "x"; then
        echo "E: No NFS configuration in $FSTAB" 1>&2
	exit 1
      fi
    fi
  fi
}

configure_fstab() {
  progress "Configuring NFS... "
  if test `uname` = "SunOS"; then
    :
  else
    if test "x$BACKUP" != "x" && test "x$TYPE" = "xmaster"; then
      if ! grep "$NFSDEVICE" $FSTAB 2> /dev/null | grep "$NFSDIR" > /dev/null
      then
        add_startmark "##" "FAILOVER" > $tmp
        echo "$NFSDEVICE	$NFSDIR	$SHAREFSTYPE	rw	0	0" >> $tmp
        add_endmark "##" "FAILOVER" >> $tmp
        add_end_vaconf $FSTAB $tmp "FAILOVER"
      fi
    else
      remove_fstab
    fi
  fi
  progress "Configuring NFS... done"
}

remove_fstab() {
  if test `uname` = "SunOS"; then
    :
  else
    strip_vaconf $FSTAB "FAILOVER"
  fi
}


sanity_check() {
  sanity_error=0
  if test "x$TYPE" = "xmaster" && test "x$BACKUP" != "x"; then
    if ! test -x "/etc/init.d/heartbeat"; then
      echo "E: No /etc/init.d/heartbeat found" 1>&2
      exit 1
    fi
    if ! test -x "/etc/init.d/mon"; then
      echo "E: No /etc/init.d/mon found" 1>&2
      exit 1
    fi
    if ! include "$BACKUP" "$SLAVES"; then
      echo "E: BACKUP '$BACKUP' is not a slave server" 1>&2
      exit 1
    fi
    val=`getvalue $DIRECTORY/ultrapossum-environ SLURPDSLAVES 2> /dev/null`
    if test "x$val" != "x" && test "x$val" != "x$SLURPDSLAVES"; then
      echo "E: unable to change slurpd slave servers when failover environment" 1>&2
      exit 1
    fi
    if test -f $HACF; then
      :
    else
      progress "Installing New version of ha.cf... "
      $MODULEDIR/failover/ha.cf.sh > $HACF
      progress "Installing New version of ha.cf... done"
    fi
    if test -f $HARESOURCES; then
      :
    else
      progress "Installing New Version of haresources... "
      cat $TEMPLATEDIR/haresources.in > $HARESOURCES
      progress "Installing New Version of haresources... done"
    fi
    if test -f $AUTHKEYS; then
      :
    else
      progress "Installing New version of authkeys... "
      cp /dev/null $AUTHKEYS
      chmod 600 $AUTHKEYS
      $MODULEDIR/failover/authkeys.sh > $AUTHKEYS
      progress "Installing New version of authkeys... done"
    fi
  fi
  if test "x$BACKUP" != "x" && include "$LDAPMASTER" "$MASTER $SLAVES"; then
    echo "E: LDAPMASTER '$LDAPMASTER' must be a virtual name." 1>&2
    sanity_error=1
  fi
  if test "x$BACKUP" != "x" && ! include "$BACKUP" "$SYNCBACKUPS"; then
  # If we use a backup server, need to set NFSDIR or nice_failback on
    if test "x$NFSDIR" = "x"; then
      if test "`egrep "^nice_failback" $HACF | awk -F' ' '{print $2;}'`" != "on"; then
        echo "E: In order to use a failover backup server, need to set NFSDIR or set nice_failback on in ha.cf" 1>&2
        sanity_error=1
      fi
    else
      if test -f "$LOCAL_REPLOGFILE"; then
        echo "E: Replication has been already started." 1>&2
        sanity_error=1
      fi
    fi
  fi
  sanity_fstab
  if test "$sanity_error" != "0"; then
    exit 1
  fi
}

configure() {

  sanity_check

  if test "x$NFSDIR" != "x" && test "x$NFSDEVICE" != "x"; then
    configure_fstab
  else
    remove_fstab
  fi

  sed -e "s/#HOST#/$HOST/" -e "s/#UPDATEDN#/$UPDATEDN/" \
    -e "s/#CREDENTIALS#/$CREDENTIALS/" \
         < $ULTRAPOSSUM_RECOVERY_CONFIN > $ULTRAPOSSUM_RECOVERY_CONF

  configure_mon
  configure_heartbeat

}

remove() {
  remove_mon
  /bin/rm -f $ULTRAPOSSUM_RECOVERY_CONF
  if test -f "$HACF"; then strip_vaconf $HACF "HACF"; fi
  if test -f $HARESOURCES; then strip_vaconf $HARESOURCES "HARESOURCES"; fi
}

case "x$1" in
	xconfigure)
		configure
  		ultrapossum-config set .status ULTRAPOSSUM_MODULE_FAILOVER=installed
	;;
	xremove)
		remove
		ultrapossum-config remove .status ULTRAPOSSUM_MODULE_FAILOVER
	;;
	xsanity)
		sanity_check
	;;
	x)
		echo "Usage: $0 <configure|remove|sanity>" 1>&2
		exit 1
	;;
	x*)
		echo "Unknown argument: $1" 1>&2
		exit 1
	;;
esac

