#!/bin/bash

# ===============================
# INSTALL PROCESSOR SCRIPT
# ===============================

if [ -z "$1" ]; then
    echo "Lỗi: Bạn chưa nhập tên Processor."
    echo "Cách dùng: $0 <PROCESSOR_NAME>"
    exit 1
fi

PROCESSOR_NAME=$1
USER_HOME=$HOME
LOG_DIR="$USER_HOME/logs"

echo "=== BẮT ĐẦU CÀI ĐẶT VỚI TÊN: $PROCESSOR_NAME ==="

# ---------------------------------------------------------
# 1. CÀI ĐẶT ZSH
# ---------------------------------------------------------
echo "--- Đang cài đặt ZSH ---"

sudo apt-get update -y
sudo apt-get install -y zsh

if [ ! -d "$USER_HOME/.oh-my-zsh" ]; then
    sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended
    sudo chsh -s "$(which zsh)" "$USER"
else
    echo "Oh My Zsh đã được cài đặt."
fi

# ---------------------------------------------------------
# 2. CÀI ĐẶT DOCKER
# ---------------------------------------------------------
echo "--- Đang cài đặt Docker ---"

sudo apt-get update -y
sudo apt-get install -y ca-certificates curl gnupg

# Tạo keyrings
sudo install -m 0755 -d /etc/apt/keyrings

# Xóa key cũ
sudo rm -f /etc/apt/keyrings/docker.gpg

# Tải GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg \
    | sudo gpg --dearmor --yes -o /etc/apt/keyrings/docker.gpg

sudo chmod a+r /etc/apt/keyrings/docker.gpg

# Xóa repo cũ nếu có
sudo rm -f /etc/apt/sources.list.d/docker.list

# Add Docker repo
echo "deb [arch=$(dpkg --print-architecture) \
signed-by=/etc/apt/keyrings/docker.gpg] \
https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" \
| sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# Install Docker
sudo apt-get update -y
sudo apt-get install -y \
    docker-ce docker-ce-cli containerd.io \
    docker-buildx-plugin docker-compose-plugin

sudo usermod -aG docker "$USER"

echo "LƯU Ý: Bạn cần logout/login để dùng Docker không cần sudo."

# ---------------------------------------------------------
# 3. WORKSPACE + NETWORK
# ---------------------------------------------------------
echo "--- Thiết lập Workspace ---"

mkdir -p "$USER_HOME/workspace/docker-ffmpeg-api"
mkdir -p "$USER_HOME/workspace/docker-processor"
mkdir -p "$LOG_DIR"

if ! sudo docker network ls | grep -q "ffmpeg-processor-network"; then
    sudo docker network create ffmpeg-processor-network
fi

# ---------------------------------------------------------
# 4. CLONE docker-ffmpeg-api
# ---------------------------------------------------------
echo "--- Cài đặt docker-ffmpeg-api ---"

cd "$USER_HOME/workspace/docker-ffmpeg-api"

if [ ! -d "iam-video-py" ]; then
    git clone https://pull:glpat-xFsJomazxZScsAGSSW1B@git.iamsoftware.com.vn/hoa.ngominh/iam-video-py.git
fi

cd iam-video-py
chmod +x update_ffmpeg.sh
sudo ./update_ffmpeg.sh

# ---------------------------------------------------------
# 5. CLONE docker-processor + TẠO FILE UPDATE
# ---------------------------------------------------------
echo "--- Cài đặt docker-processor ---"

cd "$USER_HOME/workspace/docker-processor"

if [ ! -d "iam-video" ]; then
    git clone https://pull:glpat-fxg_iVX7PHspz39cJczR@git.iamsoftware.com.vn/hoa.ngominh/iam-video.git
fi

cd iam-video/processor

# Tạo file update-processor-1.sh
cat <<EOF > update-processor-1.sh
#!/bin/bash
WORKDIR="$USER_HOME/workspace/docker-processor/iam-video/processor"
cd "\$WORKDIR"

git pull https://pull:glpat-fxg_iVX7PHspz39cJczR@git.iamsoftware.com.vn/hoa.ngominh/iam-video.git

docker stop iam-processor-container || true
docker rm iam-processor-container || true
docker rmi iam-processor:latest || true

docker build -t iam-processor .
docker run -d -p 3005:3005 --network ffmpeg-processor-network \
    --name iam-processor-container --restart always \
    -e PROCESSOR_NAME='${PROCESSOR_NAME}' iam-processor

API_URL="https://app.autovideo.vn/api/daemon/Processor/update?name=${PROCESSOR_NAME}"

curl -s -X PUT "\$API_URL" \
  -H "Content-Type: application/json" \
  -d '{"requireUpdate": false}'

echo "API updated: requireUpdate=false"
EOF

chmod +x update-processor-1.sh

echo "Đang khởi tạo processor..."
sudo ./update-processor-1.sh

# ---------------------------------------------------------
# 6. CRON CHECK UPDATE
# ---------------------------------------------------------
echo "--- Cài đặt Auto Update ---"

sudo tee /usr/local/bin/check_update.sh > /dev/null <<EOF
#!/bin/bash

API_URL="https://app.autovideo.vn/api/daemon/Processor/ping?name=${PROCESSOR_NAME}"

RES=\$(curl -s -X POST "\$API_URL")

echo "Response: \$RES"

if echo "\$RES" | grep -q '"requireUpdate":true'; then
    echo "Update required! Running update script..."
    $USER_HOME/workspace/docker-processor/iam-video/processor/update-processor-1.sh
fi
EOF

sudo chmod +x /usr/local/bin/check_update.sh

CRON_CMD="*/5 * * * * /usr/local/bin/check_update.sh 2>&1 | tee -a $LOG_DIR/check_update.log | tail -n 1000 > $LOG_DIR/check_update.log.tmp && mv $LOG_DIR/check_update.log.tmp $LOG_DIR/check_update.log"

(crontab -l 2>/dev/null | grep -F "/usr/local/bin/check_update.sh") || (crontab -l 2>/dev/null; echo "$CRON_CMD") | crontab -

echo "=== CÀI ĐẶT HOÀN TẤT CHO: $PROCESSOR_NAME ==="

