# The gcc4ti prefix.
%global prefix_gcc4ti %{_prefix}/m68k-gcc4ti/

# gcc4ti is a pile of arcane garbage and produces no useful debuginfo packages.
%define debug_package %{nil}

# Disable binary stripping.
%global __os_install_post %{nil}

# The current commit we're building from.
%global commit0 9bbe32dde3bd79b35c3071b0611287258e20f757
%global shortcommit0 %(c=%{commit0}; echo ${c:0:7})

Name:           gcc4ti
Version:        0.96

# This is a bit awkward.
# Bump the major digit to 1 on full release of 0.96 (if that ever happens)
# Bump the second digit when updating the release of 0.96b11 from e.g. git
# Bump the beta tag if a new beta of the release is released.
Release:        0.7.beta11%{?dist}

Summary:        Mature SDK for Texas Instruments 68k graphing calculators

License:        GPLv2
URL:            https://github.com/debrouxl/gcc4ti/

Source0:        https://github.com/debrouxl/gcc4ti/archive/%{commit0}.tar.gz#/gcc4ti-%{shortcommit0}.tar.gz

BuildRequires: gcc

# Requires an ADP documentation reader.
Requires: qt-assistant-adp

%description
GCC4TI is a complete and mature GPL SDK for the TI-89, TI-89T, TI-92+
and TI-V200 (collectively known as TI-68k) calculators in the C and
ASM languages.

%prep
# Some cleanup steps.
rm -rf %{buildroot}/
rm -rf gcc4ti
%setup -qn gcc4ti-%{commit0}

%build
# Run updatesrc script
cd trunk/tigcc-linux/scripts
./updatesrc
cd ../gcc4ti-0.96b11/scripts

# Set environment variables
export PREFIX_GCC4TI=%{buildroot}/%{prefix_gcc4ti}
export CC=gcc
export CFLAGS=-"Os -s -fno-exceptions -fomit-frame-pointer"
export GCC4TIHOST=""

# As it turns out I don't trust the 'install scripts to not think they
# need to install into the buildroot.

#export PREFIX_GCC4TI_PREFIX=%{prefix_gcc4ti}
#sed 's/"--prefix=$PREFIX_GCC4TI"/"--prefix=$PREFIX_GCC4TI_PREFIX"/g' -i launch.gcc

%install
# Set environment variables (again)
export PREFIX_GCC4TI=%{buildroot}/%{prefix_gcc4ti}
export CC=gcc
export CFLAGS=-"Os -s -fno-exceptions -fomit-frame-pointer"
export GCC4TIHOST=""

# Run the install steps that actually install things.
cd trunk/tigcc-linux/gcc4ti-0.96b11/scripts/
./Install_step_1
./Install_step_2
./Install_step_3
./Install_step_4
./Install_step_5

# Install documentation.
# For now, the gcc4ti docs still live in its own prefix.
cd ../
mkdir -p %{buildroot}/%{_docdir}/gcc4ti
cp README README.linux AUTHORS CHANGELOG BUGS DIRECTORIES HOWTO %{buildroot}/%{_docdir}/gcc4ti

# Fix gcc4ti docs symlink... for now, by "fix" we mean "get rid of". :(
rm %{buildroot}/%{prefix_gcc4ti}/doc/index.html
#ln %{prefix_gcc4ti}/doc/html/index.html %{buildroot}/%{prefix_gcc4ti}/doc/index.html

# Create profile.d files for the TIGCC enviroment variable.
mkdir -p %{buildroot}%{_sysconfdir}/profile.d/

# Write out a profile script for gcc4ti in bash
cat > %{buildroot}%{_sysconfdir}/profile.d/gcc4ti.sh << EOF
# Set up gcc4ti environment vars and put it on path
if [ -z "\$TIGCC" ]; then
    export TIGCC=%{prefix_gcc4ti}
    export PATH=\$PATH:\$TIGCC/bin/
fi
EOF

# Write out a profile script for gcc4ti in csh
cat > %{buildroot}%{_sysconfdir}/profile.d/gcc4ti.csh << EOF
# Set up gcc4ti environment vars and put it on path
if ! \$?TIGCC then
    setenv TIGCC %{prefix_gcc4ti}
    setenv PATH \$PATH\\:\$TIGCC/bin/
endif
EOF

# I'm not convinced this will work.
# Apparently it does!
export QA_SKIP_BUILD_ROOT=1

%files
%{prefix_gcc4ti}/
%{_sysconfdir}/profile.d/gcc4ti.sh
%{_sysconfdir}/profile.d/gcc4ti.csh
%doc %{_datadir}/doc/gcc4ti
%license trunk/tigcc-linux/gcc4ti-0.96b11/COPYING

%changelog
* Thu May 10 2018 Ben Rosser <rosser.bjr@gmail.com> - 0.96-0.7.beta11
- Rebuild for Fedora 28 release.

* Fri Nov 17 2017 Ben Rosser <rosser.bjr@gmail.com> - 0.96-0.6.beta11
- Rebuild for Fedora 27 release.

* Wed Jul 19 2017 Ben Rosser <rosser.bjr@gmail.com> - 0.96-0.5.beta11
- Rebuild for Fedora 26 release (and Mageia 6).

* Mon Nov 21 2016 Ben Rosser <rosser.bjr@gmail.com> - 0.96-0.4.beta11
- Apply some fixes from gcc4ti git.

* Tue Apr 19 2016 Ben Rosser <rosser.bjr@gmail.com> - 0.96-0.3.beta11
- Update to current gcc4ti git.

* Sat Mar 19 2016 Ben Rosser <rosser.bjr@gmail.com> - 0.96-0.2.beta11
- Disable stripping of resulting binaries.

* Fri Mar 18 2016 Ben Rosser <rosser.bjr@gmail.com> - 0.96-0.1.beta11
- Initial package.