#!/usr/bin/env bash

basedir=$(cd $(dirname "${BASH_SOURCE[0]}"); pwd )
cd $basedir

echo "setting JDK-17..." >> install.log
#rpm -qa | grep -i jdk | xargs yum remove -y 
cd /usr/local/
#rm -rf jdk*
wget -O openjdk-17.0.2_linux-x64_bin.tar.gz http://files.dxpark.cn/openjdk-17.0.2_linux-x64_bin.tar.gz
#yum install -y expect
tar zxvf openjdk-17.0.2_linux-x64_bin.tar.gz

echo "setting init parkos..." >> install.log
cd /fly/
rm -rf dxpark*
mkdir -p dxpark
cd /fly/dxpark && mkdir -p parkos && mkdir -p parkweb && mkdir -p tools
cd /fly/dxpark/parkos && mkdir -p db

cd /usr/local/
pkill redis
pkill redis
rm -rf redis*
wget -O redis.tar.gz http://files.dxpark.cn/redis.tar.gz
tar zxvf redis.tar.gz
cd redis
wget -O redis.conf http://files.dxpark.cn/redis.conf

echo "download to init projcet..." >> install.log
#yum -y install epel-release
yum -y install python-pip
yum -y install python-requests
yum -y install python-psutil
#yum -y install freetype-devel
#yum -y install libappindicator-gtk3
cd /fly/dxpark/tools
wget -O hmcityos.png http://files.dxpark.cn/hmcityos.png

cd /fly/dxpark/parkos
wget -O application.yaml http://files.dxpark.cn/v2/application.yaml
wget -O application-wh.yaml http://files.dxpark.cn/v2/application-wh.yaml
wget -O application-whmy.yaml http://files.dxpark.cn/v2/application-whmy.yaml
wget -O application.properties http://files.dxpark.cn/v2/application.properties
wget -O install.sh http://files.dxpark.cn/v2/install_flyos.sh
wget -O start.sh http://files.dxpark.cn/v2/start_flyos.sh
wget -O stop.sh http://files.dxpark.cn/v2/stop_flyos.sh
wget -O restart.sh http://files.dxpark.cn/v2/restart.sh

wget -O libs.tar.gz http://files.dxpark.cn/libs.tar.gz
wget -O logback-spring.xml http://files.dxpark.cn/logback-spring.xml
wget -O lib.tar.gz http://files.dxpark.cn/v040/lib.tar.gz
wget -O parkos-0.4.0.jar http://files.dxpark.cn/v040/parkos-0.4.0.jar
#park.db file
cd db
wget -O park.db http://files.dxpark.cn/park.db
cd /fly/dxpark/
wget -O parkweb.tar.gz http://files.dxpark.cn/v040/parkweb.tar.gz

cd /fly/
wget -O dxpark_check.py http://files.dxpark.cn/flyos/dxpark_check.py
wget -O dxpark_version.py http://files.dxpark.cn/flyos/dxpark_version.py
wget -O crontab.log http://files.dxpark.cn/crontab.log

echo "unzip projcet..." >> install.log
cd /fly/dxpark/
rpm -e sunloginremote
rpm -e sunloginclient
wget -O sunloginenterprise_3.6.0.52553_x86_64.rpm http://files.dxpark.cn/sunloginenterprise_3.6.0.52553_x86_64.rpm
rpm -ivh sunloginenterprise_3.6.0.52553_x86_64.rpm
wget -O ToDesk_Host_amd64.rpm http://files.dxpark.cn/ToDesk_Host_amd64.rpm
#rpm -ivh ToDesk_Host_amd64.rpm
tar zxvf parkweb.tar.gz
cd /fly/dxpark/parkos
tar zxvf lib.tar.gz
tar zxvf libs.tar.gz

#授权
chmod a+x /fly/dxpark/parkos/*.sh 
chmod a+x /fly/*.py 

sh /fly/dxpark/parkos/install.sh
sh /fly/dxpark/parkos/stop.sh
sh /fly/dxpark/parkos/stop.sh
sh /fly/dxpark/parkos/stop.sh

chown -R fly:fly /fly/
chmod 777 -R /fly/dxpark/parkos
chmod 777 -R /fly/dxpark/parkweb

#服务检测任务
sed -i '/crontab.log/d' /etc/crontab
sed -i '/python.log/d' /etc/crontab
sed -i '/systemctl/d' /etc/crontab

echo "*/1 * * * * root python /fly/dxpark_check.py >> /fly/crontab.log" >> /etc/crontab
echo "30 0 * * * root python /fly/dxpark_version.py >> /fly/crontab.log" >> /etc/crontab
echo "0 0/4 * * * root systemctl stop runsunloginclient.service && systemctl stop runsunloginclient.service" >> /etc/crontab

#服务启动
echo "start projcet..." >> install.log
cd /fly/dxpark/
rm -rf *.tar.gz
rm -rf install.log
cd /fly/dxpark/parkos/
rm -rf *.tar.gz
rm -rf install.log

python /fly/dxpark_check.py >> /fly/crontab.log

