#!/bin/sh
# This is to be run from cron.
# This shell script runs Perl scripts for creating symbolic links to PMID.pdf reprint
# files in human readable pathnames based on author name, year, journal name, vol# etc.
# For example, user-level crontab may look like:
# % crontab -l
# 11 5,17 * * * /bin/sh /usr/local/bin/updatePDFlinks
# which will invoke this script at 5:11, and 17:11 daily.
# --- 

PATH=/usr/local/bin:/usr/bin:/bin

# default value; override in .pubmedpdfrc
# PMIDROOT="$HOME/reprints"

##### CUSTOMIZE THE LINE BELOW:SANITIZED ####
PMIDROOT="/documents/myreprints"

# extract non-default PATH and PMIDROOT values, and any other config
# source "$HOME/.pubmedpdfrc"

# the PMID directory must pre-exist...
if [ ! -e "$PMIDROOT" ] ; then
    echo "$PMIDROOT does not exist; creating."
    mkdir "$PMIDROOT" || exit
fi
# ...and be writable
if [ ! -w "$PMIDROOT" ] ; then 
    echo "Can't write to $PMIDROOT." ; exit
fi

# the PMID directory must pre-exist...
if [ ! -e "$PMIDROOT/PMID" ] ; then
    echo "$PMIDROOT/PMID does not exist; creating."
    mkdir "$PMIDROOT/PMID" || exit
fi
# ...and be writable
if [ ! -w "$PMIDROOT/PMID" ] ; then 
    echo "Can't write to $PMIDROOT/PMID." ; exit
fi


# the symlinks directory must pre-exist...
if [ ! -e "$PMIDROOT/symlinks" ] ; then
    echo "$PMIDROOT/symlinks does not exist; creating."
    mkdir "$PMIDROOT/symlinks" || exit
fi
# ...and be writable
if [ ! -w "$PMIDROOT/symlinks" ] ; then 
    echo "Can't write to $PMIDROOT/symlinks." ; exit
fi

# the log directory must pre-exist...
if [ ! -e "$PMIDROOT/log" ] ; then
    echo "$PMIDROOT/log does not exist; creating."
    mkdir "$PMIDROOT/log" || exit
fi
# ...and be writable
if [ ! -w "$PMIDROOT/log" ] ; then
    echo "Can't write to $PMIDROOT/log." ; exit
fi

# now ensure our perl exists...
perl -e "" || exit

# ...is new enough...
perl -e "if($]>=5){exit 0;}else\
{print \"Perl version >= 5 required.\n\"; exit 1;}" || exit

# ...as has needed modules
FAIL=""
if ! perl -e "use File::Path; exit 0" ; then
    echo
    echo "Perl module File::Path missing. Suggestion:"
    echo "  sudo perl -MCPAN -e shell"
    echo "  install File::Path"
    FAIL="yes"
fi
if ! perl -e "use LWP::Simple; exit 0" ; then
    echo
    echo "Perl module LWP::Simple missing. Suggestion:"
    echo "  sudo perl -MCPAN -e shell"
    echo "  install LWP::Simple"
    FAIL="yes"
fi
if ! perl -e "use XML::DOM; exit 0" ; then
    echo
    echo "Perl module XML::DOM missing. Suggestion:"
    echo "  sudo perl -MCPAN -e shell"
    echo "  install XML::DOM"
    FAIL="yes"
fi
if ! perl -e "use Getopt::Std; exit 0" ; then
    echo
    echo "Perl module Getopt::Std missing. Suggestion:"
    echo "  sudo perl -MCPAN -e shell"
    echo "  install Getopt::Std"
    FAIL="yes"
fi

if [ $FAIL ] ; then
    exit
fi

#
# ======= File and path configurations =================
# (sanitized)
# PMID directory containing real reprint files
PMIDdir="$PMIDROOT/PMID"

# Top directories containing journals and authors links
jSymLinkDir="$PMIDROOT/symlinks/journals"
aSymLinkDir="$PMIDROOT/symlinks/authors"

# This file contains names of PDF files in PMID directory (for mirroring ease)
# Also written to by site sync scripts run as other users (do chmod ug+w for this).
myPMIDlist="$PMIDROOT/symlinks/PMID.filelist"
myPMIDlist2="$PMIDROOT/PMID.filelist"
fileCount="$PMIDROOT/xcount.txt"

# Journal link log keeps all records of updates.
jUpdateHistoryLog="$PMIDROOT/log/add_history.log"

# Author link log is just for the last update, because there are too many lines otherwise.
aUpdateLastLog="$PMIDROOT/log/au_addedlast.log"

# Update date/time log just to see when this script has been run.
logFile="$PMIDROOT/log/update.log"

# now make sure we can actually write to these files.
# If they don't exist, we know we can create them.
if [ -e $myPMIDlist ] && [ ! -w $myPMIDlist ] ; then 
    echo "Can't write $myPMIDlist"
    exit
fi
if [ -e $jUpdateHistoryLog ] && [ ! -w $jUpdateHistoryLog ] ; then 
    echo "Can't write $jUpdateHistoryLog"
    exit
fi
if [ -e $aUpdateLastLog ] && [ ! -w $aUpdateLastLog ] ; then 
    echo "Can't write $aUpdateLastLog"
    exit
fi
if [ -e $logFile ] && [ ! -w $logFile ] ; then 
    echo "Can't write $logFile"
    exit
fi

# ======================================================
# Start processing here

# Delete junk in the PMID directory because these get in the way.
# Add other junk files as needed.
/bin/rm -f $PMIDdir/.DS_Store
/bin/rm -f $PMIDdir/Thumbs.db
/bin/rm -f $PMIDdir/.*.pdf	# SMB access from Mac with resource fork

/bin/date > $aUpdateLastLog
/usr/local/bin/pubmedlinknew.pl -a -p $PMIDdir -s $aSymLinkDir >> $aUpdateLastLog

echo "-----" >> $jUpdateHistoryLog
/bin/date >> $jUpdateHistoryLog
/usr/local/bin/pubmedlinknew.pl    -p $PMIDdir -s $jSymLinkDir >> $jUpdateHistoryLog

cd $PMIDdir; /bin/ls -1 *.pdf > $myPMIDlist
/bin/cp -f $myPMIDlist $myPMIDlist2
wc -l $myPMIDlist2 | cut -f1 -d "/" > $fileCount

/bin/date >> $logFile

# end of script
