#! /bin/bash
log_type=$1   # 爬虫策略
crawler=$2    # 哪款爬虫
env=$3        # 爬虫运行环境，正式环境: prod / 测试环境: dev

if [ ${crawler} = "shipinhao" ] && [ ${log_type} = "recommend" ] && [ ${env} = "prod" ];then
  piaoquan_crawler_dir=/Users/lieyunye/Desktop/crawler/piaoquan_crawler/
  profile_path=~/.bash_profile
  node_path=/usr/local/bin/node
  log_path=${piaoquan_crawler_dir}main/main_logs/start-appium-$(date +%Y-%m-%d).log
elif [ ${crawler} = "shipinhao" ] && [ ${log_type} = "search" ] && [ ${env} = "prod" ];then
  piaoquan_crawler_dir=/Users/piaoquan/Desktop/piaoquan_crawler/
  profile_path=~/.bash_profile
  node_path=/usr/local/bin/node
  log_path=${piaoquan_crawler_dir}main/main_logs/start-appium-$(date +%Y-%m-%d).log
elif [ ${crawler} = "jixiangxingfu" ] || [ ${crawler} = "zhongmiaoyinxin" ] || [ ${crawler} = "zhiqingtiantiankan" ] || [ ${crawler} = "ganggangdouchuan" ];then
  piaoquan_crawler_dir=/Users/piaoquan/Desktop/piaoquan_crawler/
  profile_path=./base_profile
  node_path=/usr/local/bin/node
  log_path=${piaoquan_crawler_dir}main/main_logs/start-appium-$(date +%Y-%m-%d).log
elif [ ${crawler} = "xigua" ] || [ ${log_type} = "recommend" ];then
  piaoquan_crawler_dir=/Users/kanyikan/Desktop/crawler/piaoquan_crawler/
  profile_path=/etc/profile
  node_path=/usr/local/bin/node
  log_path=${piaoquan_crawler_dir}main/main_logs/start-appium-$(date +%Y-%m-%d).log
elif [ ${crawler} = "shipinhao" ] || [ ${log_type} = "search" ];then
  piaoquan_crawler_dir=/Users/piaoquan/Desktop/piaoquan_crawler/
  profile_path=/etc/profile
  node_path=/usr/local/bin/node
  log_path=${piaoquan_crawler_dir}main/main_logs/start-appium-$(date +%Y-%m-%d).log
elif [ ${crawler} = "xngplus" ] ;then
  piaoquan_crawler_dir=/Users/a123456/Desktop/spider/piaoquan_crawler/
  profile_path=/.base_profile
  node_path=/usr/local/bin/node
  log_path=${piaoquan_crawler_dir}main/main_logs/process-mq-$(date +%Y-%m-%d).log
else
  piaoquan_crawler_dir=/Users/wangkun/Desktop/crawler/piaoquan_crawler/
  profile_path=/etc/profile
  node_path=/opt/homebrew/bin/node
  log_path=${piaoquan_crawler_dir}main/main_logs/start-appium-$(date +%Y-%m-%d).log
fi

echo "$(date "+%Y-%m-%d %H:%M:%S") 正在检测 Appium 运行状态 ..." >> ${log_path}
ps -ef | grep "/Applications/Appium.app/Contents/Resources/app/node_modules/appium/build/lib/main.js" | grep -v "grep"
if [ "$?" -eq 1 ];then
  echo "$(date "+%Y-%m-%d %H:%M:%S") Appium异常停止,正在重启!" >> ${log_path}
  cd ${piaoquan_crawler_dir} && nohup ${node_path} /Applications/Appium.app/Contents/Resources/app/node_modules/appium/build/lib/main.js >> main/main_logs/Appium.log 2>&1 &
  echo "$(date "+%Y-%m-%d %H:%M:%S") 重启Appium完毕!" >> ${log_path}
else
  cd ${piaoquan_crawler_dir} && rm -f main/main_logs/Appium.log
  echo "$(date "+%Y-%m-%d %H:%M:%S") Appium 运行状态正常。" >> ${log_path}
fi

# 删除日志
echo "$(date "+%Y-%m-%d %H:%M:%S") 开始清理 10 天前的日志文件" >> ${log_path}
find ${piaoquan_crawler_dir}main/main_logs/ -mtime +10 -name "*.log" -exec rm -rf {} \;
echo "$(date "+%Y-%m-%d %H:%M:%S") 日志文件清理完毕" >> ${log_path}
exit 0