#!/bin/bash # Make sure VUFIND_HOME is set: if [ -z "$VUFIND_HOME" ] then # set VUFIND_HOME to the absolute path of the directory containing this script # https://stackoverflow.com/questions/4774054/reliable-way-for-a-bash-script-to-get-the-full-path-to-itself export VUFIND_HOME="$(cd "$(dirname "$0")" && pwd -P)"/.. if [ "$VUFIND_HOME" = /.. ] then exit 1 fi fi # Find harvest directory for future use HARVEST_DIR="$VUFIND_LOCAL_DIR/harvest" if [ ! -d $HARVEST_DIR ] then HARVEST_DIR="$VUFIND_HOME/harvest" fi if [ -z "$MAX_BATCH_COUNT" ] then MAX_BATCH_COUNT=10 fi BASEPATH_UNDER_HARVEST=true LOGGING=true MOVE_DATA=true function usage { cat <&2 exit -1;; \?) echo "Unrecognized option '-$OPTARG'" >&2;; esac done #Decrement the argument pointer so it points to next argument shift $(($OPTIND - 1)) # Make sure command line parameter was included: if [ -z "$1" ] then usage exit 1 fi # Check MAX_BATCH_COUNT is a positive integer if ! [[ "$MAX_BATCH_COUNT" =~ ^[1-9][0-9]*$ ]] then echo "MAX_BATCH_COUNT (option -x) is not a positive integer: \"$MAX_BATCH_COUNT\"" exit 1 fi # Set up BASEPATH and check if the path is valid: if [ $BASEPATH_UNDER_HARVEST == false ] then BASEPATH=$1 else BASEPATH="$HARVEST_DIR/$1" fi if [ ! -d $BASEPATH ] then echo "Directory $BASEPATH does not exist!" exit 1 fi # Create log/processed directories as needed: if [ $LOGGING == true ] then if [ ! -d $BASEPATH/log ] then mkdir $BASEPATH/log fi fi if [ $MOVE_DATA == true ] then if [ ! -d $BASEPATH/processed ] then mkdir $BASEPATH/processed fi fi # The log() function can be redefined to suit a variety of logging needs # Positional parameters must be consistent: # $1 = name of the first file being imported if [ $LOGGING == false ] then function log { cat - > /dev/null } else function log { local FILES=$@ local LOGFILE if [ $# -eq 1 ] then LOGFILE=$BASEPATH/log/`basename $1`.log > $LOGFILE else LOGFILE=$BASEPATH/log/`basename $1`_and_more.log echo -e "This log is for the following files: \n$FILES\n" > $LOGFILE fi cat -u - >> $LOGFILE } fi # Process all the files in the target directory: find -L $BASEPATH -maxdepth 1 \( -iname "*.xml" -o -iname "*.mrc" -o -iname "*.marc" \) -type f -print0 | sort -z | xargs -0 -r -n $MAX_BATCH_COUNT | \ while read -d $'\n' files do # Logging output handled by log() function # PROPERTIES_FILE passed via environment $VUFIND_HOME/import-marc.sh $files 2> >(log $files) if [ "$?" -eq "0" ] && [ $MOVE_DATA == true ] then for file in $files do mv $file $BASEPATH/processed/`basename $file` done fi done