#!/bin/bash

node -v &> /dev/null
if [ $? -eq 0 ];then
  echo "<<=========== Web环境已部署 ==========>>"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> ./init.log
  echo "<<=========== Web环境已部署 ==========>>" >> ./init.log
  echo "##########" >> ./init.log
  exit 1;
fi

echo "<<========= Web环境部署[开始] ========>>"
date '+时间 : %Y-%m-%d %H:%M:%S' >> ./init.log
echo "<<========= Web环境部署[开始] ========>>" >> ./init.log
echo "##########" >> ./init.log

username='simba'
web_path="/home/simba/simba-server/webMonitor"

#yum -y install gcc-c++ make 
curl -sL https://rpm.nodesource.com/setup_12.x | sudo -E bash - 
yum -y install nodejs 
if [ $? -eq 0 ];then
  echo -e "nodejs安装                     \033[32m 完成 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> ./init.log
  echo    "nodejs安装-----------------------------[完成]" >> ./init.log
  echo "##########" >> ./init.log
else
  echo -e "nodejs安装                     \033[31m 失败 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> ./init.log
  echo    "nodejs安装-----------------------------[失败]" >> ./init.log
  echo "##########" >> ./init.log
  exit 1;
fi

mkdir -p ${web_path}/{logs,backup,dist}
cat ./doc/moiiomStart > ${web_path}/autoStart.sh
cat ./doc/moiiomStop > ${web_path}/stop.sh
chmod +x ${web_path}/*.sh
chown ${username}:${username} -R /home/simba

npm i -g serve@13.0.2 --registry=https://registry.npmmirror.com
if [ $? -eq 0 ];then
  echo -e "serve安装                      \033[32m 完成 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> ./init.log
  echo    "serve安装------------------------------[完成]" >> ./init.log
  echo "##########" >> ./init.log
  echo "<<========= Web环境部署[完成] ========>>"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> ./init.log
  echo "<<========= Web环境部署[完成] ========>>" >> ./init.log
  echo "##########" >> ./init.log
else
  echo -e "serve安装                      \033[31m 失败 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> ./init.log
  echo    "serve安装------------------------------[失败]" >> ./init.log
  echo "##########" >> ./init.log
  echo "<<========= Web环境部署[失败] ========>>"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> ./init.log
  echo "<<========= Web环境部署[失败] ========>>" >> ./init.log
  echo "##########" >> ./init.log
  exit 1;
fi
