# Fund TIC-80 development:
# <https://github.com/sponsors/nesbox>
# Buy TIC-80 Pro binaries from upstream:
# <https://nesbox.itch.io/tic80>

# Build non-pro by default
# Use `--with pro` to build Pro version
%bcond pro 0

# Upstream version as used in the tarball name
%global upver 1.1.2668_68b94ee

# Set the build dir instead of %%_vpath_builddir (i.e. redhat-linux-build)
# Upstream source includes this dir with tons of stuff already in it...?
%global __cmake_builddir build

Name:           tic80
# ~ for pre-release (sorts lower than without ~)
Version:        %{upver}~dev
Release:        1%{?dist}
Summary:        TIC-80 fantasy computer for tiny games
License:        MIT
URL:            https://tic80.com

# Manually prepare source tarball using ./source
# Too many submodules to bother unbundling
Source0:        %{name}-%{upver}.tar.gz
Source1:        source

BuildRequires:  cmake
BuildRequires:  gcc-c++
BuildRequires:  libglvnd-devel
BuildRequires:  pkgconfig(glut)
BuildRequires:  pkgconfig(xext)
# Audio - works fine with only pulseaudio, but include the rest?
BuildRequires:  pkgconfig(alsa)
BuildRequires:  pkgconfig(libpulse-simple)
BuildRequires:  pkgconfig(libpipewire-0.3)
BuildRequires:  pkgconfig(jack)
# Ruby support
BuildRequires:  ruby
BuildRequires:  rubygem-rake
# Wayland
BuildRequires:  pkgconfig(wayland-client)
BuildRequires:  pkgconfig(wayland-cursor)
BuildRequires:  pkgconfig(wayland-egl)
BuildRequires:  pkgconfig(xkbcommon)
# Pretty sure upstream build docs[1] are incorrect for various distros
# (either missing deps, extra unneeded deps, or outdated).
# While the program works in my brief testing (on X11), I'm not sure
# if any features are disabled or broken due to missing deps.
# See also AUR PKGBUILD[2] and FreeBSD Port[3][4].
# [1] https://github.com/nesbox/TIC-80#build-instructions
# [2] https://aur.archlinux.org/cgit/aur.git/tree/PKGBUILD?h=tic-80-git
# [3] https://github.com/nesbox/TIC-80/issues/2189
# [4] https://www.freshports.org/emulators/tic-80

BuildRequires:  desktop-file-utils


%description
TIC-80 is a free and open source fantasy computer for making, playing,
and sharing tiny games. It has built-in tools for development:
code, sprites, maps, sound editors, and the command line,
which is enough to create a mini retro game.

Games are packaged into a cartridge file, which can be easily distributed
and played on all popular platforms.

To make a retro styled game, the whole process of creation and execution
takes place under some technical limitations: 240x136 pixel display,
16 color palette, 256 8x8 color sprites, 4 channel sound, etc.

Help fund TIC-80 development: <https://github.com/sponsors/nesbox>


%package      extras
Summary:      Extra tools and demo carts for TIC-80
Requires:     %{name} = %{version}-%{release}
Supplements:  %{name} = %{version}-%{release}
%description  extras
Includes various tools, demo and benchmark carts,
the standalone SDL player, and libretro core.
Please consult upstream for usage as there are no docs.


%prep
%autosetup -n %{name}-%{upver}


%build
# Build fails near the end with no error if BUILD_SHARED_LIBS is ON
%cmake \
	-DBUILD_PRO:BOOL=%{?with_pro:ON}%{!?with_pro:OFF} \
	-DBUILD_SDLGPU:BOOL=ON \
	-DBUILD_DEMO_CARTS:BOOL=ON \
	-DBUILD_PLAYER:BOOL=ON \
	-DBUILD_LIBRETRO=ON \
	-DBUILD_WITH_JANET:BOOL=ON \
	-DBUILD_WITH_MRUBY:BOOL=ON \
	-DBUILD_SHARED_LIBS:BOOL=OFF

# Don't use %%cmake_build and %%cmake_install macros
# Haven't bothered to figure out why they don't work
pushd build
%make_build
popd


%install
pushd build
%make_install
popd

# Move icon to proper location
mkdir -p %{buildroot}%{_datadir}/icons/hicolor/256x256/apps
mv %{buildroot}%{_datadir}/icons/%{name}.png \
	%{buildroot}%{_datadir}/icons/hicolor/256x256/apps/%{name}.png

# Move mime xml file to proper location
mkdir -p %{buildroot}%{_datadir}/mime/packages
mv %{buildroot}%{_datadir}/applications/%{name}.xml \
	%{buildroot}%{_datadir}/mime/packages/%{name}.xml

# Install extra tools and standalone player
# <https://github.com/nesbox/TIC-80/tree/68b94ee596e1ac218b8b9685fd0485c7ee8d2f18/build/tools>
# <https://github.com/nesbox/TIC-80/blob/68b94ee596e1ac218b8b9685fd0485c7ee8d2f18/src/system/sdl/player.c>
install -Dpm0755 -t %{buildroot}%{_bindir} \
	build/bin/{bin2txt,cart2prj,prj2cart,wasmp2cart,xplode,player-sdl}

# Install demo carts
# (expected 32 files excluding config.tic, many named *demo.tic or *mark.tic)
install -Dpm0644 -t %{buildroot}%{_datadir}/%{name}/demos \
	build/*.tic
rm %{buildroot}%{_datadir}/%{name}/demos/config.tic

# Install libretro core to render TIC-80 carts
# <https://github.com/nesbox/TIC-80/tree/68b94ee596e1ac218b8b9685fd0485c7ee8d2f18/src/system/libretro>
install -Dpm0755 -t %{buildroot}%{_libdir} \
	build/lib/tic80_libretro.so

%check
desktop-file-validate %{buildroot}%{_datadir}/applications/%{name}.desktop


%files
%license LICENSE
%doc README.md
%{_bindir}/%{name}
%{_datadir}/applications/%{name}.desktop
%{_datadir}/icons/hicolor/256x256/apps/%{name}.png
%{_datadir}/mime/packages/%{name}.xml

%files extras
%license LICENSE
%{_bindir}/bin2txt
%{_bindir}/cart2prj
%{_bindir}/prj2cart
%{_bindir}/wasmp2cart
%{_bindir}/xplode
%{_bindir}/player-sdl
%{_datadir}/%{name}/
%{_libdir}/tic80_libretro.so


%changelog
* Sat Jul 22 2023 Justin Koh <j@ustink.org> - 1.1.2668_68b94ee~dev-1
- Initial package