#!/usr/bin/env bash TMPDIR=$(mktemp -d) CSPLIT_PREFIX=$TMPDIR/split if [[ -n "$AUTOMV_DIR" ]]; then automvdir=$AUTOMV_DIR else if [[ -d "/usr/local/etc/automv.d" ]]; then automvdir=/usr/local/etc/automv.d else echo 'Missing automv.d location: please create confs in /usr/local/etc/automv.d or supply a directory in $AUTOMV_DIR' exit 1 fi fi function extract { local file=$1 local prop=$2 grep "^${prop}=.*$" $file | cut -d '=' -f2 } function automv { local query=$1 local source=$2 local target=$3 echo "Moving files from $source matching '$query' to $target" find $source -type f \( -regex "${query}" ! -iname ".*" \) \ | xargs -I '{}' mv -v {} $target/ } function split { file=$1 filename=$(basename --suffix=.conf $file) csplit $1 \ --prefix="${CSPLIT_PREFIX}-${filename}" \ --suffix-format='_%04d.conf' \ --elide-empty-files \ --suppress-matched \ --keep-files \ --silent \ /---/ '{*}' } echo "Executing automv functions from $automvdir" for file in $automvdir/*.conf do split $file done for file in $TMPDIR/*.conf do echo "found $file" if [[ -f $file ]]; then # Extract file configs query=$(extract $file "query") sourced=$(extract $file "source") target=$(extract $file "target") automv $query $sourced $target fi done echo "Automoved files" echo "Removing tmp dir: $TMPDIR" rm -r $TMPDIR echo "Done"