#!/bin/bash

crawler_dir=$1  # 爬虫执行路径，如: ./youtube/youtube_main/run_youtube_follow.py
log_type=$2     # 日志命名格式，如: follow，则在 youtube/logs/目录下，生成 2023-02-08-follow.log
crawler=$3      # 哪款爬虫，如: youtube / kanyikan / weixinzhishu
env=$4          # 爬虫运行环境，正式环境: prod / 测试环境: dev
#machine=$5      # 爬虫运行机器，阿里云服务器: aliyun_hk / aliyun / macpro / macair / local
nohup_dir=$5    # nohup日志存储路径，如: ./youtube/nohup.log

echo "开始"
#echo ${env}
if [ ${env} = "--env=hk" ];then
  piaoquan_crawler_dir=/root/piaoquan_crawler/
  profile_path=/etc/profile
  python=python3
elif [ ${env} = "--env=prod" ];then
  piaoquan_crawler_dir=/root/piaoquan_crawler/
  profile_path=/etc/profile
  python=/usr/bin/python3
elif [ ${env} = "--env=offline" ];then
  piaoquan_crawler_dir=/Users/piaoquan/Desktop/piaoquan_crawler/
  profile_path=./base_profile
  node_path=/usr/local/bin/node
  python=python3
elif [ ${env} = "--env=dev" ];then
  piaoquan_crawler_dir=/Users/wangkun/Desktop/crawler/piaoquan_crawler/
  profile_path=/etc/profile
  node_path=/opt/homebrew/bin/node
  python=python3
fi

echo "$(date "+%Y-%m-%d %H:%M:%S") 更新环境变量..."
cd ~ && source ${profile_path}
echo "$(date "+%Y-%m-%d %H:%M:%S") 更新环境变量完成！"

if [ ${env} = "--env=hk" ];then
  echo "升级yt-dlp"
  pip3 install yt-dlp -U
else
  echo "$(date "+%Y-%m-%d %H:%M:%S") 正在更新代码..."
  cd ${piaoquan_crawler_dir} && git pull origin master --force && rm -f ${piaoquan_crawler_dir}main/nohup.log && rm -f ${piaoquan_crawler_dir}${nohup_dir}
  echo "$(date "+%Y-%m-%d %H:%M:%S") 代码更新完成！"
fi

echo "$(date "+%Y-%m-%d %H:%M:%S") 正在重启服务..."
cd ${piaoquan_crawler_dir}
nohup ${python} -u ${crawler_dir} ${log_type} ${crawler} ${env} >>${nohup_dir} 2>&1 &
echo "$(date "+%Y-%m-%d %H:%M:%S") 服务重启完毕!"

exit 0