#!/bin/bash
#set -Eeuo pipefail


$WYZ_DIR/bash/base_echo.sh "start" "$0"

install_advance_touch() {
  pipx install advance-touch &&
  ln -s /root/.local/share/pipx/venvs/advance-touch/bin/ad /usr/bin/ad
}

install_ansible() {
  pipx install ansible &&
  [ ! -e /etc/ansible/ansible.cfg ] && curl -o /etc/ansible/ansible.cfg https://yzhub.cn/config/ansible.conf
}


install_bat() {
  apt install -y bat &&
  [ ! -h /usr/local/bin/bat ] && ln -s $(which batcat) /usr/local/bin/bat
}


install_docker() {
  curl -fsSL get.docker.com -o get-docker.sh &&
  apt update &&
  sh get-docker.sh --mirror AzureChinaCloud &&
  curl -o /etc/docker/daemon.json   https://yzhub.cn/daemon.json &&
  systemctl restart docker &&
  rm ./get-docker.sh &&
  rm /etc/apt/sources.list.d/docker.list
}


install_docker-compose() {
  curl -SL https://yzhub.cn/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose &&
  chmod -R 777 /usr/local/bin/docker-compose
}


install_fd-find() {
  apt install -y fd-find &&
  [ ! -h /usr/local/bin/fd ] && ln -s $(which fdfind) /usr/local/bin/fd
}


install_fish() {
  apt install -y fish &&
  sed -i '1s/bash/fish/g' /etc/passwd &&
  [ ! -e ~/.config ]                  && mkdir ~/.config &&
  [ ! -e ~/.config/fish ]             && mkdir ~/.config/fish &&
  [ ! -e ~/.config/fish/config.fish ] && curl -o ~/.config/fish/config.fish https://yzhub.cn/config/fish.conf && source ~/.config/fish/config.fish
}


install_k9s() {
  curl -sS https://webinstall.dev/k9s | bash &&
  ln -s ~/.local/bin/k9s /usr/local/bin/k9s &&
  rm -rf Downloads
}


install_minio() {
  curl -o /usr/local/bin/minio https://dl.min.io/client/mc/release/linux-amd64/mc
  chmod +x /usr/local/bin/minio
  minio alias set wyz https://minio.home.yzhub.cn onepiecewyz zhongyao
  # minio alias set wyz http://localhost:31131 onepiecewyz zhongyao
  # minio cp --recursive ./test.txt wyz/share
}


install_mise() {
  bash ./install_mise.sh
  sed -i 's/^#mise activate fish | source/mise activate fish | source/' ~/.config/fish/config.fish
  curl -o ~/.config/mise/config.toml https://yzhub.cn/config/mise.conf
}


install_nexttrace() {
  curl -o /usr/local/bin/nexttrace https://yzhub.cn/nexttrace &&
  chmod -R 777 /usr/local/bin/nexttrace
}


install_nginx() {
  apt install curl vim wget gnupg dpkg apt-transport-https lsb-release ca-certificates -y &&
  curl -sSL https://n.wtf/public.key | gpg --dearmor > /usr/share/keyrings/n.wtf.gpg &&
  echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/n.wtf.gpg] https://mirror-cdn.xtom.com/sb/nginx/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/n.wtf.list &&
  apt update &&
  apt install nginx-extras -y &&
  rm /etc/apt/sources.list.d/n.wtf.list
}


install_pipx() {
  pip install pipx
}


install_pueue() {
  curl -o /usr/local/bin/pueue https://yzhub.cn/pueue && chmod 777 /usr/local/bin/pueue
  curl -o /usr/local/bin/pueued https://yzhub.cn/pueued && chmod 777 /usr/local/bin/pueued
  [ ! -e ~/.config/pueue ]          && mkdir ~/.config/pueue
  curl -o ~/.config/pueue/pueue.yml  https://yzhub.cn/config/pueue.conf
  curl -o /etc/systemd/user/pueued.service https://yzhub.cn/config/pueued.service
  systemctl --user enable pueued && systemctl --user restart pueued
  pueued --daemonize
}


