#!/usr/bin/make -f

TIMESTAMP ?= $(shell date -u -d "@$(SOURCE_DATE_EPOCH)" -Iseconds | cut -d+ -f1)

URL=http://cdsarc.u-strasbg.fr/ftp/I/259
endian=$(shell dpkg-architecture -q DEB_HOST_ARCH_ENDIAN)
ifeq ($(endian),little)
  otherendian=big
  othermark=01:02:03:04
else
  otherendian=little
  othermark=04:03:02:01
endif

%:
	dh $@

override_dh_auto_build-indep:
	python3 debian/cds-to-fits.py
	for i in `seq -w 7 10` ; do \
	  build-astrometry-index \
               -i tyc2.fits -o index-tycho2-$${i}.$(endian)endian.fits \
	       -d 4 -P $$i -S MAG_VT -B 0.1 -s 0 -r 1 -I 41$${i} -M; \
	done
	for i in `seq -w 11 19` ; do \
	  build-astrometry-index \
	       -i tyc2.fits -o index-tycho2-$${i}.$(endian)endian.fits \
	       -d 3 -P $$i -S MAG_VT -B 0.1 -s 0 -r 1 -I 41$${i} -M; \
	done
	for i in `seq -w 7 19` ; do \
	  python3 debian/modhead.py \
	     index-tycho2-$${i}.$(endian)endian.fits \
             1,2,3,4,5,6,7,8,9,10,11,12,13 \
	     DATE "$(TIMESTAMP)"; \
	  fits-flip-endian \
	       -i index-tycho2-$${i}.$(endian)endian.fits \
	       -o index-tycho2-$${i}.$(otherendian)endian.fits \
	       -e 1 -s 4 -e 3 -s 4 -e 4 -s 2 -e 5 -s 8 -e 6 -s 2 \
	       -e 8 -s 4 -e 9 -s 4 -e 10 -s 8 -e 11 -s 4; \
	  python3 debian/modhead.py \
	     index-tycho2-$${i}.$(otherendian)endian.fits 0,2,7\
	     ENDIAN $(othermark); \
	done

override_dh_gencontrol:
	dh_gencontrol -- -Vendian=$(endian)

get-orig-source:
	mkdir tycho2-2
	( cd tycho2-2 \
	  && curl -R -O $(URL)/{ReadMe,index.dat.gz} \
	             -O $(URL)/tyc2.dat.[00-19].gz \
	             -O $(URL)/suppl_[1-2].dat.gz \
	  && gunzip *.gz \
	  && chmod 644 * \
	  && tar cJvf ../tycho2_2.orig.tar.xz --owner=tycho2 --group=tycho2 * ) ; \
	rm -rf tycho2-2