#!/bin/bash

# This script will attempt to upgrade your version of check_wmi_plus.pl

EXIT_STATUS=0 # start with exiting on a good note!
DATIM=`date +%Y%m%d%H%M%S`
SCRIPT_NAME=`basename $0`

# make sure we r all headed in the same direction.
TO_VERSION="1.60"
# yea, cuase shell can only do interger math.
INTR2VER=`echo ${TO_VERSION} | tr -d \.`
WMI_PKG=check_wmi_plus.v${TO_VERSION}.tar.gz
#RELEASE_FILE=/etc/centos-release # Wouldn't work on centos 5.. arg.
RELEASE_FILE=/etc/redhat-release
#curl -L https://cpanmin.us | perl - App::cpanminus
CPANM_URL=http://cpanmin.us

# 
#CHECK_WMI_URL="http://edcint.co.nz/checkwmiplus/sites/default/files/check_wmi_plus.v${TO_VERSION}.tar.gz"

# check if we are going to be able to do anything.
if test ${UID} -ne 0; then
  echo "We are not root, we wouldn't be able to do what we need to"
  echo "Exiting."
  exit 136
fi

# get location of nagios, from the init script
NAGIOSAT=`grep ^prefix /etc/init.d/nagios 2>/dev/null | cut -d= -f2 `

if test -z "${NAGIOSAT}" ; then
  NAGIOSAT="/usr/local/nagios"
fi

if test -d "${NAGIOSAT}"; then
  if test -d "${NAGIOSAT}/libexec"; then
    NAGIOS_LIBEXEC="${NAGIOSAT}/libexec"
  else
    echo "Found Nagios (in \"${NAGIOSAT}\", but not the libexec dir ?"
    exit 135
  fi
else # ? couldn't find nagios
  echo "Couldn't find nagios ?"
  exit 134
fi 

if test ! -x "${NAGIOS_LIBEXEC}/check_wmi_plus.pl"; then
 echo "I couldn't find check_wmi_plus.pl in the Nagios LIBEXEC (\"${NAGIOS_LIBEXEC}\") directory."
 exit 133
fi

YOUR_VER=`${NAGIOS_LIBEXEC}/check_wmi_plus.pl --version| cut -d\  -f2`
YOUR_INTRVER=`echo ${YOUR_VER} | tr -d \.`

if test "${YOUR_INTRVER}" -ge "${INTR2VER}"; then
  echo "You already have a current version (${YOUR_VER}) of the check_wmi_plus.pl"
  echo "Nothing to do, exiting."
  exit 140
fi

echo "This script will attempt to upgrade your version check_wmi_plus.pl (${YOUR_VER})"
echo "to version ${TO_VERSION}"

# Have yum do an update ?
# sudo yum update 
echo "Please be sure your sytem has all the current patches."

# Look for cpanm
CPANM=`which cpanm 2>/dev/null`
CPANM_STATUS=$?

# if we didn't find it in the normal locations
if test ${CPANM_STATUS} -ne 0 ; then

  # Now what ? 
  # quick check if it is in /usr/local/sbin
  if test -f "/usr/local/bin/cpanm" ; then
    echo "cpanm is in /usr/local/bin/cpanm, but /usr/local/bin is not in your PATH ?"
    echo "please update your path, or relocate cpanm to a directory in your PATH (${PATH})"
    exit 137
  else
    echo "We were unable to find cpanm."
    echo "We are going to download it from '${CPANM_URL}'"
    echo "install and run it."
    read -p "Can we proceed ? [Y/N]" ANS

    case $ANS in
      [Yy]|[Yy][Ee][Ss] )
        # get cpanm
        # https://cpanmin.us wasn't working... arg.
        echo "Installing cpanm from '${CPANM_URL}'"
        #wget ${CPANM_URL} -O cpanmin.pl
        #perl cpanmin.pl App::cpanminus
        curl -L http://cpanmin.us | perl - --sudo App::cpanminus
        # Okay, cpanm should be installed... but where ?
        # fudge... okay we are going to check where it should be to 
        # make sure we don't get a rouge one. ? 
        if test -f "/usr/local/bin/cpanm"; then
          CPANM=/usr/local/bin/cpanm
        else
          if test -f "/usr/bin/cpanm"; then
            CPANM=/usr/bin/cpanm
          else
            echo "Hu ? can't find the cpanm we just installed."
            exit 142
          fi
        fi
      ;;
      *)
        # they answered with something other than yes
        echo "Please install cpanm and be sure it is in the path for root."
        exit 141
      ;;
    esac
  fi
fi

# Make a TEMPDIR to work in (with our pid, script name 
TEMPDIR="/tmp/$$_${SCRIPT_NAME}_${DATIM}"

if test ! -d "${TEMPDIR}"; then
  mkdir ${TEMPDIR}
else
  echo "Hu ? temp dir (\"${TEMPDIR}\") exists ? "
  echo "To attempt to run again please remove that directory."
fi

cd ${TEMPDIR}

#get the wmi package
echo "Get version ${TO_VERSION} of check_wmi_plus.pl"
wget http://edcint.co.nz/checkwmiplus/sites/default/files/${WMI_PKG}

