143 lines
3.7 KiB
Bash
Executable file
143 lines
3.7 KiB
Bash
Executable file
#!/usr/bin/env sh
|
|
|
|
XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}"
|
|
XDG_STATE_HOME="${XDG_STATE_HOME:-$HOME/.local/state}"
|
|
XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"
|
|
|
|
BASE_CONFIG="$XDG_DATA_HOME/config/base"
|
|
CHANGESET_DIR="$XDG_DATA_HOME/config/changeset"
|
|
MOUNT_POINT="$XDG_CONFIG_HOME"
|
|
|
|
usage() {
|
|
SCRIPT_NAME=$(basename $0)
|
|
echo "Usage: $SCRIPT_NAME [OPTIONS] <changeset_name> [unmount]"
|
|
echo
|
|
echo 'options'
|
|
echo ' -a, --automount automatically enable the changeset service file'
|
|
echo ' -f, --force ovewrite existing service file'
|
|
echo ' -s, --service-path the path to write the service file'
|
|
}
|
|
|
|
subpath() {
|
|
parent_path="$1"
|
|
sub_path="$2"
|
|
|
|
# Normalize paths (remove trailing slashes, handle relative paths, etc.)
|
|
parent_path=$(cd "$parent_path" && pwd)
|
|
sub_path=$(cd "$sub_path" && pwd)
|
|
|
|
# Add trailing slash to parent_path if it doesn't have one
|
|
[[ "$parent_path" != */ ]] && parent_path+="/"
|
|
|
|
# Check if sub_path starts with parent_path
|
|
if [[ "$sub_path" == "$parent_path"* ]]; then
|
|
return 0 # is a subpath
|
|
else
|
|
return 1 # is not a subpath
|
|
fi
|
|
}
|
|
|
|
AUTOMOUNT=0
|
|
FORCE=0
|
|
SVC_PATH="$XDG_CONFIG_HOME/systemd/user"
|
|
while true; do
|
|
case "$1" in
|
|
-h | --help | -\? ) usage; exit ;;
|
|
-a | --automount ) AUTOMOUNT=1; shift ;;
|
|
-f | --force ) FORCE=1; shift ;;
|
|
-s | --service-path ) SVC_PATH="$2"; shift 2 ;;
|
|
\? | -\? ) echo "Unknown option: $1"; exit 1 ;;
|
|
-- ) shift; break ;;
|
|
-* ) echo "ERROR: Unknown option: '$1'"; exit 1 ;;
|
|
* ) break ;;
|
|
esac
|
|
done
|
|
|
|
shift "$(($OPTIND - 1))"
|
|
|
|
SELECTED="$1"
|
|
CHANGESET="$CHANGESET_DIR/$SELECTED"
|
|
UPPER_DIR="$XDG_STATE_HOME/config/$SELECTED"
|
|
WORKDIR="$XDG_STATE_HOME/config/.work_$SELECTED"
|
|
|
|
if [ ! -d "$CHANGESET" ]; then
|
|
echo "'$CHANGESET' not found."
|
|
exit 1
|
|
fi
|
|
|
|
if [ ! -d "$UPPER_DIR" ]; then
|
|
mkdir -p $UPPER_DIR
|
|
fi
|
|
|
|
if [ "$2" == "unmount" ]; then
|
|
fusermount -zu "$MOUNT_POINT"
|
|
echo "Unmounted $MOUNT_POINT"
|
|
exit 0
|
|
fi
|
|
|
|
# Unmount if already mounted.
|
|
if mountpoint -q "$MOUNT_POINT"; then
|
|
fusermount -u "$MOUNT_POINT"
|
|
fi
|
|
|
|
SVC_NAME="$SELECTED-automount.service"
|
|
SVC_FILE="$SVC_PATH/$SVC_NAME"
|
|
TMP=$(mktemp)
|
|
mkdir -p "$SVC_PATH"
|
|
cat << EOF > "$TMP"
|
|
[Unit]
|
|
Description=Mount fuse-overlayfs config $SELECTED
|
|
|
|
[Service]
|
|
Type=oneshot
|
|
RemainAfterExit=yes
|
|
ExecStart=fuse-overlayfs -o lowerdir="$BASE_CONFIG:$CHANGESET",upperdir="$UPPER_DIR",workdir="$WORKDIR" "$MOUNT_POINT"
|
|
ExecStop=/usr/bin/fusermount -zu $MOUNT_POINT
|
|
|
|
[Install]
|
|
WantedBy=default.target
|
|
EOF
|
|
|
|
diff "$TMP" "$SVC_FILE" > /dev/null 2>&1
|
|
|
|
HAS_DIFF=$?
|
|
PROMPT=0
|
|
IS_HIDDEN=0
|
|
if [ "$HAS_DIFF" -gt 0 ]; then
|
|
if subpath "$MOUNT_POINT" "$SVC_PATH"; then
|
|
IS_HIDDEN=1
|
|
echo "WARN: writing a service file to target mountpoint"
|
|
echo "'$SVC_PATH' is a subpath of '$MOUNT_POINT'"
|
|
echo "this won't break anything, but it will be hidden once '$SELECTED' is mounted"
|
|
echo "this means you won't be able to enable it. you may run this command with -a"
|
|
echo "to automatically mount it instead"
|
|
echo
|
|
fi
|
|
|
|
if [ "$HAS_DIFF" -eq 2 ]; then
|
|
mv "$TMP" "$SVC_FILE"
|
|
elif [ "$HAS_DIFF" -eq 1 ] && [ "$FORCE" -eq 1 ]; then
|
|
mv "$TMP" "$SVC_FILE"
|
|
elif [ "$HAS_DIFF" -eq 1 ] && [ "$FORCE" -eq 0 ]; then
|
|
echo "INFO: Changes detected in existing service file but refusing to ovewrite."
|
|
echo "rerun with -f or --force to overwrite"
|
|
echo
|
|
fi
|
|
|
|
if [ "$AUTOMOUNT" -eq 1 ]; then
|
|
systemctl --user enable --now "$SVC_FILE"
|
|
else
|
|
PROMPT=1
|
|
fi
|
|
fi
|
|
|
|
mkdir -p "$WORKDIR"
|
|
|
|
fuse-overlayfs -o lowerdir="$BASE_CONFIG:$CHANGESET",upperdir="$UPPER_DIR",workdir="$WORKDIR" "$MOUNT_POINT"
|
|
|
|
echo "Mounted $MOUNT_POINT with changeset '$SELECTED'"
|
|
|
|
if [ "$PROMPT" -eq 1 ]; then
|
|
echo "Run 'systemctl --user enable --now $SVC_NAME' to automount this config"
|
|
fi
|
|
|