#! /bin/sh
#
# collect Healpix and healpy source and data files, 
#  before packing them up for a release (see ./pack)
#

NO_COLOR='\x1b[0m'
RED_COLOR='\x1b[31;01m'
GREEN_COLOR='\x1b[32;11m'
YELLOW_COLOR='\x1b[33;11m'
BLUE_COLOR='\x1b[34;11m'
MAGENTA_COLOR='\x1b[35;11m'
CYAN_COLOR='\x1b[36;11m'

echoBlu (){
    echo "${BLUE_COLOR}$1${NO_COLOR}"
    }

echoGrn (){
    echo "${GREEN_COLOR}$1${NO_COLOR}"
    }

echoRed (){
    echo "${RED_COLOR}$1${NO_COLOR}"
    }
#---------------

WRKDIR="/tmp"
#healpytag="1.15.2"
healpytag="1.17.3"
healpytag_git=""
#healpytag_git=${healpytag}
healpygit=0 # 1: uses GIT, 0: uses tar.gz
healpyrepo="https://github.com/healpy/healpy.git"
#healpyarchive="https://github.com/healpy/healpy/archive/" # git (maybe partial package)
#healpyarchive="https://pypi.python.org/packages/source/h/healpy/" # pypi (full package)
# simple paths pypi:
# http://pypi.python.org/simple/healpy/healpy-${healpytag}.tar.gz
# https://pypi.debian.net/healpy/healpy-${healpytag}.tar.gz
#
# healpyarchive="https://github.com/healpy/healpy/releases/download/${healpytag}/" # (partial)
healpyarchive="https://pypi.debian.net/healpy/"  # (full)
# from https://files.pythonhosted.org/packages/ec/75/8e8e546ef8f173555ce097ba98487a510f6a56764ed28ea3e6d11ad684cf/healpy-1.15.1.tar.gz to /Users/hivon/healpy
# https://pypi.org/project/healpy/1.15.2/#files
#healpyarchive_alt="https://github.com/healpy/healpy/archive/refs/tags/"
#healpyarchive_alt="https://github.com/healpy/healpy/archive/" # partial package (no cfitsio/, no healpy/src/_*.cpp)
#healpyarchive="/Users/hivon/healpy/"
##
mixed=0
if [ "$mixed" = "1" ]; then
    healpixbranch="trunk"
    healpixbranch_cxx="branches/branch_v330r807"
    healpixbranch_java=${healpixbranch}
else
    #healpixbranch="branches/branch_v320r670"  # r926 for 3.40
    healpixbranch="trunk"
    healpixbranch_cxx=${healpixbranch}
    healpixbranch_java=${healpixbranch}
fi
#healpixrepo="svn://svn.code.sf.net/p/healpix/code/" # read-only
healpixrepo="svn+ssh://hivon@svn.code.sf.net/p/healpix/code/" # read-only
#healpixrepo="https://healpix.svn.sourceforge.net/svnroot/healpix/"
#
#
current=`grep -v '#' ./Version`
version=${current}
printf "Enter version number [$current] "
read answer
[ "x$answer" != "x" ] && version="$answer"
VERSION=$version

echoBlu "Version: ${VERSION}    in ${WRKDIR}"
if [ ${healpygit} = 1 ] ; then
    echoBlu "healpy:   ${healpyrepo}   ${healpytag_git}"
else
    echoBlu "healpy:   ${healpyarchive}   ${healpytag}"
fi
echoBlu "healpix:  ${healpixrepo}${healpixbranch}"
[ ${healpixbranch_cxx}  != ${healpixbranch} ] && echo "   C++:   ${healpixrepo}${healpixbranch_cxx}"
[ ${healpixbranch_java} != ${healpixbranch} ] && echo "   java:  ${healpixrepo}${healpixbranch_java}"
printf "Do you want to proceed (y/n) [n]: "
read answer
doit=0
[ "x$answer" = "xy" ] && doit=1

