#!/bin/bash

### Gaim Personal Tracking Device
### by Michael Lee
### v2.02, 2003-05-09

###   Introduction
#
#  Adds custom text to the gaim chat client user information.
#

###   Installation
#
#  1. Place this script somewhere in your executable PATH.
#  2. Make sure that ifconfig, tr, lockfile, colnum, etc. are also in your path.
#  3. Make sure all of the above are executable by the desired users (this means you).
#  4. Modify the subnet information, lab information, etc. to match your network.
#  5. Execute this file upon login. To do so, place it in your ~/.bash_profile or similar file.
#

###   Caveats
#
#  This will only modify the first user encountered in the .gaimrc file.
#
#  Make sure your user info is always one line (both betfore the first
#  script execution, and after every execution).  Use <BR> tags instead 
#  of line breaks to get a new line in your user info.
#

###   Usage
#
#  This script should be automatically run upon login.
#  It takes no commandline arguments.
#

###   Changelog
#
#  2003-05-15           v2.02
#                       + Changed order of checks so that lock is checked-for first.
#                         This should clean up output because tempfile error should 
#                         be displayed less frequently.
#
#  2003-05-09           v2.01
#                       ! Multiple shells could start at once and trample over
#                         one another.  Begun the use of lockfiles to prevent this.
#
#  2003-05-06           v2.00, GPL'd
#                       + Now uses only one temp file.  User doesn't have to 
#                         deal with temp files manually.  See caveats above.
#
#  2003-04-11           v1.00
#

###   License
#
#  Copyright (C) 2003  Michael Lee <kirbysdl@yahoo.com>
#
#  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 2
#  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, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#


# Initialize!

# If lockfile exists, be paranoid and stop
if ! lockfile -r0 -l 60 -s 3 ~/.gaim-init.lock > /dev/null 2>&1; then
  echo "can't acquire lockfile!"
  exit 1
fi

# If tempfile exists, be paranoid and stop
if [[ -f ~/.gaimrc_tempfile ]]; then
  echo "tempfile in the way!"
  exit 1
fi

# Find the ip, separate the dotted ip fields, extract the third field
subnet=`ifconfig | grep 128.111 | tr "." " " | colnum 4`

# Find the ip, separate the dotted ip fields, extract the fourth field
host=`ifconfig | grep 128.111 | tr "." " " | colnum 5`

# Find the first user info's line number
infoline=`grep -m 1 --line-number "user_info" ~/.gaimrc | sed 's/^\([0-9]*\).*/\1/'`

head -n ${infoline} ~/.gaimrc > ~/.gaimrc_tempfile

# Print hostname
echo -en '\t\t\t'"If you want to find me, I'm probably in front of or near the computer ''" >> ~/.gaimrc_tempfile
echo -n `uname -n` >> ~/.gaimrc_tempfile
echo -n "''<BR><BR>" >> ~/.gaimrc_tempfile

# Print lab.  expect this to very often be very wrong
echo -n "My script thinks this hostname maps to a computer in the ''" >> ~/.gaimrc_tempfile
if (( $subnet == 43 )); then
  if (( $host > 220 )); then
    echo -n "Win XP" >> ~/.gaimrc_tempfile ;
  elif (( $host > 200 )); then
    echo -n "GSL" >> ~/.gaimrc_tempfile ;
  elif (( $host > 0   )); then
    echo -n "CSIL" >> ~/.gaimrc_tempfile ;
  else
    echo -n "[[unknown]]" >> ~/.gaimrc_tempfile ;
  fi ;
elif (( $subnet == 54)); then
  if (( $host < 5 )); then
    echo -n "TA" >> ~/.gaimrc_tempfile ;
  else
    echo -n "[[unknown]]" >> ~/.gaimrc_tempfile ;
  fi ;
else
  echo -n "[[unknown]]" >> ~/.gaimrc_tempfile ;
fi

echo "'' lab.<BR><BR>For your own privacy-be-damned tracker, try <a href=\"http://ucsb.curby.net/broadcast/gaim-init\">this script</a>!" >> ~/.gaimrc_tempfile

# Write the rest of the original .gaimrc 
infoline=$(($infoline+2))
sed -n ${infoline}~1p ~/.gaimrc >> ~/.gaimrc_tempfile

# Write to the real .gaimrc
mv ~/.gaimrc_tempfile ~/.gaimrc

# Delete lock file
rm -f ~/.gaim-init.lock
