#!/bin/bash

# 设置 Git 仓库的 URL
GIT_REPO_URL="http://120.46.190.49:10880/cxd/wechat_mini.git"
GIT_BRANCH="main"

# 设置工作目录
WORK_DIR="/docker"

# 进入工作目录
cd $WORK_DIR

# 配置 Git 凭证助手
git config --global credential.helper store

# 初始化 Git 仓库（如果尚未初始化）
if [ ! -d ".git" ]; then
    git init
    git remote add origin $GIT_REPO_URL
    git fetch
    git checkout -t origin/$GIT_BRANCH
fi

# 监测 Git 仓库的更新
while true; do
    git fetch origin $GIT_BRANCH
    LOCAL=$(git rev-parse HEAD)
    REMOTE=$(git rev-parse origin/$GIT_BRANCH)

    if [ $LOCAL != $REMOTE ]; then
        echo "New updates found. Pulling latest changes..."
        git pull origin $GIT_BRANCH
        echo "Restarting Django server..."
        pkill -f "python manage.py runserver"
        python manage.py migrate
        python manage.py runserver 0.0.0.0:8085 &
    fi

    # 每隔60秒检查一次
    sleep 60
done