#!/usr/bin/ksh
#
# filename : loop
# location : a7log
#
# purpose :
#   executes collectfiles on server selected by the command
#   list_processors and the given options
#
# parameter : 
#   -s  for all Sites    or  (as used in /opt/platform7/lbin/list_processors)
#   -w  for all Workstation            or
#   -d  for all DataStores             or
#   -p  for all Probe Processors       or
#   -r  for all DataBroker SDB         or
#   -h hostname  for a dedicated host  or
#   any combination !!                 or
#   -all goes to all Servers including Central Server
#
#---------- function usage ----------
#
# purpose
#   gives information how to use script
#
# Input  Parameter
#     1 : script name
#
  usage () {
    echo ""
    echo "ERROR:  $1  wrong or no parameter"
    echo "\t$1  usage :"
    echo "\t\t$1  [ -s || -w || -d || -h hostname ][-fd characeter]"
    echo "\t\t\t!! any combination to specify hosts is valid !! "
    echo "\t\tExamples "
    echo "\t\t\t$1 -s -h host1 -h host2 "
    echo ""
    echo "\t\t\t$1 -h a7server -fd ""\;"""
    exit
  }
#
#---------- function rdump ----------
# purpose
#   executes p7dumperror on remote hosts and collectfiles
#   executes p7alarmview on central server and collectfiles
#
# Input  Parameter
#     1 : remote hostname
#     2 : field delimiter
#
  rdump () {
# old : only HP-UX
#   s_type_l=$(remsh $1 /sbin/cat /var/opt/platform7/conf/gf36_MCIDStorage)
#
# new : differentiation for Linux DSP
#
    s_OS=""
    s_OS=$(grep $1 /etc/opt/p7install/dsp.conf | cut -f 2 -d \   )
#
    if [ $s_OS = "Linux" ]
    then
      s_type_l=$(ssh root@$1 /bin/cat /var/opt/platform7/conf/gf36_MCIDStorage)
      if [ ! $? -eq 0 ]
      then
        echo "ERROR:  $0  can not reach host $s_host"
        echo "  Did you activate the ssh password agent ?"
        return
      fi
    else
      s_type_l=$(remsh $1 /sbin/cat /var/opt/platform7/conf/gf36_MCIDStorage)
      if [ ! $? -eq 0 ]
      then
        echo "ERROR:  $0  can not reach host $s_host"
        echo "  Check /.rhosts on $s_host"
        return
      fi
    fi
#
    if [ $s_OS = "Linux" ]
    then
      ssh root@$1 mkdir -p $(pwd) 2> /dev/null
    else
      remsh $1 mkdir -p $(pwd) 2> /dev/null
    fi
#
#   remove domain name from hostname, in case it is attached
#
    s_host_parameter=$(echo $1 | awk -F\. ' { print $1;exit } ' )
    s_host_local=$(echo $(hostname) | awk -F\. ' { print $1;exit } ' )
#
    if [ "$s_host_parameter" != "$s_host_local" ]
    then
      if [ $s_OS = "Linux" ]
      then
        scp -p collectfiles $1:$(pwd)
        scp -p error* $1:$(pwd)
      else
        rcp -p collectfiles $1:$(pwd)
        rcp -p error* $1:$(pwd)
      fi
    fi
    if [ $s_OS = "Linux" ]
    then
      ssh root@$1 $(pwd)/collectfiles $2
      scp $1:$(pwd)/"*".tar.gz .
    else
      remsh $1 $(pwd)/collectfiles $2
      rcp $1:$(pwd)/"*".tar.gz .
    fi
#
#   cleanup
#
    if [ ! "$s_type_l" = "Server:0:0:1" ] # Don't delete files on Central Server
    then
      if [ $s_OS = "Linux" ]
      then
        ssh root@$1 rm $(pwd)/"*".tar.gz 2> /dev/null
      else
        remsh $1 rm $(pwd)/"*".tar.gz 2> /dev/null
      fi
    fi
  }
#
#---------- global variables
#
s_option=""
s_host=""
#
#---------- check script is running on Central Server
#
  s_type_l=$(/sbin/cat /var/opt/platform7/conf/gf36_MCIDStorage)
#
  if [ ! "$s_type_l" = "Server:0:0:1" ] 
  then
    echo "$0: ERROR: runs only on Central Server"
    exit
  fi

#
#---------- check valid input parameters
#
if [ ! $# -gt 0 ]
then
  usage $0                                       # display help and exit !!
fi
#
while [ $# -gt 0 ]
do
# echo $1                                             # for testing purpose
  case $1 in
    -all) s_option=$(echo "-s -w -d -p -r")
          s_host_list=$(hostname) ;;
    -s) s_option=$(echo $s_option $1) ;;
    -w) s_option=$(echo $s_option $1) ;;
    -d) s_option=$(echo $s_option $1) ;;
    -p) s_option=$(echo $s_option $1) ;;
    -r) s_option=$(echo $s_option $1) ;;
    -h) s_host_list=$(echo $s_host_list $2)
        shift ;; # remove hostname from parameter list
    -fd) s_fd=$2
         shift ;; # remove field delimiter from parameter list
     *) usage $0 ;;                              # display help and exit !!
  esac 
  shift
done
#
# echo "option: $s_option"                             # for testing purpose
# echo "hosts : $s_host_list"                          # for testing purpose
#
#---------- loop based on list_processors
#
if [ ! -z "$s_option" ]
then
  for s_host in $(/opt/platform7/lbin/list_processors $s_option)
  do
     rdump $s_host
  done
fi
#
# loop based on given hostnames
#
if [ ! -z "$s_host_list" ]
then
  for s_host in $(echo $s_host_list)
  do
    rdump $s_host $s_fd
  done
fi
#
