#!/bin/sh

function CheckLink
{
    if [ ! -e $1 ]; then
        ln -s $2 $1
	echo Fixed link for $1
	let LINKSFIXED=LINKSFIXED+1
    fi
}

function CheckWritable
{
    PERMS=`ls -ld $1 | awk '{  print $1;  }'`
    if [ "$PERMS" != "drwxrwxrwx" ]; then
        chmod a+w $1
	echo Corrected permissions for $1
	let DIRSFIXED=DIRSFIXED+1
    fi
}

# sign in
echo "SPT/CWIS FixLinks  (Jun 17 2004)"

# try to make sure we're being run in the right place
if [ ! -f SPT--Home.php ]; then
    echo "ERROR:  Must be run from the base SPT/CWIS directory"
    exit 1
fi

# check general links
LINKSFIXED=0
CheckLink MetadataTool/include ../include
CheckLink MetadataTool/images ../images
CheckLink index.php SPT--Home.php

# check user interface links
for UIDIR in SPTUI--*; do
    CheckLink $UIDIR/MetadataTool/include ../include
    CheckLink $UIDIR/MetadataTool/images ../images
    CheckLink $UIDIR/MetadataTool ../MetadataTool/$UIDIR
    if [ -d $UIDIR/Themes ]; then
        CheckLink $UIDIR/MetadataTool/Themes ../Themes
    fi
done

# open permissions on temp and image storage directories
DIRSFIXED=0
CheckWritable ImageStorage 
CheckWritable TempStorage 
CheckWritable ImageStorage/Previews 
CheckWritable ImageStorage/Thumbnails

# report result
if [ $LINKSFIXED -eq 0 ]; then
    echo "All links appeared okay"
else
    echo "Links Repaired = $LINKSFIXED"
fi
if [ $DIRSFIXED -eq 0 ]; then
    echo "All directory permissions appeared okay"
else
    echo "Directory Permissions Repaired = $DIRSFIXED"
fi


