#!/bin/sh
#
# Copyright (C) Freemont Avenue Software, Inc. 2000. All rights reserved.
#
# Script to setup the apache cache linked over
# multiple hard drives.
#
# syntax: link_cache [-h] [-l cache_dir1 cache_dir2 ...]
#      -h      print out this syntax message.
#      -l      link apache cache directories where
#              cache_dir1, cache_dir2, etc. are
#              file systems on seperate hard drives to be
#              used as additional cache space.
#              Each file system must be previously defined
#              and mounted by the operating system.
#
# example: 
#
# ./link_cache -l /home/hermes/cache /d2/cache /d3/cache
#
# 	This links /d2/cache and /d3/cache to /home/hermes/cache
#	where /home/hermes/cache is the default CacheRoot
#	in /etc/apache/httpd.  This will make /home/hermes/cache
#	linked to the cache directories on the secondary and
#	tertiary hard drives /d2 and /d3.  That is,
#	/home/hermes/cache/0 ---> /d2/cache/0
#	/home/hermes/cache/1 ---> /d3/cache/1
#	/home/hermes/cache/2 ---> /d2/cache/2
#	/home/hermes/cache/3 ---> /d3/cache/3
#	...etc...
#
# NOTE: httpd should be shutdown before running this script.
#

usage()
{
  echo ""
  echo "Script to setup the apache cache linked over"
  echo "multiple hard drives."
  echo ""
  echo "syntax: link_cache [-h] [-l cache_dir1 cache_dir2 ...]"
  echo "      -h      print out this syntax message."
  echo "      -l      link apache cache directories where"
  echo "              cache_dir1, cache_dir2, etc. are"
  echo "              file systems on seperate hard drives to be"
  echo "	      used as additional cache space."
  echo "              Each file system must be previously defined"
  echo "              and mounted by the operating system."
  echo ""
  echo "example: "
  echo ""
  echo "./link_cache -l /home/hermes/cache /d2/cache /d3/cache"
  echo ""
  echo "	This links /d2/cache and /d3/cache to /home/hermes/cache"
  echo "	where /home/hermes/cache is the default CacheRoot"
  echo "	in /etc/apache/httpd.  This will make /home/hermes/cache"
  echo "	linked to the cache directories on the secondary and"
  echo "	tertiary hard drives /d2 and /d3.  That is,"
  echo "	/home/hermes/cache/0 ---> /d2/cache/0"
  echo "	/home/hermes/cache/1 ---> /d3/cache/1"
  echo "	/home/hermes/cache/2 ---> /d2/cache/2"
  echo "	/home/hermes/cache/3 ---> /d3/cache/3"
  echo "	...etc..."
  echo ""
  echo "NOTE: httpd should be shutdown before running this script."
  echo ""
}

# The main cache drive for the apache cache, first
# one entered as command line argument.  It
# will hold some real cache directories and the
# rest will be links to the other caches. 
MAINCACHE=""

# The cache drives that will be linked to 
# the main cache.
OTHERCACHES=""

