#!/bin/bash
set -e

APP_DIR="/home/onlineconvert/htdocs/onlineconvert.cc"
VENV_DIR="$APP_DIR/venv"

echo "Updating package lists..."
sudo apt-get update

echo "Installing system dependencies..."
sudo apt-get install -y python3 python3-venv python3-pip \
imagemagick libcairo2-dev libreoffice-core libreoffice-writer \
libreoffice-calc libreoffice-impress ffmpeg p7zip-full jpegoptim \
pngquant npm redis-server poppler-utils libopengl0 libxcb-cursor0 \
potrace ghostscript wget curl supervisor \
tesseract-ocr tesseract-ocr-eng

echo "Installing Calibre..."
wget -nv -O- https://download.calibre-ebook.com/linux-installer.sh | sudo sh /dev/stdin version=7.10.0

echo "Creating virtual environment..."
python3 -m venv "$VENV_DIR"

echo "Activating virtual environment..."
source "$VENV_DIR/bin/activate"

echo "Upgrading pip and installing Python packages..."
pip install --upgrade pip wheel
pip install -r "$APP_DIR/requirements.txt"
pip install gunicorn gevent>=24.10.1

echo "Installing npm packages..."
sudo npm install -g svgo

echo "Setting up static files..."
sudo mkdir -p "$APP_DIR/static"
sudo chown -R $USER:www-data "$APP_DIR/static"
sudo chmod -R 755 "$APP_DIR/static"

mkdir -p "$APP_DIR/storage"
touch "$APP_DIR/storage/sqlite.db"

# -------------------------------
# Create start.sh
# -------------------------------
cat > "$APP_DIR/start-website.sh" <<'EOF'
#!/bin/bash
APP_DIR="/home/onlineconvert/htdocs/onlineconvert.cc"
VENV_DIR="$APP_DIR/venv"

cd "$APP_DIR"

echo "Starting worker..."
nohup "$VENV_DIR/bin/python" worker.py > worker.log 2>&1 &
echo "Worker started."
EOF

chmod +x "$APP_DIR/start-website.sh"

# -------------------------------
# Create wsgi.py
# -------------------------------
cat > "$APP_DIR/wsgi.py" <<'EOF'
import sys
import logging
logging.basicConfig(stream=sys.stderr)
from app import app as application

if __name__ == "__main__":
    application.run(host="0.0.0.0", port=6001)
EOF

# -------------------------------
# Create website.service
# -------------------------------
sudo tee /etc/systemd/system/website.service > /dev/null <<EOF
[Unit]
Description=All in one video converter
After=network.target redis-server.service

[Service]
User=root
Group=www-data
WorkingDirectory=$APP_DIR
ExecStart=$VENV_DIR/bin/gunicorn -c $APP_DIR/gunicorn.conf.py wsgi:application
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
EOF

# -------------------------------
# Create worker.service
# -------------------------------
sudo tee /etc/systemd/system/worker.service > /dev/null <<EOF
[Unit]
Description=Video converter worker
After=network.target redis-server.service

[Service]
User=root
WorkingDirectory=$APP_DIR
ExecStart=$APP_DIR/start-website.sh
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
EOF

# -------------------------------
# Enable and start services
# -------------------------------
sudo systemctl daemon-reload
sudo systemctl enable redis-server
sudo systemctl start redis-server
sudo systemctl enable worker
sudo systemctl start worker
sudo systemctl enable website
sudo systemctl start website

echo "Installation complete!"
echo "Website service status:"
sudo systemctl status website --no-pager
echo "Worker service status:"
sudo systemctl status worker --no-pager
echo "Redis service status:"
sudo systemctl status redis-server --no-pager