add new opts for service files

This commit is contained in:
Rowan 2025-03-07 12:10:00 -06:00
parent a901f34a9e
commit 439580c5cf

View file

@ -10,13 +10,47 @@ MOUNT_POINT="$XDG_CONFIG_HOME"
usage() { usage() {
SCRIPT_NAME=$(basename $0) SCRIPT_NAME=$(basename $0)
echo "Usage: $SCRIPT_NAME <changeset_name> [unmount]" echo "Usage: $SCRIPT_NAME [OPTIONS] <changeset_name> [unmount]"
exit 1 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'
} }
if [ $# -lt 1 ]; then subpath() {
usage parent_path="$1"
fi 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 ;;
* ) break ;;
esac
done
SELECTED="$1" SELECTED="$1"
CHANGESET="$CHANGESET_DIR/$SELECTED" CHANGESET="$CHANGESET_DIR/$SELECTED"
@ -43,31 +77,55 @@ if mountpoint -q "$MOUNT_POINT"; then
fusermount -u "$MOUNT_POINT" fusermount -u "$MOUNT_POINT"
fi fi
SYSD_DIR="$XDG_CONFIG_HOME/systemd/user"
SVC_NAME="$SELECTED-automount.service" SVC_NAME="$SELECTED-automount.service"
SVC_FILE="$SVC_PATH/$SVC_NAME"
TMP=$(mktemp) TMP=$(mktemp)
mkdir -p "$SYSD_DIR" mkdir -p "$SVC_PATH"
cat << EOF > "$TMP" cat << EOF > "$TMP"
[Unit] [Unit]
Description=Mount fuse-overlayfs config Description=Mount fuse-overlayfs config $SELECTED
[Service] [Service]
Type=oneshot Type=oneshot
RemainAfterExit=yes RemainAfterExit=yes
ExecStart=fuse-overlayfs -o lowerdir="$BASE_CONFIG:$CHANGESET",upperdir="$UPPER_DIR",workdir="$WORKDIR" "$MOUNT_POINT" ExecStart=fuse-overlayfs -o lowerdir="$BASE_CONFIG:$CHANGESET",upperdir="$UPPER_DIR",workdir="$WORKDIR" "$MOUNT_POINT"
ExecStop=/usr/bin/fusermount -u $MOUNT_POINT ExecStop=/usr/bin/fusermount -zu $MOUNT_POINT
[Install] [Install]
WantedBy=default.target WantedBy=default.target
EOF EOF
diff "$TMP" "$SYSD_DIR/$SVC_NAME" > /dev/null 2>&1 diff "$TMP" "$SVC_FILE" > /dev/null 2>&1
HAS_DIFF=$? HAS_DIFF=$?
PROMPT=0 PROMPT=0
IS_HIDDEN=0
if [ "$HAS_DIFF" -gt 0 ]; then if [ "$HAS_DIFF" -gt 0 ]; then
mv "$TMP" "$SYSD_DIR/$SVC_NAME" 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 PROMPT=1
fi
fi fi
mkdir -p "$WORKDIR" mkdir -p "$WORKDIR"
@ -80,4 +138,3 @@ if [ "$PROMPT" -eq 1 ]; then
echo "Run 'systemctl --user enable --now $SVC_NAME' to automount this config" echo "Run 'systemctl --user enable --now $SVC_NAME' to automount this config"
fi fi