%global iraf_version 2.16.1
%global snapshot_date 2018.11.01

Name:           iraf
Version:        %{iraf_version}
Release:        4.snapshot.%{snapshot_date}%{?dist}
Summary:        Image Reduction and Analysis Facility

License:        BSD and CDDL and MIT
URL:            https://iraf-community.github.io/
Source0:        https://github.com/iraf-community/%{name}/archive/v%{iraf_version}+%{snapshot_date}.tar.gz
Source1:        irafcl
Source2:        iraf.desktop
Source3:        iraf.svg
Source4:        iraf-precompile.cl

## upstream and upstreamable patches


## Fedora specific patches
# Use the system libraries for cfitsio and readline, not the bundled ones
Patch100:       https://github.com/lupinix/iraf/commit/a0d3a6c819e6545dbaa5556977e5fffe41cb21d8.patch#/iraf-Use-system-cfitsio.patch
# Global login.cl adapting our packaging, adapted Debian patch: Create-a-global-login.cl-in-etc.patch 
Patch101:       https://github.com/lupinix/iraf/commit/4c9e87fb6b593ce5de71a08d94b17806e42c284d.patch#/iraf-Create-a-global-login.cl-in-etc.patch
# Tell user that we have a modified Fedora build here
Patch102:       https://github.com/lupinix/iraf/commit/1cda1a4d40c919169702923bcff7d557f5af353b.patch#/iraf-Adjust-version-number-and-motd.patch
# Fedora version of Debian patch Remove-architecture-dependency-in-the-names-of-bin-subdir.patch
Patch103:       https://github.com/lupinix/iraf/commit/17b6f17e19053ea999d2b658552116c092cd64d0.patch#/iraf-Remove-architecture-dependency-in-the-names-of-bin-subdir.patch
# Be able to split out the development and noao packages, again: adapted from Debian Separate-NOAO-and-dev-packages.patch
Patch104:       https://github.com/lupinix/iraf/commit/acb101056db2b74800eff1324ff9840f640945aa.patch#/iraf-Separate-NOAO-and-dev-packages.patch
# Set proper compiler flags for IRAF's own compiler
Patch105:       https://github.com/lupinix/iraf/commit/975d2e68d0d9b11ababc5da3804b1fd2330e506d.patch#/iraf-Propagate-CFLAGS-and-debug-flag-to-xc.patch
# Skip source dependent tests
Patch106:       https://github.com/lupinix/iraf/commit/c79aa99df0591bc09c7af48dad8be5ebcaafd2ba.patch#/iraf-Skip-source-dependent-tests.patch
# Debian Write-default-iraf-host-tmp-variables-back-to-environment.patch, w have to set our %%{_libdir} properly in the environment
Patch107:       https://github.com/lupinix/iraf/commit/626ba8a7024dcad20909f441571f6b796f70a27f.patch#/iraf-Write-default-iraf-host-tmp-variables-back-to-environment.patch
# Debian Fix-defterm-imdif-and-cachedir-in-mkiraf.sh.patch
Patch108:       https://github.com/lupinix/iraf/commit/c317b2d0ea47654023266ef00753470fd0bc42c3.patch#/iraf-Fix-defterm-imdif-and-cachedir-in-mkiraf.sh.patch
# The xterm in Fedora uses xterm-256color as value for $TERM
Patch109:       https://github.com/lupinix/iraf/commit/fee3b76713223d56f9f4ac9b2d0a6a1be34dfdc0.patch#/iraf-Fix-TERM-for-Fedora-xterm.patch

# General requirements
BuildRequires:  desktop-file-utils
BuildRequires:  gcc
BuildRequires:  flex
BuildRequires:  make
# Required development libraries
BuildRequires:  cfitsio-devel
BuildRequires:  curl-devel
BuildRequires:  expat-devel
BuildRequires:  ncurses-devel
BuildRequires:  readline-devel
# We need basic perl for the tests
BuildRequires:  perl-interpreter

# IRAF provides the ability to create own tasks and packages, this
# functionality requires some basic development libraries and gcc
Requires:       gcc
Requires:       cfitsio-devel
Requires:       curl-devel
Requires:       expat-devel
Requires:       ncurses-devel
Requires:       readline-devel
# For some tasks xterm is the prefered terminal
Requires:       xterm

# No IRAF on big endian architectures, not supported by upstream
ExcludeArch:    ppc64 s390x

