#!/usr/bin/env bash

unknown_os ()
{
  echo "Sorry, your operating system distribution and version are not supported by this script."
  echo
  echo "Please call 15927672143 or email wangxiong@dxpark.cn and let us know if you run into any issues."
  exit 1
}

detect_os ()
{
  if [[ ( -z "${os}" ) && ( -z "${dist}" ) ]]; then
    if [ -e /etc/os-release ]; then
      . /etc/os-release
      os=${ID}
      if [ "${os}" = "poky" ]; then
        dist=`echo ${VERSION_ID}`
      elif [ "${os}" = "sles" ]; then
        dist=`echo ${VERSION_ID}`
      elif [ "${os}" = "opensuse" ]; then
        dist=`echo ${VERSION_ID}`
      elif [ "${os}" = "opensuse-leap" ]; then
        os=opensuse
        dist=`echo ${VERSION_ID}`
      else
        dist=`echo ${VERSION_ID} | awk -F '.' '{ print $1 }'`
      fi
    elif [ -e /etc/lsb-release ]; then
      . /etc/lsb-release
      if [ "${ID}" = "raspbian" ]; then
        os=${ID}
        dist=`cut --delimiter='.' -f1 /etc/debian_version`
      else
        os=${DISTRIB_ID}
        dist=${DISTRIB_CODENAME}

        if [ -z "$dist" ]; then
          dist=${DISTRIB_RELEASE}
        fi
      fi
    elif [ `which lsb_release 2>/dev/null` ]; then
      # get major version (e.g. '5' or '6')
      dist=`lsb_release -r | cut -f2 | awk -F '.' '{ print $1 }'`

      # get os (e.g. 'centos', 'redhatenterpriseserver', etc)
      os=`lsb_release -i | cut -f2 | awk '{ print tolower($1) }'`
	  
    elif [ -e /etc/redhat-release ]; then
      os_hint=`cat /etc/redhat-release  | awk '{ print tolower($1) }'`
      if [ "${os_hint}" = "centos" ]; then
        dist=`cat /etc/redhat-release | awk '{ print $3 }' | awk -F '.' '{ print $1 }'`
        os='centos'
      elif [ "${os_hint}" = "scientific" ]; then
        dist=`cat /etc/redhat-release | awk '{ print $4 }' | awk -F '.' '{ print $1 }'`
        os='scientific'
      else
        dist=`cat /etc/redhat-release  | awk '{ print tolower($7) }' | cut -f1 --delimiter='.'`
        os='redhatenterpriseserver'
      fi

    else
      aws=`grep -q Amazon /etc/issue`
      if [ "$?" = "0" ]; then
        dist='6'
        os='aws'
      else
        unknown_os
      fi
    fi
  fi

  if [[ ( -z "${os}" ) || ( -z "${dist}" ) ]]; then
    unknown_os
  fi

  # remove whitespace from OS and dist name
  os="${os// /}"
  dist="${dist// /}"

  echo "Detected operating system as ${os}/${dist}."
}

