#! /bin/bash
while getopts :k:d: ARGS; do
  case $ARGS in
  k)
    get_local_key="${OPTARG}"
    ;;
  d)
    get_shell_Id="${OPTARG}"
    ;;
  *)
    echo "未知选项：$ARGS"
    ;;
  esac
done
if [[ -f /etc/redhat-release ]]; then
  release="centos"
elif cat /etc/issue | grep -Eqi "debian"; then
  release="debian"
elif cat /etc/issue | grep -Eqi "ubuntu"; then
  release="ubuntu"
elif cat /etc/issue | grep -Eqi "centos|red hat|redhat"; then
  release="centos"
elif cat /proc/version | grep -Eqi "debian"; then
  release="debian"
elif cat /proc/version | grep -Eqi "ubuntu"; then
  release="ubuntu"
elif cat /proc/version | grep -Eqi "centos|red hat|redhat"; then
  release="centos"
else
  echo -e "${red}未检测到系统版本，请联系脚本作者！${plain}\n" && exit 1
fi
if [ "$release" == "centos" ]; then
  yum -y install wget curl git unzip
else
  apt-get -y install wget curl git unzip
fi
if [ "${get_local_key}" != "" ] && [ "${get_shell_Id}" != "" ]; then
  #判断处理器类型
  if arch | grep -Eqi "aarch64"; then
    echo "获取最新版本号..."
    edition=$(curl -L -s "https://query.him.plus/empower.php?objective=get_edition")
    echo "下载最新脚本..."
    wget --user-agent="Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.3) Gecko/2008092416 Firefox/3.0.3" --no-check-certificate -N https://pan.him.plus/shell/docking/docking_ARM_$edition.sh
    echo "运行脚本..."
    chmod +x docking_ARM_$edition.sh && ./docking_ARM_$edition.sh -k $get_local_key -d $get_shell_Id
  elif arch | grep -Eqi "x86_64"; then
    echo "获取最新版本号..."
    edition=$(curl -L -s "https://query.him.plus/empower.php?objective=get_edition")
    echo "下载最新脚本..."
    wget --user-agent="Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.3) Gecko/2008092416 Firefox/3.0.3" --no-check-certificate -N https://pan.him.plus/shell/docking/docking_$edition.sh
    echo "运行脚本..."
    chmod +x docking_$edition.sh && ./docking_$edition.sh -k $get_local_key -d $get_shell_Id
  fi
elif [ "${get_local_key}" != "" ]; then
  if arch | grep -Eqi "aarch64"; then
    echo "获取最新版本号..."
    edition=$(curl -L -s "https://query.him.plus/empower.php?objective=get_edition")
    echo "下载最新脚本..."
    wget --user-agent="Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.3) Gecko/2008092416 Firefox/3.0.3" --no-check-certificate -N https://pan.him.plus/shell/docking/docking_ARM_$edition.sh
    echo "运行脚本..."
    chmod +x docking_ARM_$edition.sh && ./docking_ARM_$edition.sh -k $get_local_key
  elif arch | grep -Eqi "x86_64"; then
    echo "获取最新版本号..."
    edition=$(curl -L -s "https://query.him.plus/empower.php?objective=get_edition")
    echo "下载最新脚本..."
    wget --user-agent="Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.3) Gecko/2008092416 Firefox/3.0.3" --no-check-certificate -N https://pan.him.plus/shell/docking/docking_$edition.sh
    echo "运行脚本..."
    chmod +x docking_$edition.sh && ./docking_$edition.sh -k $get_local_key
  fi
elif [ "${get_shell_Id}" != "" ]; then
  ehco -e "${red}必须携带 -k 参数！"
else
  #判断处理器类型
  if arch | grep -Eqi "aarch64"; then
    echo "获取最新版本号..."
    edition=$(curl -L -s "https://query.him.plus/empower.php?objective=get_edition")
    echo "下载最新脚本..."
    wget --user-agent="Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.3) Gecko/2008092416 Firefox/3.0.3" --no-check-certificate -N https://pan.him.plus/shell/docking/docking_ARM_$edition.sh
    echo "运行脚本..."
    chmod +x docking_ARM_$edition.sh && ./docking_ARM_$edition.sh
  elif arch | grep -Eqi "x86_64"; then
    echo "获取最新版本号..."
    edition=$(curl -L -s "https://query.him.plus/empower.php?objective=get_edition")
    echo "下载最新脚本..."
    wget --user-agent="Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.3) Gecko/2008092416 Firefox/3.0.3" --no-check-certificate -N https://pan.him.plus/shell/docking/docking_$edition.sh
    echo "运行脚本..."
    chmod +x docking_$edition.sh && ./docking_$edition.sh
  fi
fi
