#Usage: weapon-url-opener-nightly(1) installerContribute on GitHub

#!/usr/bin/env bash #

luisadha.github.io/weapon-url-opener-nightly Use this self-hosted script to install your weapon-url-opener-nightly.

### INSTALL

. <(curl -L https://luisadha.github.io/weapon-url-opener-nightly) 

### CODE


# `weapon-url-opener-nightly(1)` installer script that fetches the canonical `setup.sh` script and runs it
# in the calling context.
# Author: luisadha (c) 2025
# Sangat cepat rata-rata 24 ms.
VERSION="v2.3.27"

## 📝 Changelog - v2.2.24 (2025-06-23)
### Patch v2.2.24
# Bug fixeds
  
# - Finished monitoring anomaly case file lang
### Patch v2.2.8
# - Fix bug when missing files lang.auto
# - Unfinish monitoring file lang

### ✨ New Features
# - Revamped the UI for a more intuitive and cleaner experience.
# - Added automatic language detection for English and Indonesian.
# - Added support for file previews using `bat` with syntax highlighting (without line numbers).

### 🛠️ Fixes
# - Fixed a bug where nothing was selected and no valid fallback was triggered.
# - Improved **In Use** label behavior for better context awareness and clarity.

DEBUG=0
lang=""
BASE="$HOME/.local/state/wuo/"
CACHE="$HOME/.local/state/wuo/lang"
STATE="$HOME/.local/state/wuo/lang.auto"
touch "$CACHE"
mkdir -p "$BASE"
debug_log() {  
  if [ "$DEBUG" -eq 1 ]; then  
    echo -ne "[DEBUG] " "$*" >&2;
  fi 
}
normal_log() {
  if [ "$DEBUG" -eq 0 ]; then 
    echo -ne "$*"
  fi 
}


# 🔍 Deteksi opsi
if [[ $# -eq 0 ]]; then
  touch ~/.local/state/wuo/lang.default; 
  lang="default" #en # Agar eksekusi cepat
fi

for arg in "$@"; do
  case "$arg" in
    --help)
cat <<'EOF'
WUO-Nightly  Switch downloader scripts easily and quickly. Now you can use the termux-url-opener feature more often.
Usage :
        --debug              Print debug/verbose         
        --install-plugin     Install the termux-url-opener add-on required for this tool to work
        --lang=[auto/en/id]  Change languages
        --version            Print version
        --help               Print this message
EOF
  exit 0;
      ;;
    --debug)
      DEBUG=1
      ;;
    --version)
      echo "weapon-url-opener-nightly $VERSION"
      exit 0;
      ;;
    --install-plugin)
      echo -e "You will install 2 plugin for (Fist-installations) this will be added to ~/bin after installation are complete";
      curl -sSL https://raw.githubusercontent.com/luisadha/weapon-url-opener/refs/heads/master/install-plugin.sh | bash
      ;;
    --lang=*)
      LANG_ARG="${arg#--lang=}"
      ;;
  esac
done

# Atur lang setelah parsing
if [[ "$LANG_ARG" == "auto" ]]; then
  termux-tts-engines | yq -o t | cut -f1,3 --complement | xargs | awk '{print $2}' > "$CACHE"
  touch "$STATE"
  lang="auto"
elif [[ "$LANG_ARG" == "id" ]]; then
  echo "Pengenalan" > "$CACHE"
  lang="id"
elif [[ "$LANG_ARG" == "en" ]]; then
  echo "Speech" > "$CACHE"
  lang="en"
elif [[ -n "$LANG_ARG" ]]; then
  echo "undefined"
  exit 3
fi

# Jika lang tidak ditentukan tapi debug aktif, fallback
if [[ -z "$lang" && $DEBUG -eq 1 ]]; then
  lang="default"
  touch "$BASE/lang.default"
fi
  

if [[ -z "$lang" && -w "$STATE" && -w "${BASE}/lang.default" ]]; then 
  case "$(cat "$CACHE")" in
    Pengenalan) 
                lang="id" ;;
    Speech)     
                lang="en" ;;
    "")
                lang="" ;;
  esac
fi
debug_log "Detection language: $lang\n"

# Map Bahasa Indonesia
declare -A map_id=(
  [choosed_n]='Tidak memilih'
  [info_c]='Keluar dari skrip ini'
  [info_e]='Keluar'
  [banner]='WUO adalah alat penyedia termux-url-opener'
  [prompt_s]='Pilih senjata utamamu:'
  [toogle_u]='Digunakan'
  [choosed_y]='Kamu memilih'
  [info_s]='Sebagai senjata utamamu'
)

