#!/bin/sh



failuremsg="Error"
failurecode=1

while [ $# -gt 0 ]
do
    case "$1" in
        --warning|-w)
            failuremsg="Warning"
            failurecode=0;;
    esac
    shift
done

DIRREGEXP="\\.\\./"

echo "Checking for references pointing outside of session directory:"
echo "=============================================================="

REGEXP=$DIRREGEXP
DIR=$DIRMATCH
failed=0
for i in $(seq 1 10); do 
  FILES=`find * -mindepth $((i-1)) -maxdepth $i -type f | xargs`
  if [ -n "$FILES" ]; then 
    grep -s ${REGEXP} ${FILES}
    exit=$?
    if [ "$exit" -eq 0 ] ; then
      failed=1
    fi
  fi
  REGEXP="${DIRREGEXP}${REGEXP}"
done


if [ "$failed" -ne 0 ] ; then
    echo "$failuremsg: Forbidden reference to files outside of their session directory!" 
    exit $failurecode 
fi

echo "  * Success: No relative references to files outside of their session directory found."
exit 0
