#!/bin/sh

##Make it so config files are not overwritten
config() {
  NEW="$1"
  OLD="`dirname $NEW`/`basename $NEW .new`"
  # If there's no config file by that name, mv it over:
  if [ ! -r $OLD ]; then
    mv $NEW $OLD
  elif [ "`cat $OLD | md5sum`" = "`cat $NEW | md5sum`" ]; then # toss the redundant copy
    rm $NEW
  fi
  # Otherwise, we leave the .new copy for the admin to consider...
}

## List of conf files to check.
config etc/squid/cachemgr.conf.new
config etc/squid/mime.conf.new
config etc/squid/squid.conf.new
config etc/rc.d/rc.squid.new

check_squid_env() {
  def_uid=31
  def_gid=31
  SQUID_USER=squid
  # Confirm that the 'squid' user and group exist.
  grep -q "^$SQUID_USER:" /etc/group || groupadd -g $def_gid $SQUID_USER || exit 1
  grep -q "^$SQUID_USER:" /etc/passwd || useradd -c "Squid Cache" -d /bin/false -g $SQUID_USER -u $def_uid $SQUID_USER || exit 1
}

mkdir -p var/{cache,log}/squid

check_squid_env
chown -R squid.squid var/cache/squid
chown -R squid.squid var/log/squid
