#!/usr/bin/env fish # Install the J621 fish greeting: # * copies fish_greeting.fish into ~/.config/fish/functions/ # * writes ~/.config/j621Greeting/config.fish (asks for the API origin and an # optional token, or takes them as flags) # * checks the tools it needs and probes the configured backend # # Usage: # fish install.fish # fish install.fish --url https://j621.example.ts.net --token # fish install.fish --no-prompt # never ask, keep/derive defaults # fish install.fish --force # rewrite an existing config argparse 'url=' 'token=' 'no-prompt' 'force' 'help' -- $argv or begin echo "Try: fish install.fish --help" exit 1 end if set -q _flag_help sed -n '2,12p' (status --current-filename) | sed 's/^# \{0,1\}//' exit 0 end set -l here (path resolve (dirname (status --current-filename))) set -l functions_dir ~/.config/fish/functions set -l config_dir ~/.config/j621Greeting set -l config_file $config_dir/config.fish set -l default_url https://j621.rainbow-herring.ts.net set -l url $default_url set -l token "" set -l problems 0 if set -q _flag_url set url $_flag_url end if set -q _flag_token set token $_flag_token end # --- Install the function ----------------------------------------------------- mkdir -p $functions_dir cp "$here/fish_greeting.fish" "$functions_dir/fish_greeting.fish" echo "Installed $functions_dir/fish_greeting.fish" # --- Configuration ------------------------------------------------------------ mkdir -p $config_dir set -l write_config 0 if not test -f $config_file set write_config 1 else if set -q _flag_force; or set -q _flag_url set write_config 1 else echo "Keeping existing $config_file (use --force to rewrite it)" # Read the current origin back so the probe below uses it. set -l current (grep -E '^set -g J621_BASE ' $config_file 2>/dev/null | head -1 | string replace -r '^set -g J621_BASE +' '') test -n "$current"; and set url $current end if test $write_config -eq 1 if not set -q _flag_no_prompt if not set -q _flag_url read -P "API origin [$default_url]: " answer test -n "$answer"; and set url $answer end if not set -q _flag_token read -P "API token (optional, Enter to run as a guest): " answer test -n "$answer"; and set token $answer end end set url (string trim --right --chars=/ "$url") printf '# Written by install.fish on %s\n' (date '+%Y-%m-%d') > $config_file printf 'set -g J621_BASE %s\n' "$url" >> $config_file if test -n "$token" printf 'set -g J621_TOKEN %s\n' "$token" >> $config_file else printf '# set -g J621_TOKEN paste-a-token-here\n' >> $config_file end printf '# set -g J621_WEB %s\n' "$url" >> $config_file printf '# set -g J621_FASTFETCH_CONFIG jake\n' >> $config_file chmod 600 $config_file echo "Wrote $config_file" end # --- Dependencies ------------------------------------------------------------- for tool in fastfetch file if not command -v $tool >/dev/null 2>&1 set problems 1 echo "Missing required tool: $tool" end end if not command -v curl >/dev/null 2>&1; and not command -v wget >/dev/null 2>&1 set problems 1 echo "Missing required tool: curl (or wget)" end for tool in gifsicle jq python3 if not command -v $tool >/dev/null 2>&1 echo "Optional tool not found: $tool (the greeting still works)" end end # --- Probe the configured backend -------------------------------------------- echo echo "Checking $url ..." set -l health "" if command -v curl >/dev/null 2>&1 set health (curl -s -m 10 "$url/health" | string collect) else set health (wget -qO- -T 10 "$url/health" | string collect) end if string match -q '*"status":"ok"*' "$health" echo " backend: ok" set -l random_args -s -m 10 if test -n "$token" set -a random_args -H "Authorization: Token $token" end set -l probe "" if command -v curl >/dev/null 2>&1 set probe (curl $random_args "$url/api/random/?fastfetch=1" | string collect) else set probe (wget -qO- -T 10 "$url/api/random/?fastfetch=1" | string collect) end set -l detail (printf '%s' "$probe" | grep -o '"detail"[[:space:]]*:[[:space:]]*"[^"]*"' | head -1 | sed -E 's/.*:[[:space:]]*"//; s/"$//') if test -n "$detail" echo " random: $detail" if test -z "$token" echo " (a token would also let you see items that are hidden from guests)" end else echo " random: ok" end else set problems 1 echo " backend did not answer at $url/health" echo " got: $health" echo " Fix J621_BASE in $config_file (or pass --url) and run again." end # --- Wrap up ------------------------------------------------------------------ echo if test $problems -eq 0 echo "Done. Test it now with: fish_greeting" else echo "Finished with problems above; the greeting will report them too." end echo "Rating mode lives in ~/.config/fish/greeting_mode (0=NSFW, 1=SFW, 2=Questionable)." echo "The old gm-switch tool / .desktop launchers keep working — same file." exit $problems