#!/bin/bash
#
# @package      hubzero-telequotad
# @file         Makefile
# @copyright    Copyright (c) 2005-2020 The Regents of the University of California.
# @license      http://opensource.org/licenses/MIT MIT
#
# Copyright (c) 2005-2020 The Regents of the University of California.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# HUBzero is a registered trademark of The Regents of the University of California.
#

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
