#!/bin/sh

# This is the location of all of the install files
INSTALLDIR=PUT_INSTALL_PATH_HERE

#This script is for Macs.  Don't F up other systems
os=`uname -s`
if [ "$os" != "Darwin" ]
then
  echo "This is not a Mac!  Bye."
  exit
fi

#This script is for Leopard.  Don't F up other systems
osver=`uname -r | sed 's/\..*$//'`
if [ "$osver" != 9 ]
then
  echo "This is not Leopard!  Bye."
  exit
fi

#get short hostname, to use to look for host-specific files
hostname=`hostname | sed 's/\..*$//'`

doitstring=' ---  fixing...'
doit=1

# -n option means check only, don't fix anything
if [ "$1" = "-n" ]
then
  doitstring=''
  doit=0
fi

cd $INSTALLDIR
cwd=`pwd`
if [ "$cwd" != "$INSTALLDIR" ]
then
  echo "Can't cd to $INSTALLDIR.  Bye."
  exit
fi

checkfixlink() {
  if [ -L $2 ]
  then
    to=`readlink $2`
    if [ "$1" = "$to" -o "$1" = "$to" ]
    then
      #echo link from $2 to $1 is good
      :
    else
      echo link $2 is bad, points to $to$doitstring
      if [ "$doit" = 1 ]
      then
        rm $2
        ln -s $1 $2
      fi
    fi

  elif [ -d $2 ]
  then
    echo link $2 is still a directory$doitstring
    if [ "$doit" = 1 ]
    then
      rmdir $2
      ln -s $1 $2
    fi
  elif [ -f $2 ]
  then
    echo link $2 is still a file$doitstring
    if [ "$doit" = 1 ]
    then
      rm $2
      ln -s $1 $2
    fi
  else    #just doesn't exist
    echo link $2 is missing$doitstring
    if [ "$doit" = 1 ]
    then
      ln -s $1 $2
    fi
  fi
  chmod -h 755 $2
}

checkfixinstall() {
  ifile="$4"
  if [ -e "$hostname/$ifile" ]
  then
    ifile="$hostname/$ifile"
  fi
  if [ -d "$5" ]
  then
    ofile="$5/$4"
  else
    ofile="$5"
  fi
  cmp -s "$ifile" "$ofile"
  if [ $? = 0 ]
  then
    #echo "$ofile" is good
    mode=`perl -e "printf('%o',(stat('$ofile'))[2] & 07777);"`
    owner=`ls -l "$ofile" | awk '{ print $2 }'`
    group=`ls -l "$ofile" | awk '{ print $3 }'`
    if [ $mode != $1 ]
    then
      echo mode mismatch "$ofile" is $mode should be $1$doitstring
      if [ "$doit" = 1 ]
      then
	chmod $1 "$ofile"
      fi
    fi
    #No fixes for owner or group implemented yet
  else
    echo "$ofile" doesn\'t match$doitstring
    if [ "$doit" = 1 ]
    then
      install -B .orig -b -m $1 -o $2 -g $3 "$ifile" "$5"
    fi
  fi
}

checkfixrm() {
  if [ -e $1 ]
  then
    echo $1 hasn\'t been deleted$doitstring
    if [ "$doit" = 1 ]
    then
      rm -f $1
    fi
  fi
}

#
# SETUP SECTION
# Below is where you add commands for your setup.  There are threee
# commands: checkfixlink, checkfixinstall, and checkfixrm
# Samples are below.
#

# Set up symlinks
# checkfixlink will nuke any file or directory that already exists
#checkfixlink POINT_TO_PATH NEW_LINK_PATH
#for example, remove the var mail directory, and make it a link elsewhere:
#checkfixlink /stage/mail /var/mail

# Install Files
# Grabs them from the local directory, also looks in a subdirectory
# named with the short hostname of the current host, for custom files
# specific to that host.
#
#checkfixinstall OCTAL_PERMS OWNER GROUP FILE_HERE  WHERE_TO_INSTALL_IT
#for example, install an fstab file
#checkfixinstall 644 root wheel fstab /etc

# Remove Files
# Doesn't do directories, just files
# checkfixrm FILE_TO_REMOVE