#!/bin/sh

################################################################################
# Script name:  dated_dir_creator.sh                                           #
# Author:       SoD                                                            #
# Contact:      SoD- @ EFNet                                                   #
# Releasedate:  2001-05-16                                                     #
#                                                                              #
# This script is executed from crontab every midnight and then creates new     #
# dated dirs in the paths you've specified. It chmods them and if you want,    #
# creates symlinks to them. It lets you set the "closing-time" of yesterdays   #
# dir and it writes to glftpd.log in such a way that a bot can announce it.    #
#                                                                              #
# Installation: -Put this file in some approperiate place, for example         #
#                /glftpd/bin/dated_dir_creator.sh                              #
#               -Make it executable(chmod 755 /glftpd/bin/dated_dir_creator.sh)#
#               -Make an entry in your crontab so that ths script is run at    #
#                midnight every day:                                           #
#                0 0 * * * /glftpd/bin/dated_dir_creator.sh                    #
#               -Edit this script to suit your needs                           #
#               -If you are using Darkheart's botscript you should add the     #
#                following 8 lines to sitebot-glftpd.api and then rehash it.:  #
#                set chans(sectionname-NEW_DATED_DIR) " #channelname "         #
#                               ^-edit                       ^-edit            #
#                set chans(sectionname-CLOSED_DATED_DIR) " #channelname "      #
#                               ^-edit                          ^-edit         #
#                set echovars(NEW_DATED_DIR) "name section archive_close_time" #
#                set echovars(CLOSED_DATED_DIR) "name section"                 #
#                set enabled_announce(NEW_DATED_DIR) 1                         #
#                set enabled_announce(CLOSED_DATED_DIR) 1                      #
#                set mask(NEW_DATED_DIR) "[b]\[%sitein NEW DATED DIR\][b] [b]%name[b] created in [b]%section[b] %archive_close_time"
#                set mask(CLOSED_DATED_DIR) "[b]\[%sitein CLOSED DIR\][b] [b]%name[b] in [b]%section[b] has been closed"
#               -If you are using vShit's botscript you should add the         #
#                following 2 lines in the scanlog proc and then rehash it.:    #
#                NEW_DATED_DIR: {sndall "[b]\[$sitename NEW DATED DIR\][b] [b][lindex $args 0][b] created in [b][lindex $args 1][b] [lindex $args 2]"}
#                CLOSED_DATED_DIR: {sndall "[b]\[$sitename CLOSED DIR\][b] [b][lindex $args 0][b] in [b][lindex $args 1][b] has been closed"}
#               -If you are using vrpack (1.6.0 Beta) botscript you should add #
#                the following 2 lines in the scanlog proc and then rehash it.:#
#                NEW_DATED_DIR: {sndall "\002\[$sns NEW DATED DIR\]\002 \002[lindex $args 0]\002 created in \002[lindex $args 1]\002 [lindex $args 2]"}
#                CLOSED_DATED_DIR: {sndall "\002\[$sns CLOSED DIR\]\002 \002[lindex $args 0]\002 in \002[lindex $args 1]\002 has been closed"}
#                                                                              #
# Requirements: chmod, cut, date, echo, let, ln, mkdir, rm                     #
#                                                                              #
# Limitations:  -Can't handle dirnames with spaces in them.                    #
#               -You might also wanna check the rules for symlinks inside      #
#                glftpd (glftpd.docs).                                         #
#                                                                              #
################################################################################

# Here you specify the dirs in were you want new dated dirs created, use a 
# trailing frontslash ('/') for every path and separate them with a space.
PATHS="/glftpd/site/mp3/ /glftpd/site/0-day/"

# Whats the name and location of the symlinks to the "today"-dirs? Set it to 'a'
# if you don't want a "today"-symlink. Put in same order as $PATHS and separate
# them with a space. A symlink is a name so no trailing frontslash ('/')
SYMLINKS="/glftpd/site/today /glftpd/site/today-0day"

# What name do you want the dated dirs to get? (eg, %m%d, %m-%d, %Y.%d.%m). This
# applies to all sections. These are a few options (taken from dates man-page:
# %a  -  locale's abbreviated weekday name (Sun..Sat)
# %b  -  locale's abbreviated month name (Jan..Dec)
# %d  -  day of month (01..31)
# %m  -  month (01..12)
# %Y  -  last two digits of year (00..99)
DIR_NAME="%m%d"

# How long after the creation of the new dated dirs shall creation of new dirs
# be allowed in old dirs. (ie, for how long past midnight shall the users be
# allowed to upload in yesterdays dir). Put in same order as $PATHS and separate
# them with a space. Setting this to more than 24h is probably not that wise.
# Time in full hours (no 0.5, 2.5, ...).
ARCHIVE_CLOSING_TIME="1 0"

