#!/bin/bash
#
# Copyright (c) 2009 Michael Schwartzkopff
# Mail: misch@multinet.de
#
# Verison: 0.0.2: Technical demonstration
#
# 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 3 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.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#######################################################################
#
# TODO:
#	- More resources!
# 	- Parameter validation
#	- Detect dependend resources and configure them also:
#	  Something like: reswiz Filesystem adds resFS and resDRBD
#
######################################################################

SCRIPTNAME=reswiz

usage()
{
  echo "This script will configure your running (!) resource"
  echo "automagically as a resource of your cluster."
  echo ""
  echo "Usage: $SCRIPTNAME {apache}" >&2
  echo ""
  echo "Sorry. Up to now only a OCF apache and LSB resources as technical"
  echo "demostration are configured."
  return
}

configure_apache()
{
  ANZ=`ps ax -o args  | grep 'apache\|httpd' | grep -v $SCRIPTNAME | sort | uniq| wc -l`
  
  if (( $ANZ > 1 ))
    then echo "Sorry. Cold not figure out what proccess to choose."
    exit 1
  fi

  APACHECL=`ps ax -o args  | grep 'apache\|httpd' | grep -v $SCRIPTNAME | sort | uniq`

  if [ ${#APACHECL} == 0 ]
    then echo "Sorry. No running apache or httpd found."
    exit 1
  fi

  APACHEBIN=${APACHECL%% *}
  APACHEOPTS=${APACHECL#* }

  APACHECONF=`/usr/sbin/apache2 -V | grep SERVER_CONFIG_FILE | cut -d'"' -f2`

  echo "Found binary file: "$APACHEBIN
  echo "Found options: "$APACHEOPTS
  echo "Found config file: "$APACHECONF

  return 0

  crm<<EOF
    configure
      primitive resApache ocf:heartbeat:apache \
      params configfile=$APACHECONF httpd=$APACHEBIN options="$APACHEOPTS" \
      meta target-role=stopped \
      op monitor interval=30s
    commit
    exit
EOF

  return 0

}

configure_lsb ()
{
crm<<EOF
  configure
  primitive res$RESOURCE lsb::$RESOURCE \
  meta target-role=stopped \
  op monitor interval=30s
  commit
  exit
EOF

  return 0
}

if [ "x"$1 = "x" ]
  then usage
    exit 1
fi

RESOURCE=$1

# Check if its a ocf resource
lrmadmin -T ocf | grep -q $RESOURCE -
if (( ! $? )) 
  then
  case "$RESOURCE" in
    apache)
      echo "Found apache webserver resource"
      configure_apache
      exit 0
      ;;
    *)
      usage
      exit 3
      ;;
  esac
fi

lrmadmin -T lsb | grep $RESOURCE -
if (( ! $? ))
  then
    configure_lsb
    echo ""
    echo "Are your sure that your lsb script meets the conditions for"
    echo "the use in a cluster? See http://www.linux-ha.org/LSBResourceAgent"
    exit 0
fi

usage

echo "Sorry no resource agent with name "$RESOURCE" found."
exit 1
