On CachyOS (and OMF-style setups) config.fish sources a distro file that defines fish_greeting while the shell starts. A function defined that way beats autoloading from functions/, so the installed greeting never ran. install.fish now appends a guarded block to ~/.config/fish/config.fish that sources the greeting after everything else (idempotent via a marker, skipped with --no-config), and the README documents the symptom, the check (functions --details fish_greeting) and the manual one-liner. Verified in a sandbox HOME that reproduces the CachyOS setup: before the install fish resolves the distro file and prints its message, after it resolves ~/.config/fish/functions/fish_greeting.fish and runs ours; a second install leaves a single block. Applied to this machine's real config as well, where fish_greeting now resolves to the user function and the __j621_fetch_random helper is loaded.
179 lines
6.3 KiB
Fish
Executable File
179 lines
6.3 KiB
Fish
Executable File
#!/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 <api-token>
|
|
# fish install.fish --no-prompt # never ask, keep/derive defaults
|
|
# fish install.fish --force # rewrite an existing config
|
|
# fish install.fish --no-config # do not touch ~/.config/fish/config.fish
|
|
|
|
argparse 'url=' 'token=' 'no-prompt' 'force' 'no-config' '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"
|
|
|
|
# --- Override distro/OMF greetings -------------------------------------------
|
|
# Some setups (CachyOS, OMF themes, hand-rolled configs) define fish_greeting
|
|
# inline while config.fish is being read. A function defined that way wins over
|
|
# autoloading, so our file would never load. Source it from the end of
|
|
# config.fish to define ours last.
|
|
set -l marker "# >>> J621 greeting >>>"
|
|
set -l user_config ~/.config/fish/config.fish
|
|
if set -q _flag_no_config
|
|
echo "Skipping $user_config (--no-config)"
|
|
else if test -f $user_config; and grep -qF "$marker" $user_config
|
|
echo "Config already sources the greeting (marker present)"
|
|
else
|
|
mkdir -p (dirname $user_config)
|
|
begin
|
|
echo ""
|
|
echo "$marker"
|
|
echo "# Loaded after the distro config so it wins over an inline fish_greeting."
|
|
echo "if test -f $functions_dir/fish_greeting.fish"
|
|
echo " source $functions_dir/fish_greeting.fish"
|
|
echo "end"
|
|
echo "# <<< J621 greeting <<<"
|
|
end >> $user_config
|
|
echo "Added the J621 greeting to $user_config (removes any distro greeting override)"
|
|
end
|
|
|
|
# --- 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
|