#!/usr/bin/bash
# +----------------------------------------------------------------------+
# | Issabel version 4.0                                                  |
# | http://www.issabel.org                                               |
# +----------------------------------------------------------------------+
# | Copyright (c) 2019 Issabel Foundation                                |
# +----------------------------------------------------------------------+
# | The contents of this file are subject to the General Public License  |
# | (GPL) Version 2 (the "License"); you may not use this file except in |
# | compliance with the License. You may obtain a copy of the License at |
# | http://www.opensource.org/licenses/gpl-license.php                   |
# |                                                                      |
# | Software distributed under the License is distributed on an "AS IS"  |
# | basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See  |
# | the License for the specific language governing rights and           |
# | limitations under the License.                                       |
# +----------------------------------------------------------------------+


PARSED_OPTIONS=$(getopt -n "$0" -o h  --long "recordings:,help,backups:,faxes:"  -- "$@")

function parse_args {
        #PARSED_OPTIONS=$(getopt -n "$0"  -o dhb: --long "dadhi,help,backup-file:"  -- "$@")
        #Bad arguments, something has gone wrong with the getopt command.
        if [ $? -ne 0 ];
        then
                echo ERROR getting args
                exit 1
        fi
        eval set -- "$PARSED_OPTIONS"

        while true
        do
                case "$1" in
                -h|--help)
                        print_usage
                         shift;;
                --recordings)
                        if [ -n "$2" ];
                        then
                                RM_RECORDINGS=$2
                        else
                                echo "ERROR: Missing days paramenter"
                                exit
                        fi
                        shift 2;;
                --backups)
                        if [ -n "$2" ];
                        then
                                RM_BACKUPS=$2
                        else
                                echo "ERROR: ERROR: Missing days paramenter"
                                exit
                        fi
                        shift 2;;
                --faxes)
                        if [ -n "$2" ];
                        then
                                RM_FAXES=$2
                        else
                                echo "ERROR: Password is empty"
                                exit
                        fi
                        shift 2;;
                 --)
                        shift
                        break;;
                *)
                        echo "Invalid option $1"
                        print_usage
                        exit 1 ;;
                esac
        done
}

function create_cron
{
# $1: filename $2: directory $3: days $4: files pattern
  if [ $3 -ge 1 ]
  then
    echo "find $2 -type f -name '$4' -mtime +$3 -exec rm -f {} \;" > /etc/cron.daily/$1
    chmod 700 /etc/cron.daily/$1
  else
    if [ ! -z $1 ]
    then
      rm -f /etc/cron.daily/$1
    fi
  fi
  return 0
}

parse_args
if [ ! -z $RM_RECORDINGS ]
then
  create_cron rm_recordings "/var/spool/asterisk/monitor/" $RM_RECORDINGS "*.*"
fi
if [ ! -z $RM_BACKUPS ]
then
  create_cron rm_backups "/var/www/backup/" $RM_BACKUPS "*.tar"
fi
if [ ! -z $RM_BACKUPS ]
then
  create_cron rm_faxes "/var/www/faxes/" $RM_FAXES "*.*"
fi