%description
IRAF is the "Image Reduction and Analysis Facility". The main IRAF distribution
includes a good selection of programs for general image processing and
graphics, plus a large number of programs for the reduction and analysis of
optical and IR astronomy data (the "noao" packages). 
Other external or layered packages are available for applications such as data
acquisition or handling data from other observatories and wavelength regimes
such as the Hubble Space Telescope (optical), EUVE (extreme ultra-violet),
or ROSAT and AXAF or Chandra (X-ray). These external packages are distributed
separately from the main IRAF distribution but can be easily installed.
The IRAF system also includes a complete programming environment for scientific
applications, which includes a programmable Command Language scripting facility,
the IMFORT Fortran/C programming interface, and the full SPP/VOS programming
environment in which the portable IRAF system and all applications are written. 

This package is a build of the community fork of IRAF, a project which replaced
or removed non-free codes as well as copies of bundled libraries from the
original source.

%prep
%autosetup -p 1 -n %{name}-%{iraf_version}-%{snapshot_date}

# Set proper libdir
sed -i "s,__LIBDIR__,%{buildroot}%{_libdir},g" test/run_tests
sed -i "s,__LIBDIR__,%{_libdir},g" unix/hlib/f77.sh
sed -i "s,__LIBDIR__,%{_libdir},g" unix/hlib/mkiraf.sh
sed -i "s,__LIBDIR__,%{_libdir},g" unix/os/zgtenv.c
sed -i "s,__LIBDIR__,%{_libdir},g" %{SOURCE1}
# Specify that this is a Fedora build of IRAF
sed "s/__VERSION__/%{iraf_version}+%{snapshot_date}/" -i unix/hlib/zzsetenv.def
sed "s/__DATE__/$(date -uR)/g" -i unix/hlib/zzsetenv.def

%build
export iraf=$(pwd)/
export host=${iraf}/unix/
BUILD_TMP=${iraf}/irafbuild
export IRAFARCH=$(sh ./unix/hlib/irafarch.sh)
export PATH=${BUILD_TMP}/bin:${PATH}
mkdir -p ${BUILD_TMP}/bin 
./install --term xterm \
	--bindir ${BUILD_TMP}/bin \
	--cache ${BUILD_TMP}/cache \
	--imdir ${BUILD_TMP}/imdir \
	--root ${iraf} < /dev/null || true
cp unix/f2c/libf2c/f2c.h include/

make ${IRAFARCH}
bin=${iraf}/bin/ noaobin=${iraf}/noao/bin/ make sysgen
bin=${iraf}/bin/ noaobin=${iraf}/noao/bin/ bin/cl.e -f %{SOURCE4}

%check
export iraf=$(pwd)/
export host=${iraf}/unix/
BUILD_TMP=${iraf}/irafbuild
export IRAFARCH=$(sh ./unix/hlib/irafarch.sh)
export PATH=${BUILD_TMP}/bin:${PATH}
./test/run_tests

