#!/bin/bash
#
# modified by avizion 2001-2007
#
# requires av-ls.sh which is available at:
# http://files.got2get.net/glftpd/
#
# -------------------------------------------------------------------------
# Jehsom's mp3 dirscript v1.4.1 - Prevents creation of undesired directories.
# Copyright (C) 2001 jehsom@jehsom.com
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
# -------------------------------------------------------------------------
#
# This dirscript is primarily for mp3 sites. It allows the siteop to
#   prevent certain types of releases from ever being uploaded by not
#   allowing the user to create the directory in the first place. It
#   is very powerful, allowing restrictions by rls date, rls group,
#   name format, and can disallow live rels, rels already in the dupe
#   log, and rels already existing in your site's PRE directories (to
#   prevent racing of PREs.
#
# Changes 1.4 -> 1.4.1:
#   * "sample" dirs are no longer checked against the dupe DB (like cd[1-9]).
#
# Explanation of configuration options:
# -------------------------------------
# NOCHECK_TREES - Don't process new dirs under these directory hier-
#	archies. Multiple hierarchies should be space delimited.
# DAYSBACK - Number of days back to check with regular dupechecking.
#	If ANALDUPECHECK=1, set this low, as it's quite slow.
# BANNED - Names of groups whose releases aren't allowed (space delim).
# SHIT  - Keywords to ban regardsless where in title.
# ANALDUPECHECK - 1/0 Check thru entire SITE DUPE db to make sure the dir
#	being created is DEFINITELY not a dupe. Very fast.
# DATAPATH - glftpd's datapath, as listed in glftpd.conf
# ALLOWPARENS - Allow dirnames to start with "("
# RIAA_UNDERSCORES - Tries to disallow Names.Like--This.Name-FOO as
#	the RIAA specs say that Names_Must-Be_Like-THIS
# RIAA_NAMELENGTH - Enforces the RIAA 64-char max dirname length
# ALLOWED_YEARS - Disallows directories not containing the 4-digit year 
#	number or one of several permutations of the last 2 digits and
#	punctuation (e.g. *-99*, *.99*, *99.*, *99-*, *99####*, *####99*,
#	etc.) It is very intelligent. I suggest you try it out. Multiple
#	allowed years should be space delimited.
# AFFILS - Space delimited list of groups allowed to break the banned years
#	rules. For this to work, the new dir's name must end in "-GRP", 
#	where GRP is the group you have specified here.
# LOGFILE - Logs the denied MKDIR requests, and lists the reason and
#	user who tried to create the directory. Make sure the file is
#	world writable (chmod 666) so this can work.
# LIVE_OK - 1/0 Whether to allow Live releases from non-affils
# PRE_DIRS - If a rls is already here, its creation will be prevented.

#echo "Checking if group $GROUP (you) are allowed to upload here... ($2)"

if [ "$GROUP" = "AFFILS" ]; then
	case $2 in
		/site/incoming*)
			echo "You are an affiliate - so go ahead..."
			exit 0
		;;

		*)
		;;
	esac
fi

EXCEMPT="avizion"
for GANGSTER in $EXCEMPT; do
	if [ "$GANGSTER" = "$USER" ]; then
		echo "Aight homeboy... go ahead!"
		exit 0
	fi
done
#exit 0
NOCHECK_TREES="/site/requests /site/mp3/groups /site/archive/groups /site/groups"
DAYSBACK="1"
BANNED="`/bin/av-ls.sh /site/mp3/groups` `/bin/av-ls.sh /site/siteops/banned`"
SHIT="bootleg tvrip -tape- -TAPE- BOOTLEG TVRIP"
ANALDUPECHECK="1"
DATAPATH="/ftp-data"
ALLOWPARENS="0"
RIAA_UNDERSCORES="0"
RIAA_NAMELENGTH="0"
ALLOWED_YEARS=""
#AFFILS="`/bin/av-ls.sh /site/mp3/groups`"
AFFILS="`/bin/av-ls.sh /site/groups`"
LOGFILE="/ftp-data/logs/dirscript.log"
LIVE_OK="1"
PRE_DIRS="/site/mp3/groups/*"

#######################
### Ignore the rest ###
#######################

BINS="date expr ls"

function logexit () {
	echo "`date +%m%d`: Denied $1 to user $USER ($2)" >> $LOGFILE
	exit 2
}

[ -w /dev/null ] || { echo "/dev/null must be writable. Exiting."; exit 0; }

# If the dir already exists, it's obviously not right to create it
[ -d "$2/$1" ] && {
	echo "Directory already exists!"
	logexit $2/$1 "Dir Already Existing"
}	


#for bin in $BINS; do 
#    type $bin > /dev/null 2>&1 || {
#        echo "The '$bin' binary must be installed in glftpd's bin dir."
#        logexit $2/$1 "Required bin not found"
#    }
#done

# If we're in an excepted directory tree, allow the dir without checking it
if [ "$2" != "/site/today" ]; then
#echo "-->>>>>$2<<<<---"
for tree in $NOCHECK_TREES; do
	case $2 in
	    ${tree}*)
		exit 0
		;;
	    *)
		;;
	esac
done
fi

#for predir in $PRE_DIRS; do
#	[ -d "$predir/$1" ] && {
#		echo "This release is in the group's pre dir."
#		echo "Please wait until they pre it."
#		logexit $2/$1 "About to be pre'd"
#	}
#done	

# Deny releases starting with '('
[ "$ALLOWPARENS" = "0" ] && {
	case $1 in
	    \(*)
		echo "Releases starting with '(' are not allowed"
		logexit $2/$1 "Parens not allowed"
		;;
	    *)
		;;
	esac
}

# Make sure name is <= 64 chars
[ "$RIAA_NAMELENGTH" = "1" ] && {
	[ "${#1}" -gt "64" ] && {
		echo "This directory name doesn't follow RIAA conventions."
		echo "The name has ${#1} chars, but should be 64 or less."
		logexit $2/$1 "Name too long"
	}
}

# Check for RIAA naming compliance
[ "$RIAA_UNDERSCORES" = "1" ] && {
	echo $1 | grep -E "^[^_]+\.[^_]+\.[^_]+\.[^_]*$" | grep "[-]-" > /dev/null && {
		echo "This directory name doesn't follow RIAA conventions."
		echo "Underscores, not periods, must be used for spaces."
		logexit $2/$1 "Name not RIAA conformant"
	}
}

# Disallow shit releases.
#[ -n "$2" ] && cd $2
#for foo in $SHIT; do
#	echo $1 | grep -iF "\\$foo" > /dev/null && {
#		echo "${foo} releases are not accepted here."
#		logexit $2/$1 "Disallowed shit release"
#	}
#done


# Disallow banned groups.
[ -n "$2" ] && cd $2
for grp in $BANNED; do
	echo $1 | grep -i "[-]${grp}$" > /dev/null && {
		echo "${grp} is either an affil or banned - so cut it out! :)"
		logexit $2/$1 "Unallowed Group"
	}
done




# Check against dupelog
[ "$ANALDUPECHECK" = "1" ] && {
	# Don't check if it's a "CD1" or similar dir
	{ [ ${#1} -le 8 ] && echo $1 | grep -iE "^(dvd|dvd[1-9]|cd|cd[1-9]|sample)" > /dev/null; } || {
		if [ -f $DATAPATH/logs/dupelog ]; then
			nice --20 grep -i " $1$" $DATAPATH/logs/dupelog > /dev/null && {
			echo "Dupe detected! SITE DUPE $1 returns:"
			nice --20 grep -i " $1$" $DATAPATH/logs/dupelog | head -10
			logexit $2/$1 "Dupe"
    		}
	   	else
		echo 'Could not locate dupelog for anal dupechecking!'
		echo "Verify your DATAPATH setting and try again."
    	fi
	}	
}

# Check that the rls has a required year in the name, unless
# it's an affil, in which case we forget about it.
#[ -n "$ALLOWED_YEARS" ] && {
#	yearok="0"; shortname="0"
#	echo $1 | grep -Ei "[-](`echo $AFFILS | sed 's/ /|/g'`)$" > /dev/null && 
#		yearok=1
#	[ ${#1} -le 8 ] || echo $1 | grep -iE "^(cd[1-9]|.*approved)" > /dev/null &&
#		shortname="1"
#	for year in $ALLOWED_YEARS; do
#		echo $1 | grep -E "(${year}|[-\.]${year#??}\b|\b${year#??}[-\.]|\b${year#??}[0-9]{4}\b|\b[0-9]{4}${year#??}\b)" > /dev/null && 
#			yearok="1"
#	done
#	[ "$yearok" = "0" -a "$shortname" = "0" ] && {
#		echo "Unallowed year."
#		logexit $2/$1 "Unallowed Year"
#    }
#}

#{ [ "$LIVE_OK" -eq "1" ] || 
#	echo $1 | grep -Ei "[-](`echo $AFFILS | sed 's/ /|/g'`)$" > /dev/null; } || {
#	echo $1 | grep -Ei "([(]live[)]|[-_.]live[_.](in|at|on)[^[:alpha:]]|[0-9][0-9][-_.][0-9][0-9][-_.][0-9][0-9])" > /dev/null && {
#		echo "Live releases not allowed."
#		logexit $2/$1 "Live"
#	}
#}	

#ago=0
#[ "$shortname" = "0" ] && while [ $ago -le $DAYSBACK ]; do
#	date=`date --date "$ago days ago" +%m%d`
#	ls ../$date 2>/dev/null | grep -i "\b$1\b" > /dev/null 2>&1 && {
#		echo "\"$1\" already exists in the"
#		echo "directory dated $date. Looks like a dupe."
#		logexit $2/$1 "In recent dated dir"
#	}
#	ago=$(($ago + 1))
#done
exit 0