parkos_base ()
{
  echo "begin init parkos base with python ${pyv}."
  hostnamectl set-hostname park
  sed -i 's/park//g' /etc/hosts
  sed -i '1s/$/& park/' /etc/hosts
  useradd -d /home/park -p '' park
  
  echo "make parkos work dir."
  cd / && mkdir -p /dxpark
  cd /dxpark && mkdir -p service
  cd /dxpark/service && mkdir -p parkos
  cd /dxpark/service/parkos && mkdir -p db
  
  echo "init parkos work environment."
  cd /usr/local/
  wget -O redis.tar.gz http://frp.hjzhparking.com/redis.tar.gz
  tar zxvf redis.tar.gz
  #wget -O jdk17_linux_x64_bin.tar.gz http://frp.hjzhparking.com/jdk17_linux_x64_bin.tar.gz
  #tar zxvf jdk17_linux_x64_bin.tar.gz
  wget -O openjdk-17.0.2_linux-x64_bin.tar.gz http://frp.hjzhparking.com/openjdk-17.0.2_linux-x64_bin.tar.gz
  tar zxvf openjdk-17.0.2_linux-x64_bin.tar.gz
  
  echo "init parkos work data."
  ##动态
  cd /dxpark/
  wget -O parkweb.tar.gz http://frp.hjzhparking.com/parkweb.tar.gz
  wget -O psutil-2.1.3.tar.gz http://lffrp.hmcityos.cn/files/parkos/psutil-2.1.3.tar.gz
  tar zxvf psutil-2.1.3.tar.gz
  
  cd /dxpark/service/parkos
  wget -O parkos-0.5.5.jar http://frp.hjzhparking.com/v055/parkos-0.5.5.jar
  wget -O lib.tar.gz http://frp.hjzhparking.com/lib.tar.gz
  tar zxvf lib.tar.gz
  cd /dxpark/service/parkos/lib
  wget -O comm-0.0.1.jar http://frp.hjzhparking.com/v055/comm-0.0.1.jar
  ##固定
  cd /dxpark/
  wget -O dxpark_check.py http://frp.hjzhparking.com/v2/dxpark_check.py
  wget -O dxpark_version.py http://frp.hjzhparking.com/v2/dxpark_version.py
  wget -O crontab.log http://frp.hjzhparking.com/crontab.log
  cd /dxpark/service/
  ##视频
  wget -O EasyDarwin.tar.gz http://frp.hjzhparking.com/EasyDarwin.tar.gz
  tar zxvf EasyDarwin.tar.gz
  ##远程
  wget -O easyntd.tar.gz http://frp.hjzhparking.com/easyntd.tar.gz
  #tar zxvf easyntd.tar.gz
  wget -O frpc.tar.gz http://frp.hjzhparking.com/frpc.tar.gz
  tar zxvf frpc.tar.gz
  cd /dxpark/service/parkos
  wget -O libs.tar.gz http://frp.hjzhparking.com/libs.tar.gz
  ##v2文件（无nginx+openjdk）
  wget -O application.yaml http://frp.hjzhparking.com/v2/application.yaml
  wget -O application-hj.yaml http://frp.hjzhparking.com/v2/application-hj.yaml
  wget -O application-hjmy.yaml http://frp.hjzhparking.com/v2/application-hjmy.yaml
  wget -O application.properties http://frp.hjzhparking.com/v2/application.properties
  wget -O logback-spring.xml http://frp.hjzhparking.com/v2/logback-spring.xml
  wget -O install.sh http://frp.hjzhparking.com/v2/install.sh
  wget -O start.sh http://frp.hjzhparking.com/v2/start.sh
  wget -O stop.sh http://frp.hjzhparking.com/v2/stop.sh
  wget -O restart.sh http://frp.hjzhparking.com/v2/restart.sh
  tar zxvf libs.tar.gz
  cd /dxpark/service/parkos/db
  wget -O park.db http://frp.hjzhparking.com/park.db
  
  pkill nginx
  pkill nginx
  pkill nginx
  
  sh /dxpark/service/parkos/install.sh
  sh /dxpark/service/parkos/stop.sh
  sh /dxpark/service/parkos/stop.sh
  sh /dxpark/service/parkos/stop.sh
  
  chmod a+x /dxpark/*.py
  chmod a+x /dxpark/service/frpc/*.sh
  chmod a+x /dxpark/service/parkos/*.sh 
  
  chown -R park:park /dxpark/
  chown -R park:park /usr/local/redis/
  chmod 777 -R /dxpark/
  
  #视频
  cd /dxpark/service/EasyDarwin/
  bash install.sh
  bash start.sh
  
  echo "delete temp files."
  cd /dxpark/
  rm -rf *.tar.gz
  cd /dxpark/service/
  rm -rf *.tar.gz
  cd /dxpark/service/parkos/
  rm -rf *.tar.gz
  
  #开机自启
  cd /etc/systemd/system/
  wget -O parkredis.service http://frp.hjzhparking.com/parkredis.service
  wget -O parkos.service http://frp.hjzhparking.com/parkos.service
  systemctl enable parkredis.service
  systemctl enable parkos.service
}

centos_base ()
{
  echo "init centos base with python ${pyv}."
  ##更新阿里云镜像
  rm -f /etc/yum.repos.d/CentOS-Base.repo.backup
  mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
  wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
  sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
  yum makecache 
  yum -y update
  
  yum -y remove firefox*
  yum -y install mysql
  yum -y install gcc wget
  yum -y install pcre pcre-devel
  yum -y install make zlib-devel gcc-c++ libtool openssl openssl-devel
  yum -y install openssl openssl-devel lintool openssl-devel
  
  cd /dxpark/service
  wget -O google-chrome-stable_current_x86_64.rpm http://frp.hjzhparking.com/google-chrome-stable_current_x86_64.rpm
  rpm -ivh google-chrome-stable_current_x86_64.rpm
  wget -O sunloginenterprise_3.6.0.52553_x86_64.rpm http://frp.hjzhparking.com/sunloginenterprise_3.6.0.52553_x86_64.rpm
  rpm -ivh sunloginenterprise_3.6.0.52553_x86_64.rpm
  wget -O todesk-v4.7.2.0-c7-x86_64.rpm http://frp.hjzhparking.com/todesk-v4.7.2.0-c7-x86_64.rpm
  #rpm -ivh ToDesk_Host_amd64.rpm
  
  sed -i '/zh_CN.UTF-8/d' /etc/profile
  sed -i '/JAVA_HOME/d' /etc/profile
  sed -i '/export PATH/d' /etc/profile
  
  echo "export LC_ALL=\"zh_CN.UTF-8\"" >> /etc/profile
  echo "export LANG=\"zh_CN.UTF-8\"" >> /etc/profile
  echo "export JAVA_HOME=/usr/local/jdk-17.0.2" >> /etc/profile
  echo 'export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tool.jar' >> /etc/profile
  echo 'export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin' >> /etc/profile
  
  echo "net.ipv4.tcp_fin_timeout = 30" >>  /etc/sysctl.conf
  echo "net.ipv4.tcp_tw_reuse = 1" >>  /etc/sysctl.conf
  echo "net.ipv4.tcp_timestamps = 1" >>  /etc/sysctl.conf
  echo "net.ipv4.tcp_tw_recycle = 1" >>  /etc/sysctl.conf
  echo "net.ipv4.tcp_keepalive_time = 1800" >>  /etc/sysctl.conf
  echo "net.ipv4.tcp_keepalive_probes = 3" >>  /etc/sysctl.conf
  echo "net.ipv4.tcp_keepalive_intvl = 10" >>  /etc/sysctl.conf
  echo "net.ipv4.tcp_syncookies = 1" >>  /etc/sysctl.conf
  
  #echo "* soft nofile 65536" >>  /etc/security/limits.conf 
  #echo "* hard nofile 65536" >>  /etc/security/limits.conf
  
  /sbin/sysctl -p
  
  yum -y install iptables-services iptables
  sed '11 i-A INPUT -p tcp -m state --state NEW -m tcp --dport 8080 -j ACCEPT' -i /etc/sysconfig/iptables
  sed '11 i-A INPUT -p tcp -m state --state NEW -m tcp --dport 8086 -j ACCEPT' -i /etc/sysconfig/iptables
  sed '11 i-A INPUT -p tcp -m state --state NEW -m tcp --dport 8090 -j ACCEPT' -i /etc/sysconfig/iptables
  sed '11 i-A INPUT -p tcp -m state --state NEW -m tcp --dport 8100 -j ACCEPT' -i /etc/sysconfig/iptables
  sed '11 i-A INPUT -p tcp -m state --state NEW -m tcp --dport 8200 -j ACCEPT' -i /etc/sysconfig/iptables
  sed '11 i-A INPUT -p tcp -m state --state NEW -m tcp --dport 8091 -j ACCEPT' -i /etc/sysconfig/iptables
  sed '11 i-A INPUT -p tcp -m state --state NEW -m tcp --dport 8101 -j ACCEPT' -i /etc/sysconfig/iptables
  sed '11 i-A INPUT -p tcp -m state --state NEW -m tcp --dport 8201 -j ACCEPT' -i /etc/sysconfig/iptables
  sed '11 i-A INPUT -p tcp -m state --state NEW -m tcp --dport 8500 -j ACCEPT' -i /etc/sysconfig/iptables
  
  systemctl disable firewalld
  systemctl enable iptables
  systemctl restart iptables
  
  echo "setting system open start..."
  sed -i '/check/d' /etc/rc.d/rc.local
  sed -i '/crontab/d' /etc/rc.d/rc.local
  sed -i '/frp/d' /etc/rc.d/rc.local
  sed -i '/parkos/d' /etc/rc.d/rc.local
  sed -i '/nginx/d' /etc/rc.d/rc.local
  sed -i '/python/d' /etc/rc.d/rc.local
  
  sed -i '/check/d' /etc/crontab
  sed -i '/crontab/d' /etc/crontab
  sed -i '/parkos/d' /etc/crontab
  sed -i '/frp/d' /etc/crontab
  sed -i '/python/d' /etc/crontab
  sed -i '/ntpdate/d' /etc/crontab
  sed -i '/systemctl/d' /etc/crontab
  
  #时间校准
  timedatectl set-timezone Asia/Shanghai
  timedatectl set-local-rtc 1
  hwclock --systohc --localtime
  clock -w
  systemctl enable ntpd
  yum -y install ntp
  echo "2 0 * * * root /usr/sbin/ntpdate ntp1.aliyun.com" >> /etc/crontab
  echo "0 */4 * * * root systemctl stop runsunloginclient.service && systemctl stop runsunloginclient.service" >> /etc/crontab
  
  if [ "${pyv}" -ge 3 ]; then
    #python>=3
	##frp
	cd /dxpark/
	wget -O parkos_frp.py http://frp.hjzhparking.com/parkos3_frp.py
	yum -y install python3-pip
	yum -y install python3-requests
	yum -y install python3-psutil
	yum -y install gcc
	yum -y install python3-devel*
	{
		cd /dxpark/psutil-2.1.3
		python3 setup.py install
	} || {
		echo "psutil init error"
	}
	echo "*/1 * * * * root python3 /dxpark/dxpark_check.py >> /dxpark/crontab.log" >> /etc/crontab
	echo "20 0 * * * root python3 /dxpark/dxpark_version.py > /dxpark/crontab.log" >> /etc/crontab
	python3 /dxpark/parkos_frp.py >> /dxpark/install.log
  else
    #python2
	##frp
	cd /dxpark/
	wget -O parkos_frp.py http://frp.hjzhparking.com/parkos_frp.py
	yum -y install python-pip
	yum -y install python-requests
	yum -y install python-psutil
	yum -y install gcc
	yum -y install python-devel*
	{
		cd /dxpark/psutil-2.1.3
		python setup.py install
	} || {
		echo "psutil init error"
	}
	echo "*/1 * * * * root python /dxpark/dxpark_check.py >> /dxpark/crontab.log" >> /etc/crontab
	echo "20 0 * * * root python /dxpark/dxpark_version.py > /dxpark/crontab.log" >> /etc/crontab
	python /dxpark/parkos_frp.py >> /dxpark/install.log
  fi
  
  source /etc/profile
}

