#!/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}."
}

centos_base ()
{
	yum -y install gcc
	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
	./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-stream_ssl_module --with-stream
	make && make install
	cd /usr/local/nginx/conf
	rm -rf nginx.conf
	wget -O nginx.conf http://files.hmcityos.cn/nginx.conf
	chmod -R 777 /usr/local/nginx/logs
	chmod -R 777 /usr/local/nginx/proxy_temp
	chown -R park:park /usr/local/nginx/
	
	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 8100 -j ACCEPT' -i /etc/sysconfig/iptables
	systemctl enable iptables
	systemctl restart iptables
}

ubuntu_base ()
{
	apt-get -y install gcc make
	apt-get -y install libpcre3 libpcre3-dev
	apt-get -y install libssl-dev zlib1g-dev
	apt-get -y install openssl
	
	./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-stream_ssl_module --with-stream
	make && make install
	cd /usr/local/nginx/conf
	rm -rf nginx.conf
	wget -O nginx.conf http://files.hmcityos.cn/nginx.conf
	chmod -R 777 /usr/local/nginx/logs
	chmod -R 777 /usr/local/nginx/proxy_temp
	chown -R park:park /usr/local/nginx/
	
	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 8100 -j ACCEPT' -i /etc/sysconfig/iptables
	systemctl enable iptables
	systemctl restart iptables
}

main ()
{
  echo "begin check system version for parkos. "
  detect_os
  if [ "${os,,}" = "centos" ]; then
	centos_base
  elif [ "${os,,}" = "ubuntu" ]; then
	ubuntu_base
  else
    unknown_os
  fi
  
  echo "commit done."
}

main
