#!/usr/bin/env sh
# shellcheck disable=SC2039
arch=$(uname -m)
if [[ $arch == "x86_64" ]]; then
  arch="amd64"
fi
if [[ $arch == "aarch64" ]]; then
  arch="arm64"
fi
if [[ $arch == "armv7l" ]]; then
  arch="arm32"
fi
if [[ $arch == "armv8l" ]]; then
	arch="arm64"
fi
if [[ $arch == "aarch" ]]; then
	arch="arm32"
fi
# shellcheck disable=SC2112
CreateStartShell() {
    mkdir -p /data/tt/bin
    cat > /data/tt/bin/start.sh <<EOF
#!/bin/bash
/data/tt/bin/ttmanager_${arch} -c /data/tt/node -t bynode &
ncat --sh-exec "ncat 127.0.0.1 22" -l 11294 --keep-open &
EOF
		chmod +x /data/tt/bin/start.sh
}
# shellcheck disable=SC2112
ConfigDns() {
    echo "nameserver 119.29.29.29" > /etc/resolv.conf
    echo "nameserver 180.76.76.76" >> /etc/resolv.conf
}
CheckNetwork() {
  echo "Checking Network Connection"
  ping baidu.com -c 5 > /dev/null
  # shellcheck disable=SC2181
  if [[ $? -ne 0 ]]; then
    echo "ping baidu.com failed"
    exit 1
  fi
  echo "ping baidu.com success"
}
InstallPython3() {
    which python3
    # shellcheck disable=SC2181
    if [[ $? -eq 0 ]]; then
      echo "Python3 Is Installed"
      return
    fi
    echo "Installing Python3"
    opkg update
    opkg install python3
    # shellcheck disable=SC2181
    if [[ $? -eq 0 ]]; then
      echo "Install Python3 success"
      return 0
    fi
    echo "Install Python3 failed"
    exit 1
}
InstallNcat() {
    which ncat
		# shellcheck disable=SC2181
		if [[ $? -eq 0 ]]; then
			echo "ncat Is Installed"
			return
		fi

    echo "Installing ncat"
    opkg update > /dev/null
    opkg install ncat
    # shellcheck disable=SC2181
    if [[ $? -eq 0 ]]; then
      echo "Install ncat success"
      return 0
    fi
    echo "Install ncat failed"
    exit 1
}

InstallJq() {
    which jq
		# shellcheck disable=SC2181
		if [[ $? -eq 0 ]]; then
			echo "jq Is Installed"
			return
		fi

    echo "Installing jq"
    opkg update > /dev/null
    opkg install jq
    # shellcheck disable=SC2181
    if [[ $? -eq 0 ]]; then
      echo "Install jq success"
      return 0
    fi
    echo "Install jq failed"
    exit 1
}

InstallIptables() {
		which iptables
		# shellcheck disable=SC2181
		if [[ $? -eq 0 ]]; then
			echo "iptables Is Installed"
			return
		fi

    echo "Installing iptables"
    opkg update > /dev/null
    opkg install iptables
    # shellcheck disable=SC2181
    if [[ $? -eq 0 ]]; then
      echo "Install iptables success"
      return 0
    fi
    echo "Install iptables failed"
}
InstallTt() {
    f=TTNODE.py
    rm -f $f
    wget  https://gitee.com/liumou_site/pvet/raw/master/model/tt/$f&&python3 $f -st -b bynode -p -service
}
EnableRcLocal() {
    echo "/data/tt/bin/start.sh" > /etc/rc.local
    echo "exit 0" >> /etc/rc.local
}

CleanCrontab(){
	touch clean.conf
	crontab clean.conf
	crontab clean.conf
	crontab clean.conf
	rm -f clean.conf
}

InstallLibmnl(){
	# 安装libmnl
	wget -c http://jmk7.liumou.site:11188/oecloud/libmnl-1.0.5.zip
	unzip libmnl-1.0.5.zip
	cd libmnl-1.0.5
	chmod +x libmnl-1.0.5/build-aux/install-sh
	mkdir -p /usr/local/libmnl-1.0.5/lib
	export GREP=/bin/grep
	./configure  --prefix=/usr/local/libmnl-1.0.5/  --disable-dependency-tracking
	make&&make install
}
InstallIproute2() {
	# 安装iproute2
	wget -c http://jmk7.liumou.site:11188/oecloud/iproute2-6.8.0.zip
	unzip iproute2-6.8.0.zip
	cd iproute2-6.8.0
	export PKG_CONFIG_PATH=/usr/local/libmnl-1.0.5/lib/pkgconfig/
	./configure --prefix=/usr/local/iproute2-6.8.0/ --with-openssl --with-privsep-path=/usr/local/iproute2-6.8.0/ --with-ethtool --with-ipset --with-iproute2 --with-iputils --with-libmnl --with-libnetfilter-conntrack --with-libnetfilter-queue
	make&&make install
}
InstallGcc() {
	# 安装gcc
	which gcc
	# shellcheck disable=SC2181
	if [[ $? -eq 0 ]]; then
		echo "gcc Is Installed"
		return
	fi
	echo "Installing gcc"
	opkg update > /dev/null
	opkg install gcc
	# shellcheck disable=SC2181
	if [[ $? -eq 0 ]]; then
		echo "Install gcc success"
		return 0
	fi
	echo "Install gcc failed"
}
InstallLibdbus(){
	which dbus-daemon
	# shellcheck disable=SC2181
	if [[ $? -eq 0 ]]; then
		echo "libdbus Is Installed"
		return
	fi
	echo "Installing libdbus"
	opkg update > /dev/null
	opkg install libdbus libdht libc libstdcpp6
	# shellcheck disable=SC2181
	if [[ $? -eq 0 ]]; then
		echo "Install libdbus success"
		return 0
	fi
	echo "Install libdbus failed"
}
InstallPppoeConf(){
	apt install -y pppoeconf pppoe
	if [[ $? -ne 0 ]]; then
		echo "Install pppoeconf pppoe failed"
		exit 1
	fi
	# 开始PPPOE拨号
	pppoeconf
	if [[ $? -ne 0 ]]; then
		echo "pppoeconf failed"
		exit 1
	fi
	echo "PPPOE dialing..."
	pppoe-start
	if [[ $? -ne 0 ]]; then
		echo "pppoe-start failed"
		exit 1
	fi
	while true; do
		sleep 1
		ping baidu.com -c 1 > /dev/null
		# shellcheck disable=SC2181
		if [[ $? -eq 0 ]]; then
			echo "PPPOE dialing success"
			break
		fi
	done
}
CreateStartShell # 创建启动脚本
ConfigDns # 配置DNS
CheckNetwork # 检查网络
InstallGcc # 安装gcc
InstallLibmnl # 安装libmnl
InstallIproute2 # 安装iproute2
InstallPython3 # 安装Python3
InstallTt # 安装TT
InstallPppoeConf # 安装PPPOE
InstallNcat # 安装Ncat
InstallJq # 安装Jq
InstallIptables # 安装iptables
EnableRcLocal # 配置rc.local
CleanCrontab # 清理定时任务