#!/bin/bash

# Define your Jellyfin server URL and API key
JELLYFIN_URL="http://127.0.0.1:8096"
API_KEY="API KEY"

# Run journalctl command and search for the specified error messages
error_message_1="database disk image is malformed"
error_message_2="database or disk is full"

error_found_1=$(journalctl -u jellyfin --since "1 hour ago" | grep "$error_message_1")
error_found_2=$(journalctl -u jellyfin --since "1 hour ago" | grep "$error_message_2")

# If either error message is found, delete the database file and trigger library scan
if [ -n "$error_found_1" ] || [ -n "$error_found_2" ]; then
    echo "Error found in journal, deleting database file..."
    # Stop Jellyfin
    sudo systemctl stop jellyfin
    # Remove bad DB
    rm -f /var/lib/jellyfin/data/library.db
    # Replace bad DB
    cp "/path/to/backups/Jellyfin/library.db" "/var/lib/jellyfin/data/library.db"
    # Fix Permissions
    /usr/bin/chown jellyfin:jellyfin "/var/lib/jellyfin/data/library.db"
    echo "Database file replaced with backup."

    # Start Jellyfin
    sudo systemctl start jellyfin
    sleep 30

    # Trigger library scan
    echo "Triggering library scan..."
    curl -X POST \
        -H "Content-Type: application/json" \
        -H "X-Emby-Token: $API_KEY" \
        -d '{"Recursive":true}' \
        "$JELLYFIN_URL/library/refresh"
    echo "Library scan triggered."
    
    # Send Telegram message to admin
    curl -s -X POST -d "chat_id=CHAT ID" -d text="Jellyfin library.db had issues today. Check the logs!" https://api.telegram.org/botBOT:TOKEN/sendMessage
else
    echo "No error found in journal."
fi

greenecho "Double checking the DB integrity"
    
# Run the SQLite integrity check and capture the output
output=$(sudo sqlite3 /var/lib/jellyfin/data/library.db "PRAGMA integrity_check;")

# Check if the output indicates that the database is OK
if [[ "$output" != "ok" ]]; then

    echo "Database integrity check failed: $output"
    # Stop Jellyfin
    sudo systemctl stop jellyfin
    yellowecho "Waiting 60 seconds for jellyfin to stop"
    sleep 60
    # Remove bad DB
    rm -f /var/lib/jellyfin/data/library.db
    rm -f /var/lib/jellyfin/data/library.db*
    # Replace bad DB
    cp "/path/to/backups/Jellyfin/library.db.master" "/var/lib/jellyfin/data/library.db"
    # Fix Permissions
    /usr/bin/chown jellyfin:jellyfin "/var/lib/jellyfin/data/library.db"
    /usr/bin/chmod 600 "/var/lib/jellyfin/data/library.db"
    greenecho "Database file replaced with backup."
    greenecho "Starting Jellyfin."
    yellowecho "Waiting 60 seconds so JF can start."
    # Start Jellyfin
    sudo systemctl start jellyfin
    sleep 60
else
    
    greenecho "Database integrity is OK."
fi


#Even if no DB curruption, update the live TV, why not?
# Trigger Live TV / M3U scan
echo "Triggering Live TV / M3U scan..."
curl -X POST \
    -H "Content-Type: application/json" \
    -H "X-Emby-Token: $API_KEY" \
    "$JELLYFIN_URL/ScheduledTasks/Running/bea9b218c97bbf98c5dc1303bdb9a0ca"
echo "Live TV / M3U scan triggered."
 
exit 0