%install
# IRAF has no proper installation routine, we have to copy stuff manually...
mkdir -p %{buildroot}%{_bindir}
mkdir -p %{buildroot}%{_includedir}
mkdir -p %{buildroot}%{_sysconfdir}/iraf
mkdir -p %{buildroot}%{_libdir}/iraf/{bin,dev,extern,lib,local,math,noao,pkg,sys,unix/{bin,hlib}}
mkdir -p %{buildroot}%{_libdir}/iraf/pkg/{bench,cl,dataio,dbms,ecl,images,language,lists,obsolete,plot,proto,system,utilities,xtools}
mkdir -p %{buildroot}%{_libdir}/iraf/noao/{artdata,astcat,astutil,bin,digiphot,imred,lib,mtlocal,nproto,obsutil,onedspec/scombine,rv,twodspec/{apextract,longslit}}
mkdir -p %{buildroot}%{_libdir}/iraf/noao/digiphot/{apphot,daophot,photcal,ptools}
mkdir -p %{buildroot}%{_libdir}/iraf/noao/imred/{bias,ccdred,crutil,dtoi,generic,irred,quadred/{ccdproc,quad}}
# Binaries
install -D -m 755 bin/*.e %{buildroot}%{_libdir}/iraf/bin
install -D -m 755 unix/bin/*.e %{buildroot}%{_libdir}/iraf/unix/bin
# Libraries
install -D -m 755 bin/lib*.a %{buildroot}%{_libdir}/iraf/bin
install -D -m 755 bin/libmain.o %{buildroot}%{_libdir}/iraf/bin
install -D -m 755 unix/bin/lib*.a %{buildroot}%{_libdir}/iraf/unix/bin
# Header
cp -r include %{buildroot}%{_libdir}/iraf/
cp lib/*.{h,inc} %{buildroot}%{_libdir}/iraf/lib
cp -r lib/{math,pkg,sysruk.x} %{buildroot}%{_libdir}/iraf/lib
# hlib
cp unix/hlib/*.{h,inc,sf} %{buildroot}%{_libdir}/iraf/unix/hlib
cp unix/hlib/f77.sh %{buildroot}%{_libdir}/iraf/unix/hlib
cp -r unix/hlib/libc %{buildroot}%{_libdir}/iraf/unix/hlib
# NOAO files
install -D -m 755 noao/bin/*.e %{buildroot}%{_libdir}/iraf/noao/bin
install -D -m 755 noao/bin/lib*.a %{buildroot}%{_libdir}/iraf/noao/bin
cp noao/lib/*.{h,inc} %{buildroot}%{_libdir}/iraf/noao/lib

# libpkg.a files
for f in $(find . -name libpkg.a) ; do \
	install -D -m 755 $f %{buildroot}%{_libdir}/iraf/$f ; \
done

for f in $(find . -name \*.hlp \
            -o -name \*.hd \
            -o -name \*.men \
            -o -name \*.cl \
            -o -name \*.par \
            -o -name \*.key \
            -o -name \*.def \
            -o -name \*.dat \
            -o -name \*.gui \
            -o -name \*.pkg \
            -o -name \*.mip \
            -o -name \*.fits \
	    -o -path ./dev/\* | \
	    cut -c3-) \
            unix/hlib/motd* \
            lib/syserrmsg lib/syshelpdir lib/scr/help.html ; do \
install -p -D -m 644 $f %{buildroot}%{_libdir}/iraf/$f ; \
done

> %{buildroot}%{_libdir}/iraf/extern/.zzsetenv.def
mv %{buildroot}%{_libdir}/iraf/unix/hlib/login.cl %{buildroot}%{_sysconfdir}/iraf/
mv %{buildroot}%{_libdir}/iraf/unix/hlib/motd %{buildroot}%{_sysconfdir}/iraf/motd
mv %{buildroot}%{_libdir}/iraf/dev/irafhosts %{buildroot}%{_sysconfdir}/iraf/
rm -f %{buildroot}%{_libdir}/iraf/dev/hosts %{buildroot}%{_libdir}/iraf/dev/uhosts
rm -rf %{buildroot}%{_libdir}/iraf/unix/f2c
echo '# HOSTS -- IRAF local network host table.' > %{buildroot}%{_sysconfdir}/iraf/hosts
for f in unix/hlib/util.sh \
         unix/hlib/mkiraf.sh unix/hlib/mkmlist.sh \
         unix/hlib/irafarch.sh ; do \
  install -p -D -m 755 $f %{buildroot}%{_libdir}/iraf/$f ; \
done

ln -sf %{_libdir}/iraf/unix/hlib/extern.pkg %{buildroot}%{_sysconfdir}/iraf/extern.pkg
ln -sf %{_libdir}/iraf/unix/hlib/hosts %{buildroot}%{_sysconfdir}/iraf/hosts
ln -sf %{_libdir}/iraf/dev/irafhosts %{buildroot}%{_sysconfdir}/iraf/irafhosts
ln -sf %{_libdir}/iraf/unix/bin/sgidispatch.e %{buildroot}%{_bindir}/sgidispatch
ln -sf %{_libdir}/iraf/unix/bin/generic.e %{buildroot}%{_bindir}/generic
ln -sf %{_libdir}/iraf/unix/bin/mkpkg.e %{buildroot}%{_bindir}/mkpkg
ln -sf %{_libdir}/iraf/unix/bin/xc.e %{buildroot}%{_bindir}/xc
ln -sf %{_libdir}/iraf/unix/bin/xyacc.e %{buildroot}%{_bindir}/xyacc
ln -sf %{_libdir}/iraf/unix/hlib/libc/iraf.h %{buildroot}%{_includedir}/iraf.h

# Install launcher script, desktop file and icon
desktop-file-install --dir=%{buildroot}%{_datadir}/applications %{SOURCE2}
install -D -m 755 %{SOURCE1} %{buildroot}%{_bindir}
install -D -m 644 %{SOURCE3} %{buildroot}%{_datadir}/pixmaps/iraf.svg

%files
%license COPYRIGHT doc/LICENSES/*
%{_bindir}/*
%{_datadir}/applications/iraf.desktop
%{_datadir}/pixmaps/iraf.svg
%{_includedir}/iraf.h
%{_libdir}/iraf
%{_sysconfdir}/iraf

%changelog
* Fri May 03 2019 Christian Dersch <christian.dersch@physik.uni-marburg.de> - 2.16.1-4.snapshot.2018.11.01
- Include snapshot version in prompt

* Thu May 02 2019 Christian Dersch <christian.dersch@physik.uni-marburg.de> - 2.16.1-3.snapshot.2018.11.01
- initial packaging effort