install_sealos() {
  #wget https://github.com/labring/sealos/releases/download/v5.0.0-beta4/sealos_5.0.0-beta4_linux_amd64.deb
  curl -o /root/sealos.deb https://yzhub.cn/sealos_5.0.0-beta4_linux_amd64.deb
  apt install /root/sealos.deb
  rm /root/sealos.deb
}

install_snap() {
  apt install -y snap snapd
}


install_ssh() {
  apt install -y ssh &&
  [ ! -e "$(which ssh)" ] && systemctl daemon-reload && systemctl enable ssh && systemctl restart ssh
  [   -e "$(which ssh)" ] && sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config
}


install_tmux() {
  apt install -y tmux xclip &&
  [ ! -e ~/.tmux.conf ] && curl -o ~/.tmux.conf https://yzhub.cn/config/tmux.conf
  [ ! -e ~/.tmux.conf ] && tmux source-file ~/.tmux.conf
}


install_usql() {
  curl -o /usr/local/bin/usql https://yzhub.cn/usql
  chmod -R 777 /usr/local/bin/usql
}


install_vim() {
  apt install -y vim &&
  [ ! -e ~/.vimrc ] && curl -o ~/.vimrc https://yzhub.cn/config/vim.conf
}


install_zerotier() {
  #客户端安装zerotier，并加入网络b6079f73c6659eb0
  curl -o ./zerotier.sh https://install.zerotier.com  &&
  bash ./zerotier.sh &&
  zerotier-cli join b6079f73c6659eb0 &&
  rm ./zerotier.sh &&
  rm /etc/apt/sources.list.d/zerotier.list

  #客户端添加wyz-nb作为moon节点
  mkdir /var/lib/zerotier-one/moons.d
  cd /var/lib/zerotier-one/moons.d
  sshpass -p 'ZHONG2563yao' rsync -aq root@110.42.99.98:/var/lib/zerotier-one/moons.d/00000014b48b804e.moon /var/lib/zerotier-one/moons.d/00000014b48b804e.moon
  zerotier-cli orbit 14b48b804e
  systemctl restart zerotier-one
  sleep 1
  zerotier-cli listpeers
  zerotier-cli listmoons
}


install_utils_by_mise() {
  #apt install autoconf gettext -y && mise install git -y
  #mise install bun -y
  #mise install deno -y
  #mise install erlang -y
  mise install go -y              #go,gofmt 
  mise install java -y            
  mise install node@16 -y         #corepack,node,npm,npx
  mise install node -y            #corepack,node,npm,npx
  mise install pnpm -y            #pnpm,pnpx
  #mise install python@pypy -y
  mise install python -y          #python3,pip3,idle3,pydoc3,python3-config
  #mise install python@miniconda3
  #mise install python@anaconda3
  #mise install ruby -y
  mise install rust -y            #cargo
  #mise install yarn -y            #yarn
}


get_version() {
  local pkg=$1
  local pkg_short=$2
  case $pkg in
    curl)                                
      $pkg_short --version 2>&1 | head -n 1 | awk '{print $1, $2}';;
    dnsutils|nginx)
      $pkg_short -v 2>&1 | head -n 1;;
    fd-find|inetutils-ping|ssh|sshpass|tmux|traceroute)
      $pkg_short -V 2>&1 | head -n 1;;
    k9s)
      $pkg_short version 2>&1 | grep 'Version:';;
    *)
      $pkg_short --version 2>&1 | head -n 1;;
  esac
}


