#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
if command -v apt-get >/dev/null; then
  if command -v sudo >/dev/null; then
    sudo apt-get update -y
    sudo apt-get install -y unzip python3 python3-pip tmux
  else
    apt-get update -y
    apt-get install -y unzip python3 python3-pip tmux
  fi
fi
python3 -m pip install -U requests loguru httpx fake-useragent PySocks --break-system-packages
use_tmux=0
if command -v tmux >/dev/null; then
  use_tmux=1
fi
tmux_session="jobs_spider"
if [ "$use_tmux" -eq 1 ]; then
  if ! tmux has-session -t "$tmux_session" 2>/dev/null; then
    tmux new-session -d -s "$tmux_session" -c "$SCRIPT_DIR"
  fi
fi
start_one() {
  name="$1"
  dir="$2"
  entry="$3"
  cd "$dir"
  mkdir -p logs
  if pgrep -f "$entry" >/dev/null; then
    echo "$name 已在运行"
    return 0
  fi
  if [ "$use_tmux" -eq 1 ]; then
    if tmux list-windows -t "$tmux_session" 2>/dev/null | awk -F: '{print $2}' | awk '{print $1}' | grep -qx "$name"; then
      if tmux respawn-window -t "$tmux_session:$name" -k -c "$dir" "python3 $entry >> logs/runner.log 2>&1"; then
        :
      else
        tmux kill-window -t "$tmux_session:$name" 2>/dev/null || true
        tmux new-window -t "$tmux_session" -n "$name" -c "$dir" "python3 $entry >> logs/runner.log 2>&1"
      fi
    else
      tmux new-window -t "$tmux_session" -n "$name" -c "$dir" "python3 $entry >> logs/runner.log 2>&1"
    fi
    echo "$name tmux: $tmux_session:$name"
  else
    nohup python3 "$entry" >> logs/runner.log 2>&1 &
    echo "$name PID: $!"
  fi
}
start_one "boss" "$SCRIPT_DIR/boss" "boos_api.py"
start_one "qcwy" "$SCRIPT_DIR/qcwy" "qcwy.py"
start_one "zhilian" "$SCRIPT_DIR/zhilian" "zhilian_single.py"
