#!/usr/bin/env bash set -Eeo pipefail ############# # Globals # ############# CMD="/usr/bin/ani-cli" CFG_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/aniwrapper" CFG_FILE="$CFG_DIR/themes/aniwrapper.rasi" DEFAULT_DOWNLOAD="$HOME/Videos/sauce" ROFI_THEME="aniwrapper.rasi" THEMES="aniwrapper (default)|dracula|doomone|fancy|flamingo|material|nord|onedark" QUALITIES="1. best (default)|2. 1080p|3. 720p|4. 360p|5. worst" QUALITY=best DPI=96 GET_QUALITY=0 IS_CUSTOM_THEME=0 IS_DOWNLOAD=0 IS_PLAY_FROM_FILE=0 IS_ROFI=1 VERBOSE=0 SILENT=0 quit="6. Quit" options="1. Stream|2. Download|3. Continue|4. Play from File|5. Sync History|$quit" theme=default ############# # Functions # ############# help_text() { while IFS= read -r line; do printf "%s\n" "$line" done <<< " Usage: aniwrapper [-cdhvqS] [-t or -T ] aniwrapper [-cvS] [-t or -T ] -f aniwrapper -Q aniwrapper -C Options: -h show this help text -q enable quality selection -c enable command-line mode (rofi disabled) -d download episode in command-line mode -C connect to history database -S silent mode (suppress output to stdout) [cannot be used with -v] -t change rofi theme -T specify custom rofi theme -f (no trailing slash) specify starting directory for play for file mode -Q query the history database " } lg() { if ((VERBOSE == 1)); then printf "%s\n" "$*" fi } seppuku() { printf "%s\n" "$*" exit 1 } quit() { lg 'Quitting...' exit 0 } run() { if ((IS_PLAY_FROM_FILE == 0 && GET_QUALITY == 1)); then get_quality fi if ((SILENT == 1)); then if ((IS_CUSTOM_THEME == 0)); then "$CMD" -D"$DPI" -Sq "$QUALITY" -t "$theme" "$@" else "$CMD" -D"$DPI" -Sq "$QUALITY" -T "$CFG_FILE" "$@" fi elif ((VERBOSE == 1)); then if ((IS_CUSTOM_THEME == 0)); then "$CMD" -D"$DPI" -vq "$QUALITY" -t "$theme" "$@" else "$CMD" -D"$DPI" -vq "$QUALITY" -T "$CFG_FILE" "$@" fi else if ((IS_CUSTOM_THEME == 0)); then "$CMD" -D"$DPI" -q "$QUALITY" -t "$theme" "$@" else "$CMD" -D"$DPI" -q "$QUALITY" -T "$CFG_FILE" "$@" fi fi } get_quality() { if ((IS_ROFI == 1)); then selection=$(rofi -dpi "$DPI" -dmenu -config "$CFG_FILE" \ -l 5 -selected-row 0 \ -theme-str 'listview {columns: 1;}' -p "Choose video quality:" \ -sep '|' -only-match <<< "$QUALITIES") QUALITY=$(awk '{print $2}' <<< "$selection") else printf "%s" "Enter quality [$QUALITIES]: " read -r QUALITY fi lg "selected quality: $QUALITY" } # generates a span mesg for rofi given # input: message: str generate_span() { msg="$*" span="$msg" printf "%s\n" "$span" } parse_args() { while getopts 'vhqcdf:-:t:T:CQ:D:S' OPT; do case "$OPT" in h) help_text exit 0 ;; v) VERBOSE=1 ;; q) GET_QUALITY=1 lg "Quality prompt enabled" ;; c) IS_ROFI=0 lg "Command-line (ani-cli) mode set" ;; d) IS_DOWNLOAD=1 lg "Download flag set..." ;; f) IS_PLAY_FROM_FILE=1 play_path="$OPTARG" lg "Play from file flag set... skipping main menu" lg "PLAY_PATH: $play_path" ;; t) theme="$OPTARG" lg "custom theme provided: $theme" case "$theme" in aniwrapper) lg "Default theme chosen... doing nothing" theme=default ;; dracula) ROFI_THEME=aniwrapper-dracula.rasi ;; doomone | doom-one) ROFI_THEME=aniwrapper-doomone.rasi ;; fancy) ROFI_THEME=aniwrapper-fancy.rasi ;; flamingo) ROFI_THEME=aniwrapper-flamingo.rasi ;; material) ROFI_THEME=aniwrapper-material.rasi ;; nord) ROFI_THEME=aniwrapper-nord.rasi ;; onedark) ROFI_THEME=aniwrapper-onedark.rasi ;; *) seppuku "$theme not a valid theme file. Themes: [$THEMES]" ;; esac CFG_FILE="$CFG_DIR/themes/$ROFI_THEME" ;; C) lg "Connecting to history database -> $CFG_DIR/history.sqlite3" sqlite3 "$CFG_DIR/history.sqlite3" exit $? ;; Q) query="$OPTARG" lg "DATABASE QUERY: $query" sqlite3 -line "$CFG_DIR/history.sqlite3" "$query" exit $? ;; T) CFG_FILE="$OPTARG" [ ! -f "$CFG_FILE" ] && seppuku "config file $CFG_FILE does not exist" IS_CUSTOM_THEME=1 ;; D) DPI="$OPTARG" ;; S) SILENT=1 ;; *) help_text exit 1 ;; esac done } check_flags() { # Check if command-line flag is set if ((VERBOSE == 1 && SILENT == 1)); then seppuku "verbose and silent options cannot be used together" fi if ((IS_ROFI == 0 && IS_DOWNLOAD == 0)); then run -c exit $? elif ((IS_ROFI == 0 && IS_DOWNLOAD == 1)); then printf "%s" "Enter download dir: " read -r dl_dir lg "Download dir: $dl_dir" if [ ! -d "$dl_dir" ]; then mkdir -p "$dl_dir" || seppuku "Error creating directory: $dl_dir" fi run "-cd $dl_dir" exit $? elif ((IS_ROFI == 1 && IS_PLAY_FROM_FILE == 1 && IS_CUSTOM_THEME == 1)); then run "-f$play_path" -T "$CFG_FILE" exit $? elif ((IS_ROFI == 1 && IS_PLAY_FROM_FILE == 1 && IS_CUSTOM_THEME == 0)); then run "-f$play_path" -t "$theme" exit $? fi } ######## # Main # ######## main() { choice=$(echo "${options[@]}" | rofi -dpi "$DPI" -dmenu -only-match -sep '|' \ -config "$CFG_FILE" -l 6 -i -p "Aniwrapper") [ "$choice" == "$quit" ] && quit selection=$(printf "%s\n" "$choice" | awk '{ print $1 }') case "$selection" in 1.) # --------------------------------------------------------------------------- # streaming # --------------------------------------------------------------------------- lg "Streaming mode" run ;; 2.) # --------------------------------------------------------------------------- # download # --------------------------------------------------------------------------- lg "Download anime" dl_dir=$( rofi -dpi "$DPI" -dmenu -config "$CFG_FILE" \ -mesg "$(generate_span "Enter the path to the download directory, or leave blank to go with the default: $HOME/Videos/sauce/")" \ -l 1 -p "Enter download dir:" ) # if dl_dir is none set to current directory [ "$dl_dir" == "" ] && dl_dir="$DEFAULT_DOWNLOAD" run -d "$dl_dir" ;; 3.) # --------------------------------------------------------------------------- # continue # --------------------------------------------------------------------------- lg "Continue watching" run -H ;; 4.) # --------------------------------------------------------------------------- # play # --------------------------------------------------------------------------- lg "Play from file selected" IS_PLAY_FROM_FILE=1 span=$(printf '%s\n%s\n' "$(generate_span "Provide a valid path to a directory or leave blank to go with the default: $HOME/Videos/sauce/")" "$(generate_span "The program will begin searching for media files from the supplied directory")") play_dir=$(rofi -dpi "$DPI" -dmenu -config "$CFG_FILE" \ -l 1 -mesg "$span" -p "Enter path to starting directory:") if [ -z "$play_dir" ]; then mkdir -p "$DEFAULT_DOWNLOAD" || seppuku "error creating default download directory" run -f"$DEFAULT_DOWNLOAD" else run -f"$play_dir" fi exit $? ;; 5.) lg "Sync history database" roficmd="rofi -dpi $DPI -dmenu -config $CFG_FILE -l 0 -p" username=$($roficmd "Enter the username of the remote user:") [ -z "$username" ] && seppuku "No username provided... exiting" host=$($roficmd "Enter the host for the remote machine (eg 192.168.1.99):") [ -z "$host" ] && seppuku "No host provided... exiting" port=$($roficmd "Enter in the ssh port for remote machine or leave blank for default [22]:") [ -z "$port" ] && port=22 keypath=$($roficmd "Enter path to private key (leave blank if not needed or if unsure):") if ! printf "%s\n%s\n%d\n%s\n" "$username" "$host" "$port" "$keypath" | run -s; then lg "Aniwrapper was unable to sync the databases..." exit 1 else lg "Databases synced successfully" quit fi ;; 6.) # --------------------------------------------------------------------------- # get out # --------------------------------------------------------------------------- quit ;; *) help_text ;; esac } parse_args "$@" lg "CONFIG DIR: $CFG_DIR" lg "ROFI CFG: $CFG_FILE" check_flags main