#!/bin/bash
#
#####
#####  Written by Paul A. Luzzi
#####    on 07-16-2005
#####

#####
#####  Start/override
#####
case "$1" in
  'start') FLAG="start" ;;
  'stop' ) FLAG="stop" ;;
  *      ) FLAG="start" ;;
esac

if [ -z "$1" ]
  then
    FLAG="start"
  fi

#####
#####  Take some action
#####
case "$FLAG" in
'start')
        #####
        #####  Start off the mounting parts
        #####
        echo "Starting drbd force up operations" | tee -a /tmp/start-drbd.log

        #####
        #####  Start with status of current drbd
        #####
        echo "Checking status of drbd0 device ..." | tee -a /tmp/start-drbd.log
        drbdsetup /dev/drbd0 state | tee -a /tmp/start-drbd.log
        if [ ! "$?" = "0" ]
          then
            echo "Starting drbd core services ..." | tee -a /tmp/start-drbd.log
            /etc/init.d/drbd start | tee -a /tmp/start-drbd.log
          fi
        
        #####
        #####  Check on drbdsetup status
        #####
        echo "Current status of drbd0 device ..." | tee -a /tmp/start-drbd.log
        drbdsetup /dev/drbd0 state | tee -a /tmp/start-drbd.log
        ##
        echo "Forcing state to primary of drbd0 device ..." | tee -a /tmp/start-drbd.log
        drbdsetup /dev/drbd0 primary --do-what-I-say | tee -a /tmp/start-drbd.log
        ##
        echo "New status of drbd0 device ..." | tee -a /tmp/start-drbd.log
        drbdsetup /dev/drbd0 state | tee -a /tmp/start-drbd.log
        
        #####
        #####  Now make sure that we know to start up the volume
        #####
        echo "Current status of bb drbd disk device ..." | tee -a /tmp/start-drbd.log
        /etc/ha.d/resource.d/drbddisk bb status | tee -a /tmp/start-drbd.log
        ##
        echo "Forcing status of bb drbd disk device to start state ..."
        /etc/ha.d/resource.d/drbddisk bb start
        ##
        echo "New status of bb drbd disk device ..."
        /etc/ha.d/resource.d/drbddisk bb status
        
        #####
        #####  Finally mount it for use
        #####
        echo "Mounting the /dev/drbd0 device ...."
        mount /dev/drbd0
        
        #####
        #####  Check on drbdsetup status
        #####
        echo "Current status of drbd1 device ..."
        drbdsetup /dev/drbd1 state
        ##
        echo "Forcing state to primary of drbd1 device ..."
        drbdsetup /dev/drbd1 primary --do-what-I-say
        ##
        echo "New status of drbd1 device ..."
        drbdsetup /dev/drbd1 state
        
        #####
        #####  Now make sure that we know to start up the volume
        #####
        echo "Current status of web drbd disk device ..."
        /etc/ha.d/resource.d/drbddisk web status
        ##
        echo "Forcing status of web drbd disk device to start state ..."
        /etc/ha.d/resource.d/drbddisk web start
        ##
        echo "New status of web drbd disk device ..."
        /etc/ha.d/resource.d/drbddisk web status
        
        #####
        #####  Finally mount it for use
        #####
        echo "Mounting the /dev/drbd1 device ...."
        mount /dev/drbd1
        ;;

'stop')
        #####
        #####  Nothing to do
        #####
        echo "Unmounting and disabling drbd on shutdown"

        #####
        #####  Finally mount it for use
        #####
        echo "Unmount volume drbd0 ..."
        umount /dev/drbd0
        
        #####
        #####  Now make sure that we know to start up the volume
        #####
        echo "Current status of bb drbd disk device ..."
        /etc/ha.d/resource.d/drbddisk bb status
        ##
        echo "Forcing status of bb drbd disk device to stop state ..."
        /etc/ha.d/resource.d/drbddisk bb stop
        ##
        echo "New status of bb drbd disk device ..."
        /etc/ha.d/resource.d/drbddisk bb status

        #####
        #####  Check on drbdsetup status
        #####
        echo "Current status of drbd0 device ..."
        drbdsetup /dev/drbd0 state
        ##
        echo "Forcing state to secondary of drbd0 device ..."
        drbdsetup /dev/drbd0 secondary
        ##
        echo "New status of drbd0 device ..."
        drbdsetup /dev/drbd0 state
        
        #####
        #####  Finally mount it for use
        #####
        echo "Unmount volume drbd1 ..."
        umount /dev/drbd1
        
        #####
        #####  Now make sure that we know to start up the volume
        #####
        echo "Current status of web drbd disk device ..."
        /etc/ha.d/resource.d/drbddisk web status
        ##
        echo "Forcing status of web drbd disk device to stop state ..."
        /etc/ha.d/resource.d/drbddisk web stop
        ##
        echo "New status of web drbd disk device ..."
        /etc/ha.d/resource.d/drbddisk web status

        #####
        #####  Check on drbdsetup status
        #####
        echo "Current status of drbd1 device ..."
        drbdsetup /dev/drbd1 state
        ##
        echo "Forcing state to secondary of drbd1 device ..."
        drbdsetup /dev/drbd1 secondary
        ##
        echo "New status of drbd1 device ..."
        drbdsetup /dev/drbd1 state
        
        #####
        #####  Finalize with stop of all drbd
        #####
        echo "Stopping all of drbd ..."
        /etc/init.d/drbd stop
        ;;

*)
        echo "Usage: $0 { start | stop }"
        exit 1
        ;;
esac
exit 0
#