ubuntu_base ()
{
  echo "init ubuntu base with python ${pyv}."
  ##更新ssh
  apt -y update
  apt-get -y install ntp
  apt-get -y install make
  apt-get -y install gcc
  apt-get -y install g++
  apt-get -y install libpcre3 libpcre3-dev
  apt-get -y install libssl-dev zlib1g-dev
  apt-get -y install sqlite3
  apt-get -y install net-tools
  apt-get -y install vim
  apt-get -y install curl
  apt-get -y install openssh-server
  apt-get -y install mariadb-client
  chmod +w /etc/ssh/sshd_config
  sed -i '/PermitRootLogin/d' /etc/ssh/sshd_config
  echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
  systemctl restart ssh
  systemctl restart sshd
  systemctl enable ntp 
  systemctl start ntp
 
  ##卸载
  dpkg -r sunloginclient
  dpkg -r sunloginclient
  dpkg -r sunloginremote
  dpkg -r sunloginenterprise
  #卸载火狐浏览器
  dpkg -r firefox*
  apt-get remove -y firefox*
  apt-get purge -y firefox*
  
  cd /dxpark/service
  wget -O google-chrome-stable_current_x86_64.deb http://frp.hjzhparking.com/google-chrome-stable_current_x86_64.deb
  dpkg -i google-chrome-stable_current_x86_64.deb
  wget -O sunloginenterprise-5.4.0.18280-amd64.deb http://frp.hjzhparking.com/sunloginenterprise-5.4.0.18280-amd64.deb
  dpkg -i sunloginenterprise-5.4.0.18280-amd64.deb
  wget -O ToDesk_Host_amd64.deb http://frp.hjzhparking.com/todesk-v4.7.2.0-amd64.deb
  #dpkg -i ToDesk_Host_amd64.deb
  
  sed -i '/zh_CN.UTF-8/d' /etc/profile
  sed -i '/JAVA_HOME/d' /etc/profile
  sed -i '/export PATH/d' /etc/profile
  
  echo "export LC_ALL=\"zh_CN.UTF-8\"" >> /etc/profile
  echo "export LANG=\"zh_CN.UTF-8\"" >> /etc/profile
  echo "export JAVA_HOME=/usr/local/jdk-17.0.2" >> /etc/profile
  echo 'export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tool.jar' >> /etc/profile
  echo 'export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin' >> /etc/profile
  
  echo "net.ipv4.tcp_fin_timeout = 30" >>  /etc/sysctl.conf
  echo "net.ipv4.tcp_tw_reuse = 1" >>  /etc/sysctl.conf
  echo "net.ipv4.tcp_timestamps = 1" >>  /etc/sysctl.conf
  echo "net.ipv4.tcp_tw_recycle = 1" >>  /etc/sysctl.conf
  echo "net.ipv4.tcp_keepalive_time = 1800" >>  /etc/sysctl.conf
  echo "net.ipv4.tcp_keepalive_probes = 3" >>  /etc/sysctl.conf
  echo "net.ipv4.tcp_keepalive_intvl = 10" >>  /etc/sysctl.conf
  echo "net.ipv4.tcp_syncookies = 1" >>  /etc/sysctl.conf
  
  #echo "* soft nofile 65536" >>  /etc/security/limits.conf 
  #echo "* hard nofile 65536" >>  /etc/security/limits.conf
  
  echo "setting system open start..."
  sed -i '/check/d' /etc/crontab
  sed -i '/crontab/d' /etc/crontab
  sed -i '/parkos/d' /etc/crontab
  sed -i '/frp/d' /etc/crontab
  sed -i '/python/d' /etc/crontab
  sed -i '/ntpdate/d' /etc/crontab
  sed -i '/systemctl/d' /etc/crontab
  
  #ubuntu时间网络校准
  apt-get -y install ntpdate
  apt-get -y install ntpsec-ntpdate
  timedatectl set-timezone Asia/Shanghai
  timedatectl set-ntp true
  #echo "2 0 * * * root ntpdate -d ntp1.aliyun.com" >> /etc/crontab
  echo "0 */4 * * * root systemctl stop runsunloginclient.service && systemctl stop runsunloginclient.service" >> /etc/crontab
  
  ##处理依赖问题
  apt --fix-broken -y install
  if [ "${pyv}" -ge 3 ]; then
    #python>=3
	##frp
	cd /dxpark/
	wget -O parkos_frp.py http://frp.hjzhparking.com/parkos3_frp.py
	apt-get -y install python3-pip
	apt-get -y install python3-requests
	apt-get -y install python3-psutil
	apt-get -y install gcc
	apt-get -y install python3-devel*
	{
		cd /dxpark/psutil-2.1.3
		python3 setup.py install
	} || {
		echo "psutil init error"
	}
	echo "*/1 * * * * root python3 /dxpark/dxpark_check.py >> /dxpark/crontab.log" >> /etc/crontab
	echo "20 0 * * * root python3 /dxpark/dxpark_version.py > /dxpark/crontab.log" >> /etc/crontab
	python3 /dxpark/parkos_frp.py >> /dxpark/install.log
  else
    #python<=2
	##frp
	cd /dxpark/
	wget -O parkos_frp.py http://frp.hjzhparking.com/parkos_frp.py
	apt-get -y install python-pip
	apt-get -y install python-requests
	apt-get -y install python-psutil
	apt-get -y install gcc
	apt-get -y install python-devel*
	{
		cd /dxpark/psutil-2.1.3
		python setup.py install
	} || {
		echo "psutil init error"
	}
	echo "*/1 * * * * root python /dxpark/dxpark_check.py >> /dxpark/crontab.log" >> /etc/crontab
	echo "20 0 * * * root python /dxpark/dxpark_version.py > /dxpark/crontab.log" >> /etc/crontab
	python /dxpark/parkos_frp.py >> /dxpark/install.log
  fi
  source /etc/profile
}

