#!/bin/bash
#
# @package      hubzero-telequotad
# @file         Makefile
# @author       Nicholas J. Kisseberth <nkissebe@purdue.edu>
# @copyright    Copyright (c) 2005-2014 HUBzero Foundation, LLC.
# @license      http://www.gnu.org/licenses/lgpl-3.0.html LGPLv3
#
# Copyright (c) 2005-2014 HUBzero Foundation, LLC.
#
# This file is part of: The HUBzero(R) Platform for Scientific Collaboration
#
# The HUBzero(R) Platform for Scientific Collaboration (HUBzero) is free
# software: you can redistribute it and/or modify it under the terms of
# the GNU Lesser General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# HUBzero is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# HUBzero is a registered trademark of HUBzero Foundation, LLC.
#

PATH=/usr/bin:/bin
export PATH

#
# This is run as root.  We switch to the specified user and re-invoke.
#
if [[ $(id -u) == 0 ]]
then
  if (( $# > 2 ))
  then
    echo "Command was '$*'"
    echo "Syntax: $0 [degree=<degree>] user=<username>"
    exit 1
  fi

  degree=default
  user=""
  cmd=$0
  while (( $# >= 1 ))
  do
    case $1 in
      degree=*) degree=${1#degree=}; shift;;
      user=*) user=${1#user=}; shift;;
      *) user=$1; shift;;
    esac
  done

  #
  # Make sure that user exists.
  #
  id -u $user > /dev/null || exit 1

  #
  # Change to the user's home directory.
  # Invoke su without --login so that dotfiles are not invoked.
  # Otherwise, the user could run arbitrary things on the fileserver.
  #
  dir=$(getent passwd $user | cut -d: -f6)
  cd $dir
  exec su -c "$cmd degree=${degree}" ${user}
fi

degree=default
cmd=$0
while (( $# >= 1 ))
do
  case $1 in
    degree=*) degree=${1#degree=}; shift;;
    *) echo "Purge: Unrecognized option '$1'"; shift;;
  esac
done

#
# Remove the expired session directories.
#
find ~/data/sessions -name '*-expired' | while read dir
do
  echo Removing $dir
  rm -rf $dir
done

#
# If the results directory doesn't exist, there's nothing more we can do.
#
if [[ ! -d ~/data/results ]]
then
  echo Directory ~/data/results does not exist.
else
  if [[ $degree == default ]]
  then
    #
    # Sort all files in data/results/... in order of age.
    #
    echo "Deleting only enough result files to meet storage limit."
    if (( `find ~/data/results -not -type d | wc -l` > 0 ))
    then
      ls -c1tr `find ~/data/results -not -type d` | while read file
      do
        if quota -q > /dev/null 2>&1
        then
          break
        else
          echo Removing ${file}
          rm -f ${file}
        fi
      done
    fi
  elif [[ $degree =~ olderthan[0-9][0-9]* ]]
  then
    d=${degree#olderthan}
    echo "Deleting result files older than ${d} days."
    find ~/data/results -mtime +${d} -not -type d -exec rm -fv {} \;
  elif [[ $degree == all ]]
  then
    echo "Deleting all result files."
    rm -rf ~/data/results
  else
    echo "Purge: Unrecognized degree option: '${degree}'"
  fi
fi

#
# Remove empty directories.
#
find ~/data/results -type d -exec rmdir {} \; > /dev/null 2>&1
rmdir ~/data/results > /dev/null 2>&1

echo
echo
if quota -q > /dev/null 2>&1
then
  echo Your usage has been reduced below your quota.
else
  echo The attempt to purge old files did not reduce your usage below your quota.
fi