get_short() {
  local pkg=$1
  case $pkg in
    advance-touch)     echo "ad"           2>&1;;
    bottom)            echo "btm"          2>&1;;
    dnsutils)          echo "dig"          2>&1;;
    fd-find)           echo "fd"           2>&1;;
    httpie)            echo "http"         2>&1;;
    inetutils-ping)    echo "ping"         2>&1;;
    net-tools)         echo "netstat"      2>&1;;
    ripgrep)           echo "rg"           2>&1;;
    silversearcher-ag) echo "ag"           2>&1;;
    systemd)           echo "systemctl"    2>&1;;
    zerotier)          echo "zerotier-cli" 2>&1;;
    *)                 echo "$pkg"         2>&1;;
  esac
}


start_install() {
  echo "$pkg is installing"
  local pkg=$1
  case $pkg in
    advance-touch)        install_advance_touch;;
    ansible)              install_ansible;;
    bat)                  install_bat;;
    docker)               install_docker;;
    docker-compose)       install_docker-compose;;
    fd-find)              install_fd-find;;
    fish)                 install_fish;;
    k9s)                  install_k9s;;
    minio)                install_minio;;
    mise)                 install_mise;;
    nexttrace)            install_nexttrace;;
    nginx)                install_nginx;;
    pueue)                install_pueue;;
    sealos)               install_sealos;;
    ssh)                  install_ssh;;
    tmux)                 install_tmux;;
    usql)                 install_usql;;
    vim)                  install_vim;;
    zerotier)             install_zerotier;;
    iredis|yt-dlp)pipx    install "$pkg";;
    bottom|deno|mcfly)    cargo install "$pkg";;
    jump)                 install_snap; snap install "$pkg";;
    *)                    apt install -y "$pkg";;
  esac
  echo "$(printf '%*s' 60)" | tr ' ' '-'
}


# 定义多个软件包列表
declare -A package_lists
package_lists[base]="ssh vim git curl systemd ripgrep fd-find rsync net-tools inetutils-ping fish nginx mise sd tmux pueue ufw"
package_lists[network]="nmap telnet dnsutils traceroute zerotier httpie nexttrace"
package_lists[remote]="sshpass smbclient minio"
package_lists[utils]="bat duf ncdu aptitude tig git-extras ranger cloc jump glances snap usql"
package_lists[docker]="docker docker-compose"
package_lists[k8s]="k9s sealos"
package_lists[rust]="bottom deno mcfly"
package_lists[python]="pipx ansible advance-touch iredis yt-dlp"
package_lists[deprecate]="silversearcher-ag octave"

# 默认列表
# 调用./install.sh --list "base dev"
# 调用./install.sh n
default_list="base network utils docker python"

# 解析命令行参数
if [[ "$1" == "--list" ]]; then
  if [[ -n "$2" ]]; then
    selected_list=$2
  else
    echo "Available lists:"
    for list in "${!package_lists[@]}"; do
      echo "  $list"
    done
    exit 0
  fi
elif [[ -n "$1" ]]; then
  # 如果提供了参数但不是 --list，则认为是单个包
  packages=("$1")
else
  selected_list=$default_list
fi

# 如果没有直接指定包，则构建要安装的包列表
if [[ -z ${packages+x} ]]; then
  packages=()
  for list in $selected_list; do
    if [[ -n "${package_lists[$list]}" ]]; then
      packages+=(${package_lists[$list]})
    else
      echo "Warning: Unknown list '$list', skipping."
    fi
  done

  # 如果没有选择任何有效的列表,使用默认列表
  if [ ${#packages[@]} -eq 0 ]; then
    echo "No valid lists selected, using default list."
    packages=(${package_lists[$default_list]})
  fi
fi

counter=1
counter_all=${#packages[@]}

# 逐一检查软件包并安装
for pkg in "${packages[@]}"; do
  printf " %02d/%02d " $counter $counter_all
  pkg_short=$(get_short "$pkg")
  if command -v "$pkg_short" &> /dev/null; then
    version=$(get_version "$pkg" "$pkg_short")
    echo "Already installed $pkg ($version)"
  else
    start_install "$pkg"
  fi

  ((counter++))
done
