#!/bin/bash

# Simple installation script for Kino

if [ "$EUID" -ne 0 ]; then
  echo "Please run as root (use sudo)"
  exit 1
fi

INSTALL_DIR="/usr/local/lib/Kino"
BIN_LINK="/usr/bin/kino"
BIN_LINK_LOCAL="/usr/local/bin/kino"
DESKTOP_FILE="/usr/share/applications/kino.desktop"

echo "Installing to $INSTALL_DIR..."

# Create directory
mkdir -p "$INSTALL_DIR"

# Copy binary and metadata using 'install' to handle running processes correctly
install -m 755 Kino "$INSTALL_DIR/"
install -m 644 assets/icon.png "$INSTALL_DIR/"

# For directory copy, we use cp but remove destination first to ensure clean update
rm -rf "$INSTALL_DIR/lib"
cp -r lib "$INSTALL_DIR/"

# Create wrapper scripts
cat <<EOF > "$BIN_LINK"
#!/bin/bash
cd "$INSTALL_DIR"
./Kino "\$@"
EOF

cp "$BIN_LINK" "$BIN_LINK_LOCAL"
chmod +x "$BIN_LINK" "$BIN_LINK_LOCAL"

# Install desktop file
cp assets/kino.desktop "$DESKTOP_FILE"
# Update path in desktop file if needed

# Update MIME database
echo "Updating MIME database..."
update-desktop-database

echo "Installation complete!"
echo "Installed version timestamp: $(date -r "$INSTALL_DIR/Kino")"
echo "You can now run 'kino' or find it in your application menu."