# Process the command line.
count=0
max=`expr $# - 2`
DIR_NUM=`expr $# - 1`
if [ $# -eq 0 ]; then
  echo ""
  echo "Must specify at least one argument"
  usage
  exit 1
fi
if [ $# -gt 1 ]; then
  if [ "$1" = "-l" ]; then
    if [ $# -lt 3 ]; then
      echo ""
      echo "Must specify at least two cache_dirs"
      usage
      exit 1
    fi
  else
    echo ""
    echo "Invalid argument"
    usage
    exit 1
  fi
fi
while [ $# -gt 0 ]; do
  case "$1" in
    -l)
      MAINCACHE=$2
      shift
      shift
      while [ $count -lt $max ]; do
        OTHERCACHES="$OTHERCACHES $1"
        count=`expr $count + 1`
        shift
      done
      ;;
    -h)
      usage
      exit 1
      ;;
    *)
      echo ""
      echo "Invalid argument"
      usage
      exit 1
      ;;
  esac
done

# directories in apache cache to make links
# out of directories are 0-9, -, @, a-z, A-Z. 
CACHE_DIRS="0 \
            1 \
            2 \
            3 \
            4 \
            5 \
            6 \
            7 \
            8 \
            9 \
            a \
            b \
            c \
            d \
            e \
            f \
            g \
            h \
            i \
            j \
            k \
            l \
            m \
            n \
            o \
            p \
            q \
            r \
            s \
            t \
            u \
            v \
            w \
            x \
            y \
            z \
            A \
            B \
            C \
            D \
            E \
            F \
            G \
            H \
            I \
            J \
            K \
            L \
            M \
            N \
            O \
            P \
            Q \
            R \
            S \
            T \
            U \
            V \
            W \
            X \
            Y \
            Z \
            @ \
            _"

# Get the apache userid and groupid.
echo ""
CONF=/etc/apache/httpd.conf
if [ -f $CONF ]; then
  USERID=`grep "^User" $CONF | awk '{print $2}'`
  GROUPID=`grep "^Group" $CONF | awk '{print $2}'`
  CACHEROOT=`grep "^CacheRoot" $CONF | awk '{print $2}'`
  if [ "$CACHEROOT" != "$MAINCACHE" ]; then
    echo "CacheRoot $CACHEROOT in $CONF does not match"
    echo "the directory you have specified to link as a link_cache argument"
    echo "as the main cache: $MAINCACHE"
    echo ""
    echo "Modify $CONF or the link_cache argument so that these directories agree"
    echo ""
    exit 1
  fi
else
  echo "No such file $CONF.  Aborting"
  echo ""
  exit 1
fi

# Make sure the caches requested to link exist and
# that the caches are not in use.
CACHE_LIST="$MAINCACHE $OTHERCACHES"
if [ ! -d $MAINCACHE ]; then
  echo "Making directory $MAINCACHE"
  mkdir $MAINCACHE
else
  echo "$MAINCACHE already exists"
  for i in $CACHE_DIRS; do
    if [ -d $MAINCACHE/$i ]; then
      echo "Cache $MAINCACHE must be empty.  Aborting"
      echo ""
      echo "Remove everything in $MAINCACHE" 
      echo ""
      exit 1
    fi
  done 
fi
for i in $OTHERCACHES; do
  if [ ! -d $i ]; then
    echo "Making directory $i"
    mkdir $i
  else
    echo "$i already exists"
    for j in $CACHE_DIRS; do
      if [ -d $i/$j ]; then
        echo "Cache $i must be empty. aborting"
        echo ""
        echo "Remove everything in $i" 
        echo ""
        exit 1
      fi
    done
  fi
done
chown -R $USERID:$GROUPID $CACHE_LIST
chmod -R 750 $CACHE_LIST

# Set up the links
echo "Setting up links from $MAINCACHE to $OTHERCACHES"

# Count the number of apache cache directories
let ndirs=0
for i in $CACHE_DIRS; do
  let ndirs=ndirs+1
done

# Count the number of other cache drives besides the main
let cnt=0
for i in $OTHERCACHES; do
  let cnt=cnt+1
done

# Set up a list of other caches ndirs in length that cycles
# through them all repeatedly
let i=0
biglist=""
while [ i -lt $ndirs ]; do
  biglist="$biglist$OTHERCACHES "
  let i=i+cnt
done

# This command makes each (blank separated) token a line argument from $1 to $n
set $CACHE_DIRS
let i=0
for disk in $biglist; do
    echo "ln -s $disk/$1 $MAINCACHE/$1"
    mkdir $disk/$1
    ln -s $disk/$1 $MAINCACHE/$1
  shift                # Move $2 to $1, $3 to $2, etc.
  let i=i+1
  if [ $i -ge $ndirs ]; then
    break
  fi
done

# Change ownership and permissions
echo "Changing the cache ownership"
chown -R $USERID:$GROUPID $CACHE_LIST
chmod -R 750 $CACHE_LIST
echo ""
echo "Linking finished.  Restart httpd to make use of the links"
echo ""
