diff --git a/update.sh b/update.sh index c97e89c..59ce467 100755 --- a/update.sh +++ b/update.sh @@ -3,14 +3,16 @@ set -e cd "$(dirname "$0")" +export PATH="$HOME/.bun/bin:/usr/local/go/bin:$HOME/go/bin:$PATH" + SERVICE_FILE="$HOME/.config/systemd/user/webterm.service" DEFAULT_USERNAME="izackp" DEFAULT_TTL_SECONDS="86400" UPDATE_AUTH_PASSWORD=0 +INSTALLING=0 if [ ! -f "$SERVICE_FILE" ]; then - echo "Missing service file: $SERVICE_FILE" >&2 - exit 1 + INSTALLING=1 fi while getopts ":p" opt; do @@ -26,6 +28,11 @@ while getopts ":p" opt; do done shift $((OPTIND - 1)) +if [ "$INSTALLING" -eq 1 ]; then + echo "No service file found — running first-time install." + UPDATE_AUTH_PASSWORD=1 +fi + if [ "$UPDATE_AUTH_PASSWORD" -eq 1 ]; then echo "Enter password for webterm login." read -rsp "Password for $DEFAULT_USERNAME: " WEBTERM_PASSWORD @@ -36,6 +43,39 @@ if [ "$UPDATE_AUTH_PASSWORD" -eq 1 ]; then fi fi +if [ "$INSTALLING" -eq 1 ]; then + mkdir -p "$(dirname "$SERVICE_FILE")" + LOG_DIR="$HOME/.webterm" + mkdir -p "$LOG_DIR" + BINARY_PATH="$HOME/go/bin/webterm" + WORK_DIR="$(pwd)" + cat > "$SERVICE_FILE" </dev/null | grep -q "Linger=yes"; then + echo "Enabling linger for $USER so the service starts at boot without login..." + loginctl enable-linger "$USER" + fi +fi + CURRENT_SECRET="$(sed -n 's/^Environment=WEBTERM_AUTH_COOKIE_SECRET=//p' "$SERVICE_FILE" | tail -n 1)" if [ -z "$CURRENT_SECRET" ]; then CURRENT_SECRET="$(openssl rand -hex 32)" @@ -110,8 +150,13 @@ mv "$tmp_target" ~/go/bin/webterm echo "Reloading user systemd config..." systemctl --user daemon-reload -echo "Restarting service..." -systemctl --user restart webterm.service +if [ "$INSTALLING" -eq 1 ]; then + echo "Enabling and starting service..." + systemctl --user enable --now webterm.service +else + echo "Restarting service..." + systemctl --user restart webterm.service +fi echo "Done. Status:" systemctl --user status webterm.service --no-pager