#!/bin/sh

# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (the "License").  You may not use this file except in compliance
# with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END

# Possible defaults are TEST, ALL, AIX, SUNOS, NONE
#
DEFAULT_MODE="NONE"

SSH_USER="root"

if [ "${SSH_USER}"x = "root"x  ] ; then
# only necessary for root users (def: ADMIN: no changes to the system)
  LOGGER_STARTMSG=""
  LOGGER_ENDMSG=""
fi

LOGFILE="/tmp/$( basename $0 ).log"

logmsg() {
  echo $*
  echo $* >>${LOGFILE}
}

if [ "$1"x = ""x ] ; then
  echo "Usage: $0 input_file [SUNOS|AIX|ALL|TEST|NONE]"
  echo "Default mode is ${DEFAULT_MODE}  "
  exit 1
fi

MODE=$2
[ "${MODE}"x = ""x ] && MODE=${DEFAULT_MODE}

INPUTFILE=$1

TMPFILE="/tmp/$( basename $0 ).$$"
TARGET_TMPFILE="/tmp/tmp_$$_$( basename $0 )"

INPUT_OK=1
while [ ${INPUT_OK} != 0 ] ; do
  INPUT_OK=0
  logmsg ""
  logmsg "$0 - executing scripts on a list of machines using scp and ssh" 
  logmsg "The input file with the list of machine names is \"${INPUTFILE}\""
  logmsg "The temporary file used on this machine is \"${TMPFILE}\"."
  logmsg "The temporary file used on the machines to process is \"${TARGET_TMPFILE}\"."
  logmsg "The mode is \"${MODE}\"." 
  logmsg "The ssh/scp user used is \"${SSH_USER}\"."
  if [ "${SSH_USER}"x = "root"x  ] ; then
    logmsg "Logger Start message is \"${LOGGER_STARTMSG}\"." 
    logmsg "Logger End message is \"${LOGGER_ENDMSG}\"." 
  fi
  logmsg "The logfile is \"${LOGFILE}\"."  
  logmsg "Press enter to continue or ctrl-c to abort, or \"list\" to view the file  \"${INPUTFILE}\""
  read USERINPUT
  if [ "${USERINPUT}"x = "list"x ] ; then
    logmsg ""
    logmsg "The contents of the file \"${INPUTFILE}\" are"
    logmsg " ----------------- "
    more ${INPUTFILE} | tee -a ${LOGFILE}
    logmsg " ----------------- "
    logmsg "Press enter to continue"
    read USERINPUT
    INPUT_OK=1
  fi
done
logmsg ""

if [ ! -f ${INPUTFILE} ] ; then
  logmsg "ERROR: The input file \"${INPUTFILE}\" does not exist!"
  exit 2
fi

ps -u ${USER} | grep ssh-agent 2>&1 1>/dev/null
if [ $? -ne 0 ] ; then
  logmsg "WARNING: ssh-agent is not running for the user id ${USER}!"
fi


logmsg "Creating the temporary script file \"${TMPFILE}\" ..."

# this is the script that is executed on every machine
#    
cat >${TMPFILE} <<EOF
#!/usr/bin/ksh
echo "Now executing the temporary file \\\"\$0\\\" ..."

MODE="${MODE}"

if [ "${SSH_USER}"x = "root"x ] ; then
  logger -p user.info "ADMIN: no changes to the system /$USER"
  logger -p user.info "ADMIN: no changes to the system /$USER"
  logger -p user.info "ADMIN: no changes to the system /$USER"
fi

