#!/bin/bash

mkdir -p /opt/operate
mkdir -p /var/log/operate

wget --no-check-certificate -P /opt https://file.tyxhetc.cn/receive-operate.zip
wget --no-check-certificate -P /opt/operate https://file.tyxhetc.cn/update/update.py

unzip -d /opt /opt/receive-operate.zip
rm -rf /opt/receive-operate.zip

sed -i 's/]/, {"pjname": "receive-operate", "version": "3.7.9", "rfile": "\/opt\/receive-operate\/run.py", "path": "\/opt\/receive-operate\/", "vpath": "\/opt\/receive-operate\/\/*_venv", "status": "0", "port": "", "rtype": "python", "proxy": "", "framework": "python", "auto_start": "0", "user": "root", "parm": "", "run": "run:app", "worker": "worker_class = geventwebsocket.gunicorn.workers.GeventWebSocketWorker", "cpu": 0, "mem": 0}]/g' /www/server/panel/plugin/pythonmamager/config.json
sed -i "s/worker_class = geventwebsocket.gunicorn.workers.GeventWebSocketWorker/worker_class = 'geventwebsocket.gunicorn.workers.GeventWebSocketWorker'/g" /www/server/panel/plugin/pythonmamager/config.json

# receive-operate 配置
cd /opt/receive-operate && /www/server/python_manager/versions/3.*.*/bin/python3 -m venv _venv && source ./_venv/bin/activate && pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple --default-timeout=100

cp -r /opt/receive-operate/_venv /opt/operate
mv /opt/operate/_venv /opt/operate/venv

# nginx 重新配置
rm -rf /www/server/nginx/conf/nginx.conf
wget --no-check-certificate -P /www/server/nginx/conf https://file.tyxhetc.cn/nginx.conf
# 执行权
chmod +x /www/server/nginx/conf/nginx.conf
# 重启 nginx
/etc/init.d/nginx reload

wget --no-check-certificate -P /www/server/panel/plugin/supervisor/profile https://file.tyxhetc.cn/receive-operate.ini

/www/server/panel/pyenv/bin/supervisorctl reload