# Map Bahasa Inggris
declare -A map_en=(
  [choosed_n]='Not choosed yet'
  [info_c]='Program close'
  [info_e]='Exit'
  [banner]='WUO is termux-url-opener provider tools'
  [prompt_s]='Choose your main weapon:'
  [toogle_u]='In Use:'
  [choosed_y]='You choosed'
  [info_s]='as your primary weapon'
)

# Fungsi untuk ambil string berdasarkan key
trmap() {
  locale="$(cat "$CACHE")";
  case "$locale" in
    Pengenalan) lang="id" ;;
    Speech)     lang="en" ;;
    *)          lang="en" ;;  # fallback default
   esac
debug_log "Query detected: ${locale:="(system detection)"}\n"

  case "$lang" in
    id) echo "${map_id[$1]}" ;;
    en) echo "${map_en[$1]}" ;;
    *) echo "Unknown language" ;;
  esac
  debug_log "Mapped language: $lang\n"
}
update_system() {
  pip install -U yt-dlp;
};
  
# Masuk ke folder koleksi
debug_log "Entering dir: " && pushd ~/bin &>/dev/null || exit
# Tambahkan dekripsi untuk opsi Exit
update_system > ~/bin/AUTO_UPDATE_YT-DLP
trmap info_c > ~/bin/EXIT
__banner__="$(trmap banner)"

# Hitung skrip provider
#count_plugin=$(ls ~/bin/*.sh| wc -l)
# Hitung hash dari termux-url-opener
ref_hash=$(md5sum ~/bin/termux-url-opener 2>/dev/null | awk '{print $1}')

# Ambil semua file .sh dalam urutan waktu
IFS=$'\n' read -d '' -r -a file_list < <(\ls -Art ~/bin/*.sh 2>/dev/null || :)

# Ambil hanya nama file-nya
IFS=$'\n' read -d '' -r -a file_list < <(printf "%s\n" "${file_list[@]}" | xargs -n1 basename 2>/dev/null )

# Tambahkan Opsi EXIT di akhir

file_list+=("AUTO_UPDATE_YT-DLP")
file_list+=("EXIT")
  
# Cari file yang sedang 'in use' (hash sama dengan termux-url-opener)
toggle=""
for file in "${file_list[@]}"; do
  [[ "$file" == "EXIT" ]] && continue
  [[ "$file" == "AUTO_UPDATE_YT-DLP" ]] && continue

  file_hash=$(md5sum ~/bin/"$file" 2>/dev/null | awk '{print $1}')
  if [[ "$file_hash" == "$ref_hash" ]]; then
    toggle="${file%.*}";
    break
  fi
done
# Siapkan header jika ada file yang in use
if [[ -n "$toggle" ]]; then
  fzf_header="$__banner__"
  current="$toggle"
else
  fzf_header="$__banner__"
  current="$toggle"
fi
# Cache biar cepat
c_y="$(trmap choosed_y )"; 
i_s="$(trmap info_s)";
inuse="$(trmap toogle_u)";
pr="$(trmap prompt_s)";
no_input="$(trmap choosed_n)";
eof="$(trmap info_e)";

# Tampilan
weapon=$(printf '%s\n' "${file_list[@]}" | while read -r f; do
    base="${f%.*}"
    printf "%s\t%s\n" "$f" "$base"
  done | fzf --prompt="$pr " \
  --height=50% \
  --layout=reverse \
  --border \
  --info=inline-right \
  --pointer='👉' \
  --ellipsis='+'\
  --color=border:cyan \
  --no-separator \
  --header-lines=0 \
  --input-border='sharp' \
  --header="$fzf_header" \
  --header-border="bold" \
  --list-border="vertical" \
  --header-first \
  --input-label="[ $inuse $current ]"\
  --highlight-line \
  --preview='bat --color=always --style=plain --paging=never --theme OneHalfDark {1}' \
  --with-nth=2 \
  --delimiter=$'\t' \
  --exit-0 | cut -f1) 

# Jika tidak memilih apa-apa, keluar
if [[ "$weapon" == "" ]]; then
  echo "$no_input"
  exit 2
fi
# Jika pilih EXIT
if [[ "$weapon" == "AUTO_UPDATE_YT-DLP" ]]; then
  exit 0
fi;
  
if [[ "$weapon" == "EXIT" ]]; then
  echo "$eof"
  exit 1
fi
rep="$(basename "$weapon")";
# Jalankan aksi
normal_log "$c_y" "$rep" "$i_s"
debug_log "Your Reply: $weapon\n"
sleep 0.1;
cp -f ~/bin/"$weapon" ~/bin/termux-url-opener
start=$(date +%s%N)
debug_log "Leaving dir.. " && cd "$OLDPWD" && printf "\n" # kode yang ingin diukur
end=$(date +%s%N)
#DEBUG 
debug_log "Time Elapsed: $(( (end - start)/1000000 )) ms\n"
#