#!/bin/bash
set -e

# Update MIME database
if ! update-mime-database /usr/share/mime; then
    echo "Error: Failed to update MIME database"
    exit 1
fi

# Ensure PHP is installed
PHP_BIN=$(command -v php)
if [ -z "$PHP_BIN" ]; then
    echo "Error: PHP binary not found"
    exit 1
fi

# Create symlink for ncc-cli
ENTRY_POINT="/usr/bin/ncc"
if ! cat <<EOF > "$ENTRY_POINT"
#!/bin/bash
$PHP_BIN /usr/share/ncc/ncc --ncc-cli "\$@"
EOF
then
    echo "Error: Failed to create entry point script at $ENTRY_POINT"
    exit 1
fi
chmod +x "$ENTRY_POINT"

# Initialize ncc
if ! ncc setup --default-repositories=/usr/share/ncc/default_repositories.json; then
    echo "Error: Failed to setup ncc"
    exit 1
fi