MACHINENAME=\`uname -n\`
OSVERSION=\`uname -s\`

echo "Processing machine \$MACHINENAME ..."

if [ "\$OSVERSION"x = "AIX"x ] ; then

  echo "This is an AIX machine."

  if [ "\${MODE}" = "AIX" -o "\${MODE}"  = "ALL" ] ; then

    if [ "${LOGGER_STARTMSG}"x != ""x -a "${SSH_USER}"x = "root"x ] ; then
      logger -p user.info ${LOGGER_STARTMSG}
      logger -p user.info ${LOGGER_STARTMSG}
      logger -p user.info ${LOGGER_STARTMSG}
    fi

# ---------------------------------------------------------------------- 
# commands for AIX machines 

    HOSTLINE=\$( grep e004.esb.eur.deuba.com /etc/hosts )
    if [ ! "\$HOSTLINE"x = ""x ] ; then
      set -- \$( echo \$HOSTLINE )
      CURIP=\$1
      CURHOSTNAME=\$2
      if [ "\$1"x = "10.75.224.29"x ] ; then
        echo "/etc/hosts entry for e004 is okay."
      else
        echo "Invalid entry in /etc/hosts found: \$HOSTLINE"
        if [ -f /etc/netsvc.conf ] ; then
          echo "Search order is:"
          cat  /etc/netsvc.conf
        else
          echo "Warning: No /etc/netsvc.conf found!"
        fi

      fi
    else
      echo "Host e004 NOT found in /etc/hosts."
    fi

# ---------------------------------------------------------------------- 

    if [ "${LOGGER_STARTMSG}"x != ""x -a "${SSH_USER}"x = "root"x ] ; then
      logger -p user.info ${LOGGER_ENDMSG}
      logger -p user.info ${LOGGER_ENDMSG}
      logger -p user.info ${LOGGER_ENDMSG}
    fi

  else
    echo "Mode is \${MODE} - skipping this machine."
  fi
elif [ "\$OSVERSION"x = "SunOS"x ] ; then
  echo "This is a Solaris machine."
  if [ "\${MODE}" = "SUNOS" -o "\${MODE}"  = "ALL" ] ; then

    if [ "${LOGGER_STARTMSG}"x != ""x -a "${SSH_USER}"x = "root"x ] ; then
      logger -p user.info ${LOGGER_STARTMSG}
      logger -p user.info ${LOGGER_STARTMSG}
      logger -p user.info ${LOGGER_STARTMSG}
    fi

# ---------------------------------------------------------------------- 
# commands for SUN machines 

    HOSTLINE=\$( grep e004.esb.eur.deuba.com /etc/hosts )
    if [ ! "\$HOSTLINE"x = ""x ] ; then
      set -- \$( echo \$HOSTLINE )
      CURIP=\$1
      CURHOSTNAME=\$2
      if [ "\$1"x = "10.75.224.29"x ] ; then
        echo "/etc/hosts entry for e004 is okay."
      else
        echo "Invalid entry in /etc/hosts found: \\\"\$HOSTLINE\\\""
        if [ -f /etc/nsswitch.conf ] ; then
          echo "Search order is:"
          grep hosts /etc/nsswitch.conf | grep -v "^#"
        else
          echo "Warning: No /etc/nsswitch.conf found!"
        fi
      fi
    else
      echo "Host e004 NOT found in /etc/hosts."
    fi

# ---------------------------------------------------------------------- 

    if [ "${LOGGER_STARTMSG}"x != ""x -a "${SSH_USER}"x = "root"x ] ; then
      logger -p user.info ${LOGGER_ENDMSG}
      logger -p user.info ${LOGGER_ENDMSG}
      logger -p user.info ${LOGGER_ENDMSG}
    fi

  else
    echo "Mode is \${MODE} - skipping this machine."
  fi

else
  echo "ERROR: Invalid operating system found: \$OSVERSION"
fi

# if [ "\${MODE}" != "TEST" ] ; then
#  echo "Deleting the temporary file \\\"\${TARGET_TMPFILE}\\\" ..."
#  rm ${TARGET_TMPFILE}
# fi

EOF

logmsg "Processing all machines ..."
  
for CURMACHINE in $( cat ${INPUTFILE} ) ; do
  logmsg "---- Processing ${CURMACHINE} ..."
  scp ${TMPFILE} ${SSH_USER}@${CURMACHINE}:${TARGET_TMPFILE} | tee -a ${LOGFILE}
#  ssh -l ${SSH_USER} ${CURMACHINE} "/usr/bin/ksh ${TARGET_TMPFILE}" | tee -a ${LOGFILE}
done

if [ "${MODE}" != "TEST" ] ; then
  logmsg "Deleting the temporary script file \"${TMPFILE}\" ..."
  rm ${TMPFILE}
fi

logmsg "All done."
