#!/bin/sh
# Одноразово: passwordless sudo для joplin-hotspot-alias.sh (setmanual *.100, dhcp)
set -e

SCRIPT="$(CDPATH= cd -- "$(dirname "$0")" && pwd)/joplin-hotspot-alias.sh"
DEST="/etc/sudoers.d/joplin-hotspot-alias"
USER_NAME="$(whoami)"

[ -x "$SCRIPT" ] || chmod +x "$SCRIPT"

TMP="$(mktemp)"
trap 'rm -f "$TMP"' EXIT

cat >"$TMP" <<EOF
# Joplin WebDAV: primary *.100 на Wi-Fi (setmanual) + dhcp (только этот скрипт)
${USER_NAME} ALL=(ALL) NOPASSWD: ${SCRIPT}, ${SCRIPT} ensure, ${SCRIPT} ensure --force, ${SCRIPT} repair, ${SCRIPT} dhcp
EOF

echo "Будет создан $DEST:"
cat "$TMP"
echo ""
sudo cp "$TMP" "$DEST"
sudo chmod 440 "$DEST"
sudo visudo -c -f "$DEST"
echo "OK. LaunchAgent сможет выставлять *.100 без пароля."
