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

mysql_base()
{
  cd / && mkdir -p /dxpark
  cd /dxpark && mkdir -p tools
  wget -o parkos.sql http://files.dxpark.cn/mysql/parkos.sql
}

centos_mysql ()
{
  cd /dxpark/tools
  wget -o mysql7.tar.gz http://files.dxpark.cn/mysql/mysql7.tar.gz
  wget -o mysql7_install.sh http://files.dxpark.cn/mysql/mysql7_install.sh
  wget -o mysql7_init.sql http://files.dxpark.cn/mysql/mysql7_init.sql
  
  tar zxvf mysql7.tar.gz
  cd /dxpark/tools/mysql
  
  yum remove -y mariadb-libs-1:5.5.56-2.el7.x86_64
  yum remove -y mariadb-libs.x86_64 
  
  rpm -qa | grep -i mysql | xargs yum remove -y 
  
  rm -rf /var/log/mysqld.log
  rm -rf /var/lib/mysql
  rm -rf /etc/my.cnf*
  
  yum install -y tcl-devel
  yum install -y expect
  
  yum install -y mysql-community-common-5.7.21-1.el7.x86_64.rpm 
  yum install -y mysql-community-libs-5.7.21-1.el7.x86_64.rpm
  yum install -y mysql-community-libs-compat-5.7.21-1.el7.x86_64.rpm
  yum install -y mysql-community-devel-5.7.21-1.el7.x86_64.rpm
  yum install -y mysql-community-client-5.7.21-1.el7.x86_64.rpm 
  yum install -y mysql-community-server-5.7.21-1.el7.x86_64.rpm
  
  echo "sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES" >> /etc/my.cnf
  echo "event_scheduler=1" >> /etc/my.cnf
  
  systemctl enable mysqld
  systemctl restart mysqld
  
  sleep 10

passwd=`grep 'A temporary password' /var/log/mysqld.log | awk '{print $NF}'`
/usr/bin/expect <<EOF
spawn mysql -uroot  -p
expect "Enter password:"
send "$passwd\r"
expect "mysql>"
send "source /dxpark/tools/mysql7_init.sql\r"
send "quit\r"
expect eof
EOF

mysql -hlocalhost -P3306 -u park -ppark123 -e "
drop database if exists parkos;  
CREATE DATABASE IF NOT EXISTS parkos default charset utf8mb4 COLLATE utf8mb4_general_ci;
quit" 
mysql -hlocalhost -P3306 -u park -ppark123 parkos</dxpark/tools/parkos.sql
}

ubuntu_mysql()
{
  cd /dxpark/tools
  wget -o ubuntu.tar.gz http://files.dxpark.cn/mysql/ubuntu.tar.gz
  wget -o mysql7_init.sql http://files.dxpark.cn/mysql/mysql7_init.sql
  tar zxvf ubuntu.tar.gz
  cd /dxpark/tools/ubuntu
  
  sudo systemctl stop mysql
  sudo systemctl stop mysql
  sudo apt-get -y remove --purge mysql-server mysql-client mysql-common 
  sudo rm -rf /etc/mysql /var/lib/mysql
  sudo apt-get -y autoremove
  sudo apt-get -y autoclean
  sudo apt-get -y remove --purge mysql-server-core-*

  sudo apt-get -y install expect
  sudo apt-get -y update
  sudo apt-get -y install ./libmysql*
  sudo apt-get -y install libtinfo5
  
  sudo apt-get -y install ./mysql-community-client_5.7.29-1ubuntu18.04_amd64.deb
  sudo apt-get -y install ./mysql-client_5.7.29-1ubuntu18.04_amd64.deb
  ##会出现界面设置root密码
  sudo apt-get -y install ./mysql-community-server_5.7.29-1ubuntu18.04_amd64.deb
  sudo apt-get -y install ./mysql-server_5.7.29-1ubuntu18.04_amd64.deb

  #echo "sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES" >> /etc/my.cnf
  #echo "event_scheduler=1" >> /etc/my.cnf
  
  #source /dxpark/tools/mysql7_init.sql
 
 echo "请输入设置的root密码"
 read passwd
/usr/bin/expect <<EOF
spawn mysql -uroot  -p
expect "Enter password:"
send "$passwd\r"
expect "mysql>"
send "source /dxpark/tools/mysql7_init.sql\r"
send "quit\r"
expect eof
EOF
  systemctl enable mysql 
  systemctl restart mysql
  mysql -hlocalhost -P3306 -u park -ppark123 -e "
  drop database if exists parkos;  
  CREATE DATABASE IF NOT EXISTS parkos default charset utf8mb4 COLLATE utf8mb4_general_ci;
  quit" 
  mysql -hlocalhost -P3306 -u park -ppark123 parkos</dxpark/tools/parkos.sql
}

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

main