if test ! -f "${WMI_PKG}"; then
  echo "Didn't get the file we were looking from from"
  echo "  http://edcint.co.nz/checkwmiplus/sites/default/files/${WMI_PKG} ? "
  exit 138
fi

# Okay file here, going to try and untar
tar -zxvf ${WMI_PKG}
#Backup the old check_wmi_plus.pl file
# not sure if we want to do this.
cp ${NAGIOS_LIBEXEC}/check_wmi_plus.pl check_wmi_plus.pl-orig-${DATIM}

# update vars $conf_file and "use lib"
#sed "s+^my $conf_file='/opt/nagios/bin/plugins/check_wmi_plus.conf'+my $conf_file='/usr/local/nagios/libexec/check_wmi_plus.conf'+" check_wmi_plus.pl  > newstage1_check_wmi_plus.pl
sed "s+conf_file='/opt/nagios/bin/plugins/check_wmi_plus.conf';$+conf_file='/usr/local/nagios/libexec/check_wmi_plus.conf';+" check_wmi_plus.pl > stage1-check_wmi_plus.pl
sed 's+^use lib "/usr/lib/nagios/plugins"+use lib "/usr/local/nagios/libexec"+' stage1-check_wmi_plus.pl > check_wmi_plus.pl
rm stage1-check_wmi_plus.pl

# going to assume LongOpts is/was installed...

# now need to determin what os we are on
if test -f "${RELEASE_FILE}" ; then
  DIST="el" # we consider both RedHat and CentOS 'el'
  # okay we are on the right track
  version=`sed 's/.*release \([0-9.]\+\).*/\1/' ${RELEASE_FILE}`
  MJVR=`echo ${version} | cut -d. -f1`
fi

case ${DIST}${MJVR} in
  el5 )
    # Need Number/Format.pm before it will even tell us that it wouldn't work... arg.
    sudo yum -y install perl-Number-Format.noarch

    # we should have cpanm update packages.
    # okay, could do something mucho cool and comb through the output to figure out what isn't working
    # but not gonna do that now.
    echo "Installing Perl module Data::Dumper"
    ${CPANM} Data::Dumper
    echo "Installing Perl module Scalar::Util"
    ${CPANM} Scalar::Util
    echo "Installing Perl module Storable"
    ${CPANM} Storable
    echo "Installing Perl module DateTime"
    ${CPANM} DateTime

    # check to see if this is already set ? 
    echo "Turning on '\$ignore_my_outdated_perl_module_versions' in config file"
    echo "\$ignore_my_outdated_perl_module_versions=1;" >> /usr/local/nagios/libexec/check_wmi_plus.conf 
    
    echo "CentOS 5 specific setup complete."
  ;;
  el6 )
    echo "Installing Perl Number::Format module."
    sudo yum -y install perl-Number-Format.noarch

    # we should have cpanm update packages.
    # okay, could do something mucho cool and comb through the output to figure out what isn't working
    # but not gonna do that now.
    echo "Installing Perl module Data::Dumper"
    ${CPANM} Data::Dumper
    echo "Installing Perl module Scalar::Util"
    ${CPANM} Scalar::Util
    echo "Installing Perl module Storable"
    ${CPANM} Storable
    echo "Installing Perl module DateTime"
    ${CPANM} DateTime

    echo "CentOS 6 specific setup complete."
  ;;
  el7 )
    if test ! -f "/lib64/libsasl2.so.2"; then
      echo "No 'libsasl2.so.2', we will link libsasl3.so to libsasl2.so.2"
      cd /lib64
      sudo ln -s /lib64/libsasl2.so.3.0.0 libsasl2.so.2
      cd -
    fi

    echo "Installing Perl module Storable"
    ${CPANM} Number::Format

    echo "Installing Perl module Storable"
    ${CPANM} Storable


    echo "CentOS 7 specific setup complete."
  ;;
  *) # default for os check..
    echo "Error I don't know how to install on your os."
    exit 139
  ;;
esac

echo "Putting new check_wmi_plus.pl in '${NAGIOS_LIBEXEC}'"
# Okay ready to put new execuable int
cp check_wmi_plus.pl ${NAGIOS_LIBEXEC}/check_wmi_plus.pl

# Update done.
# Check that check_wmi_plus.pl is happpy ? 
HAPPY_CHEK=`${NAGIOS_LIBEXEC}/check_wmi_plus.pl --version`
HAPPY_STATUS=$?

if test "${HAPPY_STATUS}" -ne 0 ; 
then
  echo "Unknown unhappyness from ${NAGIOS_LIBEXEC}/check_wmi_plus.pl ? "
  echo "the version check returned"
  echo "${HAPPY_CHEK}"
  EXIT_STATUS=137
else
  echo "check_wmi_plus.pl appears to be working."
fi

echo ""
echo "Please check your check_wmi_plus.pl is working correctly and"
echo "your check are working correctly in Nagios"
echo "Then you can delete '${TEMPDIR}' temp directory."
echo ""

echo "Run Complete."
exit ${EXIT_STATUS}