if [ ${doit} = 1 ] ; then
    cd ${WRKDIR}

    \rm -rf             ${WRKDIR}/Healpix_${VERSION}
    \rm -rf             ${WRKDIR}/healpy

    cd ${WRKDIR}
    echoGrn "-------------------- HEALPix ------------------"
    echo "svn co     ${healpixrepo}${healpixbranch}               Healpix_${VERSION}"
    svn co     ${healpixrepo}${healpixbranch}               Healpix_${VERSION}  || exit
    if [ ${healpixbranch_cxx} != ${healpixbranch} ] ; then
	echoGrn "-------------------- C++ ------------------"
	\rm -rf Healpix_${VERSION}/src/cxx
	svn co ${healpixrepo}${healpixbranch_cxx}/src/cxx   Hc++_${VERSION}
	mv Hc++_${VERSION}                                  Healpix_${VERSION}/src/cxx
    fi
    if [ ${healpixbranch_java} != ${healpixbranch} ] ; then
	echoGrn "-------------------- Java ------------------"
	\rm -rf Healpix_${VERSION}/src/java
	svn co ${healpixrepo}${healpixbranch_java}/src/java Hjava_${VERSION}
	mv Hjava_${VERSION}                                 Healpix_${VERSION}/src/java
    fi

    echoGrn "---------------------- Healpy ----------------"
    if [ ${healpygit} = 1 ] ; then
	git clone ${healpyrepo}  healpy 
	cd healpy 
	git checkout ${healpytag_git} 
	git branch
	cd  healpy
	cp version.py version.py_BK
	echo '__version__ = "'"${healpytag}"'"' > version.py
	grep "${healpytag}" version.py
	cd ..
	# fills in cfitsio and healpixsubmodule
	git submodule init
	git submodule update
	cd healpixsubmodule
	\rm `find . -type f -depth 1`
	\rm -r data doc test src/C src/f90 src/idl src/java src/python
	cd ..
    else
	if [ "${healpyarchive:0:4}" = "http" ] ; then
	    hpyops="--no-check-certificate -O healpy-${healpytag}.tar.gz"
	    hpy1=${healpyarchive}healpy-${healpytag}.tar.gz # default
	    #hpy2=${healpyarchive_alt}${healpytag}.tar.gz    # alternative
	    #wget -O/dev/null -q ${hpy1} && wget ${hpy1} ${hpyops} || wget ${hpy2} ${hpyops}
	    wget ${hpy1} ${hpyops}
	else
	    echo  ${healpyarchive}healpy-${healpytag}.tar.gz
	    ls -l ${healpyarchive}healpy-${healpytag}.tar.gz
	    cp    ${healpyarchive}healpy-${healpytag}.tar.gz .
	fi
	tar -xvzpf healpy-${healpytag}.tar.gz
	mv healpy-${healpytag} healpy
    fi


    cd ${WRKDIR}
    cp -pr healpy Healpix_${VERSION}/src
    cd Healpix_${VERSION}/src/healpy

    \rm -rf .git*
    
    if [ -d healpixsubmodule/src/common_libraries ] ; then # version 1.13.0 and above
	cd  healpixsubmodule/src/common_libraries/libsharp
	echoBlu "in ${PWD}"
	for dir in c_utils libsharp pocketfft ; do
	    echoRed "Replacing ${dir}"
	    \rm -r ${dir}
	    \ln -sf ../../../../../common_libraries/libsharp/${dir} .
	    ls -l ${dir}
	done
	cd -
	cd  healpixsubmodule/src/cxx
	echoBlu "in ${PWD}"
	\ln -sf ../../../../cxx .
	for dir in Healpix_cxx cxxsupport ; do
	    echoRed "Replacing ${dir}"
	    \rm -r ${dir}
	    \ln -sf cxx/${dir} .
	    ls -l ${dir}
	done
	cd -
    fi

#     if [ -d healpixsubmodule/src/cxx/autotools ] ; then # version 1.7* and above
# 	cd  healpixsubmodule/src/cxx/autotools
# 	\ln -sf ../../../../../cxx .
# 	#for dir in Healpix_cxx c_utils cxxsupport libfftpack libsharp ; do
# 	#for dir in Healpix_cxx c_utils cxxsupport pocketfft libsharp ; do
# 	for dir in Healpix_cxx c_utils cxxsupport ; do
# 	    echoRed "Replacing ${dir}"
# 	    \rm -r ${dir}
# 	    \ln -sf cxx/${dir} .
# 	done
# 	cd -
#     elif [ -d healpixcxx ] ; then # deprecated
# 	\rm -rf healpixcxx .git*
# 	mkdir -p healpixcxx/src
# 	cd healpixcxx/src
# 	ln -s ../../../cxx .
# 	cd -
#     fi

#     if [ ! -f cfitsio/License.txt ] ; then # put by hand cfitsio source
# 	echo "Put CFITSIO source files"
# 	\rm -rf cfitsio
# 	cfitsio_tarball=cfitsio_3.36_for_healpy_1.9.x.tar.gz
# 	cp -p ~/Healpix_releases/${cfitsio_tarball} .
# 	tar -xvzpf ${cfitsio_tarball}
# 	\rm ${cfitsio_tarball}
#     fi

#     if [ ! -d healpixsubmodules/src ] ; then # put by hand healpixsubmodules
# 	echo "Put links to HEALPix/C++ source files"
# 	\rm -rf healpixsubmodules
# 	hsm_tarball=healpixsubmodule_for_healpy_1.9.x.tar.gz
# 	cp -p ~/Healpix_releases/${hsm_tarball} .
# 	tar -xvzpf ${hsm_tarball}
# 	\rm ${hsm_tarball}
#     fi

    echoBlu "--------- Discrepancy in healpy -------------"
    diff -qr ${PWD} ~/healpy/healpy-${healpytag}
    echoGrn "-------------------- Done! ------------------"
    cd ${WRKDIR}
fi

exit

