fhnw-tools/fhnw-sync

133 lines
3.2 KiB
Bash
Executable File

#!/usr/bin/env sh
set -o errexit
: "${XDG_CONFIG_HOME:="$HOME/.config"}"
config_file="$XDG_CONFIG_HOME/fhnw-sync/config"
config_read=false
set_defaults() {
ad_mount_dir="/mnt/fhnw-share"
ad_dest_dir="$HOME/documents/fhnw/ad"
ad_excluded_files=".DS_Store|Thumbs.db"
ad_sources=""
teams_dest_dir="$HOME/documents/fhnw/teams"
teams_sources=""
pass_path="accounts/fhnw/students.fhnw.ch"
}
read_config() {
if [ -f "$config_file" ]; then
# shellcheck disable=1090
. "$config_file"
fi
}
display_help() {
set_defaults
echo "Usage: $0 [option...] " >&2
echo
echo " -c, config file [default: $config_file]"
echo " -m, mount directory for the ad [default: $ad_mount_dir]"
echo " -d, root destination directory [default: $ad_dest_dir]"
echo " -e, list of excluded files in ad sync (pipe separated) [default: $ad_excluded_files]"
echo " -s, list of ad directories, relative to the mount directory that will be synced (pipe separated)"
echo " -r, root destination directory for teams sync [default: $teams_dest_dir]"
echo " -t, list of configured rclone teams sources (pipe separated)"
echo " -p, path for pass to get user and password information [default: $pass_path]"
echo " -h, display this help and exit"
echo
echo "If a config file exists, it is sourced by this script. The following keys are recognized:"
echo "- ad_mount_dir -> m"
echo "- ad_dest_dir -> d"
echo "- ad_excluded_files -> e"
echo "- ad_sources -> s"
echo "- teams_dest_dir -> r"
echo "- teams_sources -> t"
echo "- pass_path -> p"
echo
echo "Values from a config file get overwriten by cli args."
echo
}
parse_args() {
while getopts ":hc:m:d:e:s:r:t:p:" opt; do
case $opt in
h)
display_help
exit 0
;;
c)
if [ $config_read = false ]; then
config_file=$OPTARG
read_config
config_read=true
parse_args "$@"
fi
;;
m)
ad_mount_dir=$OPTARG
;;
d)
ad_dest_dir=$OPTARG
;;
e)
ad_excluded_files=$OPTARG
;;
s)
ad_sources=$OPTARG
;;
r)
teams_dest_dir=$OPTARG
;;
t)
teams_sources=$OPTARG
;;
p)
pass_path=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG" >&2
display_help
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
display_help
exit 1
;;
esac
done
}
cleanup() {
if mountpoint -q "$ad_mount_dir"; then
echo "unmounting ad share..."
sudo umount --quiet "$ad_mount_dir" || true
fi
if [ -f "$vpn_pid_file" ]; then
echo "stopping vpn..."
vpn_pid="$(sudo cat "$vpn_pid_file")"
sudo kill "$vpn_pid"
while ps -p "$vpn_pid" >/dev/null; do
sleep 1
done
fi
}
trap cleanup 0 HUP INT QUIT ABRT TERM
start_vpn() {
echo "starting vpn..."
vpn_pid_file="$(sudo mktemp)"
fhnw-vpn -m "bg" -t "$vpn_pid_file" -p "$pass_path"
}
set_defaults
read_config
parse_args "$@"
start_vpn
fhnw-ad-mount -m "$ad_mount_dir" -p "$pass_path"
fhnw-ad-sync -m "$ad_mount_dir" -d "$ad_dest_dir" -e "$ad_excluded_files" -s "$ad_sources"
fhnw-teams-sync -d "$teams_dest_dir" -s "$teams_sources"