#!/bin/sh
#
# cca: A script to run cca-login.pl from a cron job for the required weekly login
# The script analyzes the result from a wget from www.uci.edu to detect the
# CCA redirect
#
# Copyright (c) 2005, 2006 Joachim Feise (jfeise at feise dot com).
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# 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, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# You can also view the GNU General Public License on the
# World Wide Web at http://www.gnu.org/copyleft/gpl.html

# Revision history
# 1.5 - 2006-Sep-27
# Changed the license to GPLv2 only.

# 1.4 - 2006-Jul-2
# www.uci.edu IP address changed.

# 1.3 - 2006-Jan-30
# Apparently, ping, nmap's requests, and ssh are now let through by CCA.
# Resnet may have analyzed my script and taken steps to make it ineffective.
# The new procedure to detect the CCA login gets the webpage and analyzes
# it to see if it contains the redirect to the CCA login page.

# 1.2 - 2006-Jan-23
# Changed from using www.uci.edu to www.feise.com because pinging machines within
# UCI is now possible without going through CCA
#
# 1.1 - 2005-Dec-09
# Optionally allow use of TCP/IP Ack through nmap instead of ICMP Echo through ping
# Added optional logging
# Added running traceroute for logging in case of outage
#
# 1.0    - 2005-Sep-12
# Initial release.

LOGGING=$1
if [ $LOGGING. == . ]; then
  LOGGING="0"
fi

case "$LOGGING" in
  '-h')
    echo "Usage: $0 [logging]"
    echo "    logging: 1 to log to .resnet in the home directory."
    echo "Note: needs wget installed."
    echo "Get wget from http://www.gnu.org/software/wget/wget.html"
    exit 1
    ;;
esac

wget -q -O - 'http://128.195.135.139/' | grep 'perfigo_weblogin.jsp' > /dev/null
HOST_UP=$(($?))

if [ $HOST_UP == "0" ]; then
  echo "CCA login..."
  $HOME/cron/cca-login.pl -a -i ip-address -u username -p password
  if [ $LOGGING == "1" ]; then
    DATE=`date +'%F %H:%M'`
    echo $DATE >> $HOME/.resnet
    traceroute -m 10 -n 128.195.135.139 >> $HOME/.resnet
  fi
fi
