#!/bin/sh

version="1.0"
APP_URL='http://fw.koolcenter.com/binary/ddnsto/openwrt'
app_arm='ddnsto_arm.ipk'
app_aarch64='ddnsto_aarch64.ipk'
app_mips='ddnsto_mipsel.ipk'
app_x86='ddnsto_x86_64.ipk'
app_binary='ddnsto.ipk'
app_ui='luci-app-ddnsto.ipk'
app_lng='luci-i18n-ddnsto-zh-cn.ipk'

setup_color() {
    # Only use colors if connected to a terminal
    if [ -t 1 ]; then
        RED=$(printf '\033[31m')
        GREEN=$(printf '\033[32m')
        YELLOW=$(printf '\033[33m')
        BLUE=$(printf '\033[34m')
        BOLD=$(printf '\033[1m')
        RESET=$(printf '\033[m')
    else
        RED=""
        GREEN=""
        YELLOW=""
        BLUE=""
        BOLD=""
        RESET=""
    fi
}
setup_color
command_exists() {
    command -v "$@" >/dev/null 2>&1
}
error() {
    echo ${RED}"Error: $@"${RESET} >&2
}

TOKEN=""
while [ "$#" -gt 0 ]; do
  case "$1" in
    --token)
      if [ -n "$2" ]; then
        TOKEN=$2
        shift 2
      else
        error "--token requires a value"
        exit 1
      fi
      ;;
    --token=*)
      TOKEN=${1#--token=}
      shift
      ;;
    *)
      shift
      ;;
  esac
done

Download_Files(){
  local URL=$1
  local FileName=$2
  if command_exists curl; then
    curl -fsSLk ${URL} -o ${FileName}
  elif command_exists wget; then
    wget -q --no-check-certificate ${URL} -O ${FileName}
  else
    error "curl or wget is required"
    return 1
  fi
  if [ ! -s "${FileName}" ]; then
    error "download failed: ${URL}"
    return 1
  fi
}

remove_deprecated(){
  if [ -f /etc/config/ddnsto ] && grep -Eqi 'global' /etc/config/ddnsto; then
    rm -f /etc/config/ddnsto
  fi
}

clean_app(){
    rm -f /tmp/${app_binary} /tmp/${app_ui} /tmp/${app_lng}
}

fork_ugprade() {

cat <<\EOF >/tmp/.ddnsto-upgrade.sh
#!/bin/sh

      app_binary='ddnsto.ipk'
      app_ui='luci-app-ddnsto.ipk'
      app_lng='luci-i18n-ddnsto-zh-cn.ipk'
      opkg remove app-meta-ddnsto luci-i18n-ddnsto-zh-cn luci-app-ddnsto ddnsto
      opkg install /tmp/${app_binary}
      opkg install /tmp/${app_ui}
      opkg install /tmp/${app_lng}
      rm -f /tmp/${app_binary} /tmp/${app_ui} /tmp/${app_lng}
EOF

chmod 755 /tmp/.ddnsto-upgrade.sh

}

command_exists opkg || {
    error "The program only supports Openwrt."
    clean_app
    exit 1
}

if [ -f /www/luci-static/resources/luci.js ] && [ ! -f /usr/lib/lua/luci/cbi.lua ]; then
  opkg install luci-compat || true
fi

if echo `uname -m` | grep -Eqi 'x86_64'; then
    arch='x86_64'
    ( set -x; Download_Files ${APP_URL}/${app_x86} /tmp/${app_binary} || exit 1;
      Download_Files ${APP_URL}/${app_ui} /tmp/${app_ui} || exit 1;
      Download_Files ${APP_URL}/${app_lng} /tmp/${app_lng} || exit 1; ) || exit 1
elif  echo `uname -m` | grep -Eqi 'arm'; then
    arch='arm'
    ( set -x; Download_Files ${APP_URL}/${app_arm} /tmp/${app_binary} || exit 1;
      Download_Files ${APP_URL}/${app_ui} /tmp/${app_ui} || exit 1;
      Download_Files ${APP_URL}/${app_lng} /tmp/${app_lng} || exit 1; ) || exit 1
elif  echo `uname -m` | grep -Eqi 'aarch64'; then
    arch='aarch64'
    ( set -x; Download_Files ${APP_URL}/${app_aarch64} /tmp/${app_binary} || exit 1;
      Download_Files ${APP_URL}/${app_ui} /tmp/${app_ui} || exit 1;
      Download_Files ${APP_URL}/${app_lng} /tmp/${app_lng} || exit 1; ) || exit 1
elif  echo `uname -m` | grep -Eqi 'mipsel|mips'; then
    arch='mips'
    ( set -x; Download_Files ${APP_URL}/${app_mips} /tmp/${app_binary} || exit 1;
      Download_Files ${APP_URL}/${app_ui} /tmp/${app_ui} || exit 1;
      Download_Files ${APP_URL}/${app_lng} /tmp/${app_lng} || exit 1; ) || exit 1
else
    error "The program only supports Openwrt."
    exit 1
fi

rm -f /tmp/.ddnsto-upgrade.pid
remove_deprecated
fork_ugprade

if command_exists start-stop-daemon; then
  echo "fork to install, version is:"
  start-stop-daemon -S -b -q -m -x /tmp/.ddnsto-upgrade.sh -p /tmp/.ddnsto-upgrade.pid
  sleep 3
  if [ -f /tmp/.ddnsto-upgrade.pid ]; then

    PID=`cat /tmp/.ddnsto-upgrade.pid`
    if test -d /proc/"${PID}"/; then
      echo "waiting to finish"
      sleep 5
    fi

  fi
else
  echo "install, version is:"
  /tmp/.ddnsto-upgrade.sh
fi

if command_exists ddnsto; then
  VERSION=$(ddnsto -v 2>/dev/null || true)
  if [ -z "${VERSION}" ]; then
    error "cannot install ddnsto, empty version output"
    exit 1
  fi

printf "$GREEN"
cat <<-'EOF'
  ____  ____  _   _ ____ _____ ___
  |  _ \|  _ \| \ | / ___|_   _/ _ \
  | | | | | | |  \| \___ \ | || | | |
  | |_| | |_| | |\  |___) || || |_| |
  |____/|____/|_| \_|____/ |_| \___/   ....is now installed!


EOF
printf "$RESET"

  if [ -n "${TOKEN}" ]; then
    echo "${TOKEN} 已写入并启用服务，请登录 https://www.ddnsto.com/app 查看状态"
  else
    echo "感谢使用 DDNSTO，请登录 https://www.ddnsto.com 获取 token 填入插件"
  fi
  echo "如 luci 菜单未显示 DDNSTO，请刷新浏览器缓存或重新登录 luci 后台"
  echo "Version: ${VERSION}"

  if [ -n "${TOKEN}" ]; then
    if command_exists uci; then
      uci set ddnsto.@ddnsto[0].token="${TOKEN}" &&
      uci set ddnsto.@ddnsto[0].enabled='1' &&
      uci commit ddnsto &&
      /etc/init.d/ddnsto restart
    else
      error "uci not found, token not set"
    fi
  fi

else
  error "cannot install ddnsto, ddnsto not found"
fi
