#!/bin/bash

set -e

APP_NAME="FreePassport-C-Media-Player"
APP_VERSION="0.1"

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Detect architecture
ARCH=$(uname -m)
case $ARCH in
    x86_64)
        LINUXDEPLOY_URL="https://github.com/linuxdeploy/linuxdeploy/releases/download/continuous/linuxdeploy-x86_64.AppImage"
        LINUXDEPLOY_BIN="linuxdeploy-x86_64.AppImage"
        ;;
    aarch64|arm64)
        LINUXDEPLOY_URL="https://github.com/linuxdeploy/linuxdeploy/releases/download/1-alpha-20250213-2/linuxdeploy-aarch64.AppImage"
        LINUXDEPLOY_BIN="linuxdeploy-aarch64.AppImage"
        ;;
    *)
        echo -e "${RED}Error: Unsupported architecture: $ARCH${NC}"
        echo -e "${RED}Supported architectures: x86_64, aarch64${NC}"
        exit 1
        ;;
esac

OUTPUT_NAME="${APP_NAME}-${APP_VERSION}-${ARCH}.AppImage"

rm -rf AppDir/

echo -e "${GREEN}Building ${APP_NAME} AppImage for ${ARCH}...${NC}"

# Check if binary exists
if [ ! -f "passport-c-media-player" ]; then
    echo -e "${YELLOW}Binary not found. Building first...${NC}"
    make clean
    make
fi

# Create AppDir structure
echo -e "${GREEN}Setting up AppDir...${NC}"
mkdir -p AppDir/usr/bin
mkdir -p AppDir/usr/share/applications
mkdir -p AppDir/usr/share/icons/hicolor/256x256/apps

# Copy binary
cp passport-c-media-player AppDir/usr/bin/
chmod +x AppDir/usr/bin/passport-c-media-player

# Copy bundled resources (font and logo for AppImage)
if [ -f "BigBlueTermPlusNerdFontMono-Regular.ttf" ]; then
    cp BigBlueTermPlusNerdFontMono-Regular.ttf AppDir/usr/bin/
fi
if [ -f "logo.png" ]; then
    cp logo.png AppDir/usr/bin/
fi

# Create desktop file directly in script (avoids copy conflicts)
cat > AppDir/passport-c-media-player.desktop << 'EOF'
[Desktop Entry]
Name=FreePassport-C Media Player
Comment=Turn your media library into an early 2000s cable box TV guide
Exec=passport-c-media-player
Icon=passport-c-media-player
Type=Application
Categories=AudioVideo;Player;TV;
Terminal=false
EOF

# Also copy to usr/share/applications for system integration
cp AppDir/passport-c-media-player.desktop AppDir/usr/share/applications/

# Copy and convert icon
echo -e "${GREEN}Setting up icon...${NC}"
if [ -f "logo.png" ]; then
    cp logo.png AppDir/usr/share/icons/hicolor/256x256/apps/passport-c-media-player.png
    cp logo.png AppDir/passport-c-media-player.png
else
    echo -e "${RED}Warning: logo.png not found!${NC}"
fi

# Create AppRun symlink
cd AppDir
ln -sf usr/bin/passport-c-media-player AppRun
cd ..

# Download linuxdeploy if not present
if [ ! -f "${LINUXDEPLOY_BIN}" ]; then
    echo -e "${GREEN}Downloading linuxdeploy for ${ARCH}...${NC}"
    wget -q "${LINUXDEPLOY_URL}" -O "${LINUXDEPLOY_BIN}"
    chmod +x "${LINUXDEPLOY_BIN}"
fi

# Build AppImage
echo -e "${GREEN}Creating AppImage for ${ARCH}...${NC}"
./"${LINUXDEPLOY_BIN}" \
    --appdir AppDir \
    --output appimage \
    -d AppDir/usr/share/applications/passport-c-media-player.desktop \
    -i AppDir/usr/share/icons/hicolor/256x256/apps/passport-c-media-player.png

# Rename output file
# Use wildcard to find whatever AppImage was created (handles underscore/dash variations)
for file in FreePassport-C_Media_Player-*.AppImage; do
    if [ -f "$file" ]; then
        mv "$file" "${OUTPUT_NAME}"
        break
    fi
done

if [ -f "${OUTPUT_NAME}" ]; then
    echo -e "${GREEN}Success! Created ${OUTPUT_NAME}${NC}"
    ls -lh "${OUTPUT_NAME}"
else
    echo -e "${RED}Error: AppImage creation failed!${NC}"
    exit 1
fi
