#!/bin/bash
# Altria VICIdial → CRM Integration Setup
# Run as root on the VICIdial server (216.219.88.67)
set -e

echo "=== Installing dependencies ==="
zypper install -y ffmpeg git cmake gcc-c++ make

echo "=== Installing Python MySQL connector ==="
pip3 install mysql-connector-python

echo "=== Compiling whisper.cpp ==="
mkdir -p /opt/altria
cd /opt/altria

if [ ! -d "whisper.cpp" ]; then
    git clone https://github.com/ggerganov/whisper.cpp
fi

cd /opt/altria/whisper.cpp
make -j$(nproc)

echo "=== Downloading Whisper small.en model (~142MB) ==="
bash ./models/download-ggml-model.sh small.en

echo "=== Creating integration directory ==="
mkdir -p /opt/altria/logs

echo "=== Copying integration script ==="
cp /tmp/call_triage.py /opt/altria/call_triage.py
chmod +x /opt/altria/call_triage.py

echo "=== Setting up cron job (every 5 minutes) ==="
CRON_LINE="*/5 * * * * python3 /opt/altria/call_triage.py >> /opt/altria/logs/triage.log 2>&1"
(crontab -l 2>/dev/null | grep -v call_triage; echo "$CRON_LINE") | crontab -

echo ""
echo "=== Setup complete ==="
echo "Edit /opt/altria/.env with your Claude API key then run:"
echo "  python3 /opt/altria/call_triage.py"
