File ventoy.spec of Package ventoy

# Define architecture mapping to match Ventoy's folder naming convention
# RPM might say i686, but Ventoy folders are named 'i386'
%define ventoy_arch %{_arch}
%if "%{_arch}" == "i586" || "%{_arch}" == "i686"
%define ventoy_arch i386
%endif

Name:           ventoy
Version:        1.0.0
Release:        0
Summary:        A new bootable USB solution
License:        GPL-3.0-or-later
URL:            https://www.ventoy.net
Source0:        https://github.com/ventoy/Ventoy/releases/download/v%{version}/ventoy-%{version}-linux.tar.gz
Source1:        ventoy_icons-%{version}.tar
Source2:        ventoy.desktop
Source3:        VentoyGUI.sh
Source4:        busybox.sh

# Runtime dependencies
Requires:       dosfstools
Requires:       e2fsprogs
# Boolean dependency (works on Tumbleweed/Fedora)
Requires:       (busybox or busybox-static)

# Build dependencies
BuildRequires:  tar
BuildRequires:  ImageMagick
BuildRequires:  fdupes
BuildRequires:  desktop-file-utils
BuildRequires:  hicolor-icon-theme

# Restrict to architectures Ventoy supports
ExclusiveArch:  x86_64 i586 i686 aarch64 mips64el

%description
Ventoy is an open source tool to create bootable USB drive for ISO/WIM/IMG/VHD(x)/EFI files.
With ventoy, you don't need to format the disk over and over, you just need to copy the image files to the USB drive and boot it.

%prep
# -q: Quiet mode
# -n: Sets the directory to ventoy-%{version}
# -a 1: Automatically unpacks Source1 (the icons) after entering the directory
%setup -q -n ventoy-%{version} -a 1

# Unpack the icons (Source0)
# Assumes Source0 contains a top-level folder (e.g., ICON/) or files directly.
# We extract it here to ensure we can find the icons later.
tar -xf %{SOURCE0}

%build
# Validate the desktop file during build
desktop-file-validate %{SOURCE2}

%install
# 1. Prepare directories
mkdir -p %{buildroot}%{_libdir}/%{name}
mkdir -p %{buildroot}%{_bindir}

# 2. Copy files (everything from source dir)
cp -a * %{buildroot}%{_libdir}/%{name}/

# --- FIX 1: Clean up the accidental copy of the icons source folder ---
# cp -a * copied the 'ventoy_icons' folder too. We don't need it in the libdir.
rm -rf %{buildroot}%{_libdir}/%{name}/ventoy_icons-*

# 3. Clean up foreign architectures
# Remove binaries not for this system
find %{buildroot}%{_libdir}/%{name} -maxdepth 1 -type f -name "VentoyGUI.*" -not -name "VentoyGUI.%{ventoy_arch}" -delete
# Remove tool folders not for this system
find %{buildroot}%{_libdir}/%{name}/tool -maxdepth 1 -type d -not -name "%{ventoy_arch}" -not -name "tool" -exec rm -rf {} +

# 4. Process Binary Blobs
# Decompress
xz --decompress %{buildroot}%{_libdir}/%{name}/tool/%{ventoy_arch}/*.xz || true
# Strip (ignore errors if already stripped)
strip --strip-all %{buildroot}%{_libdir}/%{name}/tool/%{ventoy_arch}/* 2>/dev/null || true
strip --strip-all %{buildroot}%{_libdir}/%{name}/VentoyGUI.%{ventoy_arch} 2>/dev/null || true

# 5. Install BusyBox Shim
install -Dm755 %{SOURCE4} %{buildroot}%{_libdir}/%{name}/tool/busybox
for tool in ash hexdump xzcat; do
    # --- FIX 2: Point to ../busybox (up one level), not ./busybox ---
    ln -sf ../busybox %{buildroot}%{_libdir}/%{name}/tool/%{ventoy_arch}/$tool
done

# 6. Install and Configure Main Launcher
install -D -m755 %{SOURCE3} %{buildroot}%{_bindir}/VentoyGUI
# Inject the real install path
sed -i 's|^VentoyDir=.*|VentoyDir="%{_libdir}/%{name}"|' %{buildroot}%{_bindir}/VentoyGUI

# 7. Generate Wrappers for CLI tools
find %{buildroot}%{_libdir}/%{name} -maxdepth 1 -type f -name "*.sh" -executable | while read script; do
    script_basename="$(basename "$script")"
    bin_name="${script_basename%.sh}"
    
    if [ "$bin_name" = "VentoyGUI" ]; then continue; fi
    if [[ "$bin_name" != Ventoy* ]]; then bin_name="Ventoy$bin_name"; fi

    cat <<EOF > "%{buildroot}%{_bindir}/$bin_name"
#!/bin/bash
cd '%{_libdir}/%{name}'
exec ./"$script_basename" "\$@"
EOF
    chmod 755 "%{buildroot}%{_bindir}/$bin_name"
done

# 8. Install Desktop File
install -D -m644 %{SOURCE2} %{buildroot}%{_datadir}/applications/%{name}.desktop

# 9. Install Icons
# Look for icons inside the build directory (we unpack them in %prep)
find . -name "logo_*.png" | while read icon; do
    icon_ext="${icon##*.}"
    icon_size="$(identify -format "%wx%h" "$icon")"
    if [ -n "$icon_size" ]; then
        install -D -m644 "$icon" %{buildroot}%{_datadir}/icons/hicolor/${icon_size}/apps/%{name}.${icon_ext}
    fi
done

# 10. Final Permissions
chmod 644 %{buildroot}%{_libdir}/%{name}/tool/languages.json
chmod +x %{buildroot}%{_libdir}/%{name}/tool/*.sh

%fdupes -s %{buildroot}%{_prefix}

%check
# Optional: Basic check to ensure the binary exists
test -f %{buildroot}%{_libdir}/%{name}/VentoyGUI.%{ventoy_arch}

%post
%if 0%{?suse_version}
%icon_theme_cache_post
%endif

%postun
%if 0%{?suse_version}
%icon_theme_cache_postun
%endif

%files
%license README
# This wildcard captures VentoyGUI, Ventoy2Disk, VentoyWeb, etc.
%{_bindir}/Ventoy*
%{_libdir}/%{name}
%{_datadir}/applications/%{name}.desktop
%{_datadir}/icons/hicolor/*/apps/%{name}.png

%changelog
openSUSE Build Service is sponsored by