Files
ttyverse-extensions/manage-extensions.sh
2025-07-29 11:29:27 -04:00

288 lines
9.8 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# TTYverse Extension Manager
# Simple XDG-compliant extension management for ~/.local/share/ttyverse/extensions/
#
# XDG data directory for TTYverse
DATA_DIR="${XDG_DATA_HOME:-$HOME/.local/share}/ttyverse"
EXT_DIR="$DATA_DIR/extensions"
BUNDLED_DIR="$(dirname "$0")"
# Colors for output (only if stdout is a terminal)
if [[ -t 1 ]]; then
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
else
GREEN=''
YELLOW=''
RED=''
NC=''
fi
# Ensure extension directory exists
mkdir -p "$EXT_DIR"
show_help() {
cat << EOF
TTYverse Extension Manager
Usage: $0 <command> [extension_name]
Commands:
list Show available and enabled extensions
enable <ext> Enable an extension (symlink to XDG extensions directory)
disable <ext> Disable an extension (remove from XDG extensions directory)
status <ext> Show status of specific extension
help Show this help
Examples:
$0 list # Show all extensions
$0 enable soundpack # Enable soundpack extension
$0 disable soundpack # Disable soundpack extension
$0 status soundpack # Check if soundpack is enabled
XDG Extensions Directory: $EXT_DIR
Bundled Extensions: $BUNDLED_DIR
Note: TTYverse only loads extensions from the XDG directory.
Use this script to enable/disable bundled extensions or install your own.
EOF
}
list_extensions() {
echo -e "${GREEN}Available Extensions:${NC}"
for ext in "$BUNDLED_DIR"/*.pl; do
if [[ -f "$ext" ]]; then
basename="${ext##*/}"
name="${basename%.pl}"
if [[ -L "$EXT_DIR/$basename" || -f "$EXT_DIR/$basename" ]]; then
echo -e " ${GREEN}${NC} $name (enabled)"
else
echo -e " ${YELLOW}${NC} $name (available)"
fi
fi
done
echo -e "\n${GREEN}User Extensions:${NC}"
for ext in "$EXT_DIR"/*.pl; do
if [[ -f "$ext" && ! -L "$ext" ]]; then
basename="${ext##*/}"
name="${basename%.pl}"
echo -e " ${GREEN}${NC} $name (user-installed)"
fi
done
}
enable_extension() {
local name="$1"
local ext_file="$name.pl"
local bundled_path="$BUNDLED_DIR/$ext_file"
local user_path="$EXT_DIR/$ext_file"
if [[ ! -f "$bundled_path" ]]; then
echo -e "${RED}Error:${NC} Extension '$name' not found in bundled extensions"
echo "Available extensions:"
for ext in "$BUNDLED_DIR"/*.pl; do
if [[ -f "$ext" ]]; then
basename="${ext##*/}"
echo " ${basename%.pl}"
fi
done
return 1
fi
if [[ -L "$user_path" ]]; then
echo -e "${YELLOW}Warning:${NC} Extension '$name' is already enabled"
return 0
elif [[ -f "$user_path" ]]; then
echo -e "${YELLOW}Warning:${NC} User-installed extension '$name' already exists"
return 0
fi
ln -s "$(realpath "$bundled_path")" "$user_path"
echo -e "${GREEN}${NC} Enabled extension: $name"
echo " Linked: $bundled_path -> $user_path"
# Special handling for soundpack extension - copy default sounds and configure RC
if [[ "$name" == "soundpack" ]]; then
local sounds_src="$BUNDLED_DIR/sounds"
local sounds_dst="$DATA_DIR/sounds"
local config_dir="${XDG_CONFIG_HOME:-$HOME/.config}/ttyverse"
local rc_file="$config_dir/ttyverserc"
# Copy sound files
if [[ -d "$sounds_src" ]]; then
echo " Copying bundled sound files..."
cp -r "$sounds_src"/* "$sounds_dst/" 2>/dev/null || true
echo -e "${GREEN}${NC} Copied default sound pack to $sounds_dst/"
fi
# Auto-configure RC file
mkdir -p "$config_dir"
local needs_config=false
# Check if soundpack extension is already configured
# Need to check for exts, notifytype, and notifies all being properly set
local has_exts=false
local has_notifytype=false
local has_notifies=false
if [[ -f "$rc_file" ]]; then
grep -q "^exts.*soundpack" "$rc_file" 2>/dev/null && has_exts=true
grep -q "^notifytype=.*soundpack" "$rc_file" 2>/dev/null && has_notifytype=true
grep -q "^notifies=.*mention.*boost.*favourite" "$rc_file" 2>/dev/null && has_notifies=true
fi
if [[ "$has_exts" == "false" ]] || [[ "$has_notifytype" == "false" ]] || [[ "$has_notifies" == "false" ]]; then
needs_config=true
fi
if [[ "$needs_config" == "true" ]]; then
echo " Configuring TTYverse for sound notifications..."
# Create backup if RC file exists
[[ -f "$rc_file" ]] && cp "$rc_file" "$rc_file.backup.$(date +%s)"
# Update or create exts line
if [[ -f "$rc_file" ]] && grep -q "^exts=" "$rc_file"; then
# Check if soundpack is already in exts line
if ! grep -q "^exts=.*soundpack" "$rc_file"; then
sed -i 's/^exts=\(.*\)/exts=\1,soundpack/' "$rc_file"
echo " Added soundpack to existing exts line"
fi
else
# Add new exts line
echo "exts=soundpack" >> "$rc_file"
echo " Added exts=soundpack"
fi
# Add or update notifytype
if [[ -f "$rc_file" ]] && grep -q "^notifytype=" "$rc_file"; then
sed -i 's/^notifytype=.*/notifytype=soundpack/' "$rc_file"
echo " Updated notifytype to soundpack"
else
echo "notifytype=soundpack" >> "$rc_file"
echo " Added notifytype=soundpack"
fi
# Add or update notifies parameter for sound events
if [[ -f "$rc_file" ]] && grep -q "^notifies=" "$rc_file"; then
# Update existing notifies line to include fediverse sound types
sed -i 's/^notifies=.*/notifies=default,mention,dm,me,follow,boost,favourite/' "$rc_file"
echo " Updated existing notifies configuration"
else
# Add new notifies line
echo "notifies=default,mention,dm,me,follow,boost,favourite" >> "$rc_file"
echo " Added notifies configuration"
fi
# Add extension preferences
{
echo ""
echo "# Sound notifications (added by extension manager)"
echo "extpref_sound_command=paplay"
echo "extpref_soundpack=default"
echo ""
echo "# Fediverse sound types: default, mention, dm, me, follow, boost, favourite, poll, announcement"
} >> "$rc_file"
echo -e "${GREEN}${NC} Configured TTYverse RC file for sound notifications"
echo " Restart TTYverse to enable sounds automatically"
else
echo -e "${YELLOW}Note:${NC} Soundpack already configured in RC file"
fi
fi
}
disable_extension() {
local name="$1"
local ext_file="$name.pl"
local user_path="$EXT_DIR/$ext_file"
if [[ ! -e "$user_path" ]]; then
echo -e "${YELLOW}Warning:${NC} Extension '$name' is not enabled"
return 0
fi
if [[ -L "$user_path" ]]; then
rm "$user_path"
echo -e "${GREEN}${NC} Disabled extension: $name"
# Special handling for soundpack - offer to remove from RC file
if [[ "$name" == "soundpack" ]]; then
local config_dir="${XDG_CONFIG_HOME:-$HOME/.config}/ttyverse"
local rc_file="$config_dir/ttyverserc"
if [[ -f "$rc_file" ]] && grep -q "soundpack" "$rc_file"; then
echo -e "${YELLOW}Note:${NC} Soundpack is still configured in $rc_file"
echo " You may want to remove the soundpack lines to fully disable"
echo " Or run TTYverse without -exts=soundpack to override"
fi
fi
elif [[ -f "$user_path" ]]; then
echo -e "${YELLOW}Warning:${NC} '$name' is a user-installed extension (not a symlink)"
echo "Remove manually if needed: $user_path"
fi
}
show_status() {
local name="$1"
local ext_file="$name.pl"
local bundled_path="$BUNDLED_DIR/$ext_file"
local user_path="$EXT_DIR/$ext_file"
echo "Extension: $name"
echo "Bundled: $([[ -f "$bundled_path" ]] && echo "Available" || echo "Not found")"
if [[ -L "$user_path" ]]; then
echo -e "Status: ${GREEN}Enabled${NC} (symlinked)"
echo "Target: $(readlink "$user_path")"
elif [[ -f "$user_path" ]]; then
echo -e "Status: ${GREEN}Enabled${NC} (user-installed)"
else
echo -e "Status: ${YELLOW}Disabled${NC}"
fi
}
# Main command handling
case "${1:-help}" in
list|ls)
list_extensions
;;
enable)
if [[ -z "$2" ]]; then
echo -e "${RED}Error:${NC} Extension name required"
echo "Usage: $0 enable <extension_name>"
exit 1
fi
enable_extension "$2"
;;
disable)
if [[ -z "$2" ]]; then
echo -e "${RED}Error:${NC} Extension name required"
echo "Usage: $0 disable <extension_name>"
exit 1
fi
disable_extension "$2"
;;
status)
if [[ -z "$2" ]]; then
echo -e "${RED}Error:${NC} Extension name required"
echo "Usage: $0 status <extension_name>"
exit 1
fi
show_status "$2"
;;
help|--help|-h)
show_help
;;
*)
echo -e "${RED}Error:${NC} Unknown command: $1"
echo "Run '$0 help' for usage information"
exit 1
;;
esac