#!/bin/sh

################################################################################
# Script name:  predircheck_v1.0.sh                                            #
# Author:       SoD                                                            #
# Contact:      SoD- @ EFNet                                                   #
# Releasedate:  2001-05-01                                                     #
#                                                                              #
# This script is executed by glftpd before a directory is created and then     #
# checks for up to 3 things ( depending on if you enable all checks or not ):  #
# * Checks for dupe in the dupelog (dirname, not files of course)              #
# * Checks for what group the release origins from                             #
# * Checks for releaseyear (only works for the mp3 scene)                      #
#                                                                              #
# Installation: -Put this file in some approperiate place, for example         #
#                /glftpd/bin/predircheck_v1.0.sh                               #
#               -Make it executable (chmod 755 /glftpd/bin/predircheck_v1.0.sh)#
#               -Edit glftpd.conf and change the line with pre_dir_check to:   #
#                pre_dir_check /bin/predircheck_v1.0.sh                        #
#               -Edit this script to suit your needs                           #
#                                                                              #
# Requirements: awk, cat, echo, expr, grep, tail, tr                           #
#                                                                              #
# Limitations:  -Can't handle dirnames with spaces in them.                    #
#                                                                              #
################################################################################

# Put whatever groups from whatever scene you want banned here, use lowercase
# and separate with space.
BANNED_GROUPS="impg idc krbz tmg cdiamond"

# What years do you want banned, all years up until and including this one will
# be denied by the dirscript if you invoke that function.
BANNED_YEARS="2000"

# Where is your dupelog located?
GL_DUPELOG="/ftp-data/logs/dupelog"

# Now setup your sections and what features to use in them in the case-switch at
# the bottom of the script. Not that userfriendly but who cares... Remember to
# set it so that CD1-, CD2, sample- and requestdirs are excluded.

################################################################################

# This function makes shure banned groups releases don't get uploaded.
check_banned_groups () {
    GROUP=`echo $1 | awk -F "-" '{print $NF}' | tr '[:upper:]' '[:lower:]'`
    for BANNED_GROUP in $BANNED_GROUPS; do
        if [ $BANNED_GROUP = $GROUP ]; then
            echo -e "`echo $1 | awk -F "-" '{print $NF}'`s releases are banned"
            exit 2
        fi
    done
}

# This function makes shure that no dupes gets uploaded.
scan_dupelog () {
    TMP=`cat $GL_DUPELOG | grep -i "$1" | tail -n1`
    if [ `echo $TMP | awk '{print $1}'` ]; then
        echo -e "`echo $TMP | awk '{print $2}'` already exists in the dupelog,"\
        " it was last created at `echo $TMP | awk '{print $1}'`"
        exit 2
    fi
}

# This function denies creation of too old mp3's.
check_banned_years () {
    YEAR_FIELD=`expr \`echo $1 | awk -F "-" '{print NF}'\` - 1`
    YEAR=`echo $1 | cut -d "-" -f$YEAR_FIELD | tr -d "()"`
    if [ $YEAR -le $BANNED_YEARS ]; then
        echo -e "This site only accepts releases newer than year $BANNED_YEARS"
        exit 2
    fi
}

# Just put your paths here and call any (or none) of the above functions. Just 
# look at the examples and I think you'll understand how it works. Remember to
# exit every case with "exit ;;".
case $PWD/$1 in
    /site/mp3/????/*/*  ) exit ;;

    /site/mp3/????/*    ) scan_dupelog $1
                          check_banned_groups $1
                          check_banned_years $1
                          exit ;;

    /site/vcd/*/*       ) exit ;;

    /site/vcd/*         ) scan_dupelog $1
                          exit ;;

    /site/iso/*/*       ) exit ;;

    /site/iso/*         ) scan_dupelog $1
                          check_banned_groups $1
                          exit ;;

    /site/0day/????/*   ) scan_dupelog $1
                          check_banned_groups $1
                          exit ;;

    *                   ) exit ;;

esac