annotate script
This commit is contained in:
parent
698993349c
commit
cf77f36a70
|
@ -1,15 +1,19 @@
|
||||||
#!/usr/bin/with-contenv bash
|
#!/usr/bin/with-contenv bash
|
||||||
# shellcheck shell=bash
|
# shellcheck shell=bash
|
||||||
|
|
||||||
|
# Check if inotify exists. If not, install it.
|
||||||
if ! command -v inotifywait 2>&1 >/dev/null; then
|
if ! command -v inotifywait 2>&1 >/dev/null; then
|
||||||
echo "No inotifywait found. install inotify tools"
|
echo "No inotifywait found. install inotify tools"
|
||||||
apk add inotify-tools
|
apk add inotify-tools
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "running inotify"
|
# define stuff that should be excluded some watches.
|
||||||
|
|
||||||
BASEEXCLUDE='.*\.swp|.*\.swx'
|
BASEEXCLUDE='.*\.swp|.*\.swx'
|
||||||
|
|
||||||
|
# the main function where all of the magic happens. takes 4 arguments:
|
||||||
|
# - the folder to watch
|
||||||
|
# - stuff to exclude
|
||||||
|
# - two arguments to replace things in the paths coming from inotify, to turn them into a path nextcloud understands.
|
||||||
detect(){
|
detect(){
|
||||||
WATCH=$1
|
WATCH=$1
|
||||||
EXCLUDE=$2
|
EXCLUDE=$2
|
||||||
|
@ -17,30 +21,41 @@ detect(){
|
||||||
REPLACEWITH=$4
|
REPLACEWITH=$4
|
||||||
|
|
||||||
echo "watching $WATCH and excluding $EXCLUDE"
|
echo "watching $WATCH and excluding $EXCLUDE"
|
||||||
|
|
||||||
|
# this uses inotifywait to watch for changes
|
||||||
|
# then it runs the code in the while block to handle file paths it spits out
|
||||||
inotifywait -mre close_write,delete --format '%e|%w|%f' --exclude "$EXCLUDE" "$WATCH" | while read RAWEVENT
|
inotifywait -mre close_write,delete --format '%e|%w|%f' --exclude "$EXCLUDE" "$WATCH" | while read RAWEVENT
|
||||||
do
|
do
|
||||||
|
# inotifywait spits out the events like "eventtype|/directory|filename"
|
||||||
echo $RAWEVENT
|
# here we split them into it's three parts.
|
||||||
IFS='|'
|
IFS='|'
|
||||||
read -a SPLITEVENT <<< "$RAWEVENT"
|
read -a SPLITEVENT <<< "$RAWEVENT"
|
||||||
|
|
||||||
EVENT=${SPLITEVENT[0]}
|
EVENT=${SPLITEVENT[0]}
|
||||||
DIR=${SPLITEVENT[1]}
|
DIR=${SPLITEVENT[1]}
|
||||||
FILE=${SPLITEVENT[2]}
|
FILE=${SPLITEVENT[2]}
|
||||||
|
|
||||||
|
# by default, scan the whole directory
|
||||||
|
# we can't scan a file that's been deleted
|
||||||
REPLACEDFILE=$DIR
|
REPLACEDFILE=$DIR
|
||||||
|
|
||||||
|
# if the event wasn't a delete, we append the filename
|
||||||
|
# so we only scan the file that was changed.
|
||||||
if [[ $EVENT != 'DELETE' ]]; then
|
if [[ $EVENT != 'DELETE' ]]; then
|
||||||
REPLACEDFILE+=$FILE
|
REPLACEDFILE+=$FILE
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# replace the beginning of the paths to the filename.
|
||||||
|
# inotify spits out the filesystem path
|
||||||
|
# nextcloud expects the path relative to the user directory
|
||||||
if [ -n "$REPLACEIN" ] && [ -n "$REPLACEWITH" ]; then
|
if [ -n "$REPLACEIN" ] && [ -n "$REPLACEWITH" ]; then
|
||||||
REPLACEDFILE=${REPLACEDFILE/$REPLACEIN/$REPLACEWITH}
|
REPLACEDFILE=${REPLACEDFILE/$REPLACEIN/$REPLACEWITH}
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# actually run the OCC command
|
||||||
occ files:scan --path=\"$REPLACEDFILE\" --shallow
|
occ files:scan --path=\"$REPLACEDFILE\" --shallow
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# run the scan function on the folders I want scanned
|
||||||
detect /data/riksolo/files $BASEEXCLUDE /data/riksolo /riksolo
|
detect /data/riksolo/files $BASEEXCLUDE /data/riksolo /riksolo
|
Loading…
Reference in New Issue