#!/bin/sh ########################################################################### ## Copyright (C) Wizardry and Steamworks 2018 - License: GNU GPLv3 ## ## Please see: http://www.gnu.org/licenses/gpl.html for legal details, ## ## rights of fair usage, the disclaimer and warranty conditions. ## ########################################################################### # Load system defaults. SCRIPT_NAME=`basename $0` [ -f "/etc/default/$SCRIPT_NAME" ] && . /etc/default/"$SCRIPT_NAME" # Check that the paths exist. for DIRECTORY in $DOWNLOADS_FOLDER $TORRENTS_FOLDER $DROPBOX_FOLDER; do if [ ! -d $DIRECTORY ]; then logger -p daemon.err "$0: Could not find $DIRECTORY." exit 1 fi done # Acquire a lock. LOCK_FILE="/var/lock/$SCRIPT_NAME" if mkdir $LOCK_FILE 2>&1 >/dev/null; then trap '{ rm -rf $LOCK_FILE; }' KILL QUIT TERM EXIT INT HUP else exit 0 fi IFS=","; for CREATE_FOLDER in `find $DROPBOX_FOLDER -maxdepth 1 -mindepth 1 -not -path '\.*' -type d -printf '%P\n'`; do logger -p daemon.info "$SCRIPT_NAME: Creating torrent for directory $CREATE_FOLDER" # Create the .torrent metafile. mkdir -p /tmp/$SCRIPT_NAME mktorrent -a $HTTP_TRACKER -a $UDP_TRACKER "$DROPBOX_FOLDER/$CREATE_FOLDER" -o "/tmp/$SCRIPT_NAME/$CREATE_FOLDER.torrent" 2>/dev/null >/dev/null if [ ! -f "/tmp/$SCRIPT_NAME/$CREATE_FOLDER.torrent" ]; then logger -p daemon.err "$SCRIPT_NAME: Something went wrong whilst generating the \"$CREATE_FOLDER.torrent\" file." exit 1 fi # If the .torrent metafile already exists, compare it to the temporary metafile already generated. if [ -f "$TORRENTS_FOLDER/$CREATE_FOLDER.torrent" ]; then SUM_NEW=`sha1sum "/tmp/$SCRIPT_NAME/$CREATE_FOLDER.torrent" | awk '{ print $1 }'` SUM_OLD=`sha1sum "$TORRENTS_FOLDER/$CREATE_FOLDER.torrent" | awk '{ print $1 }'` if [ x$SUM_NEW = x$SUM_OLD ]; then # The files are identical so just terminate. exit 0 fi fi # Check if the download folder already exists and delete it. if [ -d "$DOWNLOADS_FOLDER/$CREATE_FOLDER" ]; then rm -rf "$DOWNLOADS_FOLDER/$CREATE_FOLDER" fi # Copy the new folder into the downloads folder. mv "$DROPBOX_FOLDER/$CREATE_FOLDER" "$DOWNLOADS_FOLDER" # Copy the temporary .torrent metafile into the torrents folder. mv "/tmp/$SCRIPT_NAME/$CREATE_FOLDER.torrent" "$TORRENTS_FOLDER" logger -p daemon.info "$SCRIPT_NAME: Created torrent for directory \"$CREATE_FOLDER\"!" done