Skip to content

Instantly share code, notes, and snippets.

@badcrocodile
Last active October 2, 2023 17:27
Show Gist options
  • Save badcrocodile/d6c755d8409f037c5f14bf59aad2d85a to your computer and use it in GitHub Desktop.
Save badcrocodile/d6c755d8409f037c5f14bf59aad2d85a to your computer and use it in GitHub Desktop.
Monitor multiple source directories for changes and sync to given destination
#!/bin/bash
# Ensure fswatch is installed
if ! command -v fswatch &>/dev/null; then
echo "Error: fswatch is not installed. Please install it before running this script."
exit 1
fi
if [ $# -ne 1 ]; then
echo "Usage: $0 <2 letter country code>"
exit 1
fi
dest_name="$1"
source_dirs=()
dest_dirs=()
source_base="$HOME/Containers/globaltech/packages"
dest_base="$HOME/Containers/Fool-$dest_name/public/wp-content"
# Set default set of directories to monitor
plugins=(
'fool-wp-tracker'
'fool-wp-segment'
)
themes=(
'freesite-2020-theme'
'au-theme'
)
# Validate package exists
is_valid_package() {
local dir="$source_base/$1"
if [ -d "$dir" ]; then
echo "$dir"
else
echo "Error: '$dir' is not a valid package."
exit 1
fi
}
# Override default directories if options are passed
while getopts ":p:t:" opt; do
case "$opt" in
p)
IFS=',' read -ra plugin_dirs <<<"$OPTARG"
for dir in "${plugin_dirs[@]}"; do
package=$(is_valid_package "$dir")
plugins+=("$package")
done
;;
t)
IFS=',' read -ra theme_dirs <<<"$OPTARG"
for dir in "${theme_dirs[@]}"; do
package=$(is_valid_package "$dir")
themes+=("$package")
done
;;
\?)
echo "Usage: $0 [-p plugin1,plugin2,plugin3] [-t theme1,theme2]"
exit 1
;;
esac
done
shift $((OPTIND - 1))
# If only -p or -t was passed, empty the other array
if [ -n "${plugins[0]}" ] && [ -z "${themes[0]}" ]; then
themes=()
elif [ -z "${plugins[0]}" ] && [ -n "${themes[0]}" ]; then
plugins=()
fi
for plugin in "${plugins[@]}"; do
source_dirs+=("$source_base/$plugin/")
dest_dirs+=("$dest_base/plugins/$plugin/")
done
for theme in "${themes[@]}"; do
source_dirs+=("$source_base/$theme/")
dest_dirs+=("$dest_base/themes/$theme/")
done
exclude_patterns=('.git/' 'node_modules/' 'vendor/' 'logs/' '*.log' '.cache/' '*.cache' 'sql/' 'wordpress/' '~' '*.swp' '*.sw0' '4913' '.DS_Store')
for pattern in "${exclude_patterns[@]}"; do
exclude_args+=(--exclude="$pattern")
done
graceful_termination() {
# Add any cleanup tasks here if needed
echo "Received termination signal. Stopping monitoring..."
exit 0
}
trap graceful_termination SIGINT SIGTERM
sync_directories() {
local path_to_print="${1:-Default message}"
echo "$path_to_print synchronized"
rsync -avq --delete "${exclude_args[@]}" "$source_dir" "$dest_dir"
}
# Debounce implementation attempted but incomplete
for ((i = 0; i < ${#source_dirs[@]}; i++)); do
source_dir="${source_dirs[$i]}"
dest_dir="${dest_dirs[$i]}"
# last_sync_time=0
sync_directories "Initial sync for $source_dir"
fswatch --recursive --event Updated --event Created --event Removed "${exclude_args[@]}" "$source_dir" | while read -r event_path; do
# current_time=$(date +%s)
# if ((current_time - last_sync_time >= 1)); then
sync_directories "$event_path"
# last_sync_time=$current_time
# fi
done &
done
while true; do
sleep 60
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment