#!/bin/sh

echo
echo "BECK-2 cleanup by lcamtuf"
echo "This program cleans up battlefield..."
echo

# Assume that user's homepage is into /home/httpd/html/USER...
TARGET=/home/httpd/html/`whoami`

echo -n "Going directly to your www home... "
cd $TARGET &>/dev/null

if [ "$PWD" != "$TARGET" ]; then
  echo "ERROR"
  exit 0
fi

echo "OK"

SUBDIR=x

if [ -d $SUBDIR ]; then
  echo "Removing subdirectory $SUBDIR from ${PWD}."
  echo
else
  echo "Naaah, nothing to do. Directory $SUBDIR not found."
  echo
  exit 0
fi

HOME=$PWD

echo -n "Going deeper and deeper... "

while [ -d $SUBDIR ]; do
  OLDDIR=$PWD
  cd $SUBDIR &>/dev/null
  if [ "$PWD" = "$OLDDIR" ]; then
    echo "ERROR"
    exit 0
  fi
done

echo "OK"

echo -n "Enough, now removing everything... "

while [ "$PWD" != "$HOME" ]; do
  OLDDIR=$PWD
  cd .. &>/dev/null
  rmdir $SUBDIR &>/dev/null
  if [ "$PWD" = "$OLDDIR" ]; then
    echo "ERROR"
    exit 0
  fi
done

echo  "OK"
