#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
cd "$DIR"

PORT=58201

# Hentikan service lama bila sudah ada yang berjalan
PIDS=$(pgrep -f "python3.*ocr_service.py")
if [ -n "$PIDS" ]; then
    echo "Menghentikan layanan ocr_service sebelumnya (PID: $PIDS)..."
    kill -9 $PIDS 2>/dev/null
    sleep 1
fi

echo "Menyalakan Python OCR Microservice di background pada port $PORT..."
nohup python3 -u ocr_service.py > ocr_service.log 2>&1 &
NEW_PID=$!

echo "Layanan berhasil dihidupkan! (PID: $NEW_PID)"
echo "Log pencaharian dapat Anda lihat di: $DIR/ocr_service.log"