clearlog_base ()
{
	cd /dxpark/
	rm -rf psutil*
	rm -rf parkos_frp.py
	rm -rf *.tar.gz
	cd /dxpark/service/parkos
	rm -rf *.tar.gz
	rm -rf install.log
}

main ()
{
  echo "begin check system version for parkos. "
  detect_os
  if [ "${os,,}" = "centos" ] && [ "${dist}" -ge 8 ]; then
    pyv=3
	parkos_base
	centos_base
  elif [ "${os,,}" = "centos" ] && [ "${dist}" -ge 7 ]; then
    pyv=2
	parkos_base
	centos_base
  elif [ "${os,,}" = "ubuntu" ] && [ "${dist}" -ge 22 ]; then
    pyv=3
	parkos_base
	ubuntu_base
  elif [ "${os,,}" = "ubuntu" ] && [ "${dist}" -ge 20 ]; then
    pyv=3
	parkos_base
	ubuntu_base
  elif [ "${os,,}" = "ubuntu" ] && [ "${dist}" -ge 16 ]; then
    pyv=2
	parkos_base
	ubuntu_base
  else
    unknown_os
  fi
  clearlog_base
  
  echo "begin start parkos..."
  if [ "${pyv}" -ge 3 ]; then
    #python>=3
	python3 /dxpark/dxpark_check.py >> /dxpark/crontab.log
  else
    #python<=2
	python /dxpark/dxpark_check.py >> /dxpark/crontab.log
  fi
  echo "commit done."
}

main
