OK, the revised script seems to be working as intended now. I've removed the check for the .hmt file having been accessed within the last 5 minutes and now simply check for the humaxtv process having the file open.
There are plenty of comments if people want to work out is going on at each point.
Code:
#!/bin/sh
#Set these here otherwise path problems break the script when run from cron
FIND=/mod/bin/busybox/find
GREP=/mod/bin/busybox/grep
SED=/mod/bin/busybox/sed
LSOF=/mod/bin/lsof
BASENAME=/mod/bin/busybox/basename
RMDIR=/mod/bin/busybox/rmdir
MV=/mod/bin/busybox/mv
#Needed to stop the list using spaces as delimiters
IFS=$'\n'
#Despite the pipe following the next find, the IFS still needs to be set
#Find all the top level directories in "My Video" that contain
#a .series file and iterate through them
$FIND /media/My\ Video/ -maxdepth 2 -mindepth 2 -name '.series' | $SED 's/\/.series$//g' | while read dir; do
#Strip out the series name from the directory name and look for
#other directories with the same name
series=`$BASENAME "$dir"`
echo "Checking $dir"
location=`$FIND /media/My\ Video/ -mindepth 2 -type d -name "$series"`
#$location is only set if another directory has been found with the same name
if [ -n "$location" ]; then
echo "Filing for $location"
#Use the hmt files to identify the episode base filename and strip off the suffix using sed
for episodefile in `$FIND "$dir/" -name '*.hmt' | $SED 's/.hmt$//'`
do
#Find and move all the files for this episode, if they are not in use
episode=`$BASENAME "$episodefile"`
inuse=`$LSOF | $GREP "$episode".ts`
if [ -n "$inuse" ]; then
echo "$episodefile is in use - skipping"
else
echo "Moving $episode to $location"
$MV "$dir/$episode".* "$location"
fi
done
if [ "$(ls $dir)" ]; then
#There is probably another episode being recorded
#we don't want to interfere, so do nothing
echo "Series directory not empty"
else
#.series is a hidden file, and the only hidden file that should exist
#if there are no other files in the directory, move .series as well
#and then remove the series directory at the top level - the filing is now done
#.series has to be moved last, in case the box powers down mid script
#in which case, re-running it should still cleanly finish the process
$MV $dir/.series $location
echo "Removing empty series directory"
$RMDIR "$dir"
fi
fi
done
#And show all the series that have been processed in the last day
echo "Recently filed:"
$FIND /media/My\ Video/ -mindepth 3 -mtime -1 -name '.series' | $SED 's/\/.series$//g'
Save the script in /mod/bin/seriesfiler and use 'chmod 755' to make it executable.
You need to install the lsof package to use the script, if it hasn't already been installed.
I have added the cron package and have set it to run using the following crontab entry in /mod/var/spool/cron/crontabs/root
Code:
0,15,30,45 * * * * /mod/bin/seriesfiler 2>&1 > /mod/tmp/seriesfiler.log
Once that is done, it's a matter of running
Code:
/mod/etc/init.d/S01crond restart
but after the initial configuration it should then start automatically.
One other thing that came up follows on from the analysis of the .series file posted on here last night, where it is now clear that there are details of the number of viewed files etc. in the .series file - copying this file into the destination directory is obviously broken, but the only side-effect is whether the 'new file' behaviour would be correct. I will see what I can do to fix it if it matters to enough people.
And finally, there is no reason that this can't be packaged up the same way as dedup is, although it might be better to package multiple scripts such as this into one as they don't exactly take up a lot of space.
Use of this script is 'At Your Own Risk' - don't blame me if it eats your video collection, but it has passed the first hurdle which is running for a day on my own box without any domestic violence, so it's unlikely to do any major or irreversible damage.
Edit: I had problems running the script from cron but I think it was path related. I've updated the script again to code in paths for find, basename, grep etc.