# Do you want the script to write to glftpd.log? (for announcing) [y/n]
ANNOUNCE=y

# What's the location of glftpd.log?
GL_LOG_FILE="/glftpd/ftp-data/logs/glftpd.log"

# What do you want the botscript to call the sections, put in same order as
# $PATHS and separate them with a space ( eg, MP3, 0-DAY, DivX )
SECTION_NAMES="MP3 0-DAY"

################################################################################
# No more specifications needed below,just some editing if the script won't work
################################################################################

TODAY_DIR=`date +$DIR_NAME`
YESTERDAY_DIR=`date -d '1 day ago' +$DIR_NAME`
COUNTER=1

# This function chmod's the yesterday dir (and announces it)
chmod_yesterday_dir () {
    sleep `echo $ARCHIVE_CLOSING_TIME | cut -d " " -f$1`h
    chmod 555 `echo $PATHS | cut -d " " -f$COUNTER`$YESTERDAY_DIR
    if [ $ANNOUNCE = y ] && [ $ARCHIVE_CLOSE_TIME -gt 0 ]; then
        echo `date +"%a %b %d %T %Y"` CLOSED_DATED_DIR: \
        "`date -d '1 day ago' +$DIR_NAME`" "$SECTION_NAME" >> $GL_LOG_FILE
    fi
}

# Loop through all $PATHS, one by one
for PATHH in $PATHS; do

    # Make the new dated dir and change its and yesterday dirs permissions
    if [ ! -e "$PATHH$TODAY_DIR" ]; then
        mkdir $PATHH$TODAY_DIR
        chmod 777 $PATHH$TODAY_DIR
    fi
    ARCHIVE_CLOSE_TIME=`echo $ARCHIVE_CLOSING_TIME | cut -d " " -f$COUNTER`
    SECTION_NAME=`echo $SECTION_NAMES | cut -d " " -f$COUNTER`
    chmod_yesterday_dir `echo $COUNTER` &

    # Remove old symlink, get the name of the one to create and create it
    if [ `echo $SYMLINKS | cut -d " " -f$COUNTER` != "a" ]; then
        rm -f `echo $SYMLINKS | cut -d " " -f$COUNTER`
        COUNT=1
        # Compare path to dir with path to symlink char by char,stop at mismatch
        while [ `echo $PATHH | cut -c$COUNT` = `echo $SYMLINKS | \
        cut -d " " -f$COUNTER | cut -c$COUNT` ]; do
            let COUNT=$COUNT+1
        done;
        let COUNT=$COUNT-1
        # Fix so that the last char in the path to the dir is a '/'
        while [ `echo $PATHH | cut -c$COUNT` != "/" ]; do
            let COUNT=$COUNT-1
        done
        # Remove the trailing '/'
        let COUNT=$COUNT-1
        COMMON_PATH=`echo $PATHH | cut -c1-$COUNT`
        DIFFERENCE=`echo ${PATHH#${COMMON_PATH}}`
        SYMLINK_SOURCE=`echo "./"$DIFFERENCE$TODAY_DIR`
        SYMLINK_DESTINATION=`echo $SYMLINKS | cut -d " " -f$COUNTER`
        ln -s $SYMLINK_SOURCE $SYMLINK_DESTINATION
    fi

    # Announce the making of the new dir if $ANNOUNCE is set to "y"
    if [ $ANNOUNCE = y ] && [ $ARCHIVE_CLOSE_TIME -eq 0 ]; then
        echo `date +"%a %b %d %T %Y"` NEW_DATED_DIR: "`date +$DIR_NAME`" \
        "$SECTION_NAME" \"\(archive has been closed\)\" >> $GL_LOG_FILE
    elif [ $ANNOUNCE = y ] && [ $ARCHIVE_CLOSE_TIME -eq 1 ]; then
        echo `date +"%a %b %d %T %Y"` NEW_DATED_DIR: "`date +$DIR_NAME`" \
        "$SECTION_NAME" \"\(archive will be closed in 1 hour\)\" >> $GL_LOG_FILE
    elif [ $ANNOUNCE = y ] && [ $ARCHIVE_CLOSE_TIME -gt 1 ]; then
        echo `date +"%a %b %d %T %Y"` NEW_DATED_DIR: "`date +$DIR_NAME`" \
        "$SECTION_NAME" \"\(archive will be closed in $ARCHIVE_CLOSE_TIME \
        hours\)\" >> $GL_LOG_FILE
    fi

    let COUNTER=$COUNTER+1

done;
wait
exit