#! /bin/sh
#
#=============================
#  script to pack a release
#
#  cd /tmp; svn co  [-r BRANCH] https://svn.code.sf.net/p/healpix/code/trunk Healpix_[VERSION]
#   or
#  ./getit
#
#  cd /tmp/Healpix_[VERSION]
#  ./pack
#
#=============================

. hpxconfig_functions.sh

updateVersion () {
  # date string
  reldate=`date +%Y-%m-%d`  # eg 2009-08-31
  shortdate=`date +%Y%b%d`  # eg 2009Jul31

  # temporary file
  tmpfile="./tmpfile"

  # versions number
  current=`grep -v '#' ./Version`
  version=${current}
  echoLn "Enter version number [$current] "
  read answer
  [ "x$answer" != "x" ] && version="$answer"
  #verstex=`echo $version | sed "s|\.|\\\\\\_|g"`
  verstex=`echo $version | sed "s|\.|\\\\\\\\\\\\\\\\\\\\\\\\\\\_|g"`
  echo $version $verstex $reldate

  # update configure script
  infile='./configure'
  echoBlu "editing ${infile}"
  cat $infile |\
      sed "s|^HPXVERSION=.*|HPXVERSION=${version}|g" >\
      $tmpfile
  mv $tmpfile $infile
  chmod u+x ${infile}

  # update INSTALL
  infile='./INSTALL'
  echoBlu "editing ${infile}"
  cat $infile |\
      sed "s|_HPV_|$version|g" >\
      $tmpfile
  mv $tmpfile $infile

  # update Makefile.in
  infile='./Makefile.in'
  echoBlu "editing ${infile}"
  cat $infile |\
      sed "s|_HPV_|$version|g" >\
      $tmpfile
  mv $tmpfile $infile

  # update main directory Version file
  infile='./Version'
  echoBlu "editing ${infile}"
  cat $infile |\
      sed "s|$current|$version|g" >\
      $tmpfile
  mv $tmpfile $infile


  # update IDL init_healpix file
  infile='./src/idl/misc/init_healpix.pro'
  echoBlu "editing ${infile}"
  cat $infile |\
      sed "s|version =.*$|version = '$version'|g" |\
      sed "s|date =.*$|date = '$reldate'|g" >\
      $tmpfile
  mv $tmpfile $infile

  # update doc/Tex/hpxversion.tex file
  infile='./doc/Tex/hpxversion.tex'
  echoBlu "editing ${infile}"
  cat $infile |\
      sed "s|newcommand{\\\\hpxversion}.*$|newcommand{\\\\hpxversion}{$version}|g" |\
      sed "s|newcommand{\\\\hpxverstex}.*$|newcommand{\\\\hpxverstex}{$verstex}|g" >\
      $tmpfile
  mv $tmpfile $infile
  cat $infile

  # update F90 healpix_types file (2009-07-01)
  infile='./src/f90/mod/healpix_types.F90'
  echoBlu "editing ${infile}"
  cat $infile |\
      sed "s|healpix_version =.*$|healpix_version = '$version'|g" >\
      $tmpfile
  mv $tmpfile $infile

  # update sourceforge URL of code sources
  infile='doc/TeX/healpix_src_url.tex'
  echoBlu "editing ${infile}"
  cat $infile |\
      sed "s|/HEAD/|/$revision/|g" >\
      $tmpfile
  mv $tmpfile $infile

  # update C++ code version
  infile='src/cxx/cxxsupport/announce.cc'
  echoBlu "editing ${infile}"
  cat $infile | sed "s|#define VERSION .*$|#define VERSION \"$version\"|g" > $tmpfile
  #cat $infile | sed "s|string version =.*$|string version =\"$version\";|g" > $tmpfile
  mv  $tmpfile $infile
  grep VERSION $infile
#   infile='src/cxx/autotools/configure.ac'
  infile='src/cxx/configure.ac'
#   echoBlu "editing ${infile}" #skip edition of configure.ac for 3.81
#   cat $infile | sed "s|AC_INIT.*|AC_INIT([healpix_cxx], [$version])|g" > $tmpfile
#   mv $tmpfile $infile
  grep VERSION $infile

  infile='src/cxx/README.compilation'
  echoBlu "editing ${infile}"
  cat $infile | sed "s|^Starting with release ?.??,|Starting with release 3.30,|" > $tmpfile
  mv  $tmpfile $infile
  grep release $infile

  # update C++ Doxygen files
  echoBlu "editing src/cxx/docsrc/*.dox files"
  for p in src/cxx/docsrc/*.dox ; do
      cat $p |\
	  sed "s|PROJECT_NUMBER.*$|PROJECT_NUMBER         = $version|g" > $tmpfile
      mv $tmpfile $p
  done
  grep PROJECT_NUMBER src/cxx/docsrc/*.dox

  # update test/*.par files (2013-09)
  echo "editing  test/*.par  files"
  inlist=`find test -name \*.par`
  outstring="# HEALPix $version"
  suff="bk"
  for file in $inlist ; do
      ls -l $file
      sed -i ${suff} "s|^# HEALPix.*$|$outstring|" ${file}
      sed -i ${suff} "s|^# Healpix.*$|$outstring|" ${file}
      grep "$outstring" $file
      \rm ${file}${suff}
  done
  # run ~/healpix_private/extra/scripts/update_examples.sh

  # update test/README file (2014-01)
  file='test/README'
  echoBlu "editing  $file  file"
  outstring="test data for HEALPix $version"
  sed -i ${suff} "s|test data for HEALPix.*$|$outstring|" ${file}
  grep "$outstring" $file
  \rm ${file}${suff}

}


gettingHealpy(){
    HPY_VERSION=1.12.5
    hpy_repo="https://pypi.python.org/packages/source/h/healpy"
    wrkdir=/tmp
    cd ${wrkdir}

    wget ${hpy_repo}/healpy-${HPY_VERSION}.tar.gz --no-check-certificate -O healpy-${HPY_VERSION}.tar.gz

    tar -xvzpf healpy-${HPY_VERSION}.tar.gz
    \rm -rf healpy
    mv healpy-${HPY_VERSION} healpy

    cd ${wrkdir}/healpy

    # filein='setup.py' # done in ./configure
    # fileout='setup2.py'
    # cat ${filein} | sed "s|'--disable-shared',|'--disable-shared', '--disable-dependency-tracking',|g" > ${fileout}
    # grep -H 'disable' ${filein}
    # grep -H 'disable' ${fileout}

    cd healpixsubmodule/src/cxx/autotools
    # chmod ugo+x install-sh # done in 1.9.0

    # replace hard sources by links to C++ directory, if not already done
    if [ ! -s cxx ] ; then
	\ln -sf ../../../../../cxx .
	for dir in Healpix_cxx c_utils cxxsupport libfftpack libsharp ; do
	    echo "Creating link for ${dir}"
	    \rm -r ${dir}
	    \ln -sf cxx/${dir} .
	done
    fi

    cd ${here}
    mv ${wrkdir}/healpy src

}

#=======================================

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}"
    }
#---------------

tstart=`date`
revision=`svnversion . | sed "s|[A-Z]*||g"`
echo "Revision=${revision}"
setTopDefaults
echo "This script will destroy several files and subdirectories"
echoLn "Are you sure you want to proceed ? (no/yes) [no] "
read answer
if [ "$answer" != "yes" ]; then
    echo "Exiting."
    exit 0
fi

here=`\pwd`

do_split=0
# echo "Do you want to split the produced tar files (for HFI DPC)"
# echoLn "[NO/yes]                       "
# read answer
# [ "$answer" == "yes" ] && do_split=1


#echoGrn "getting healpy (python) directory"
#gettingHealpy

echoRed "removing CVS directories..."
find . -type d -name CVS -exec \rm -rf {} \;

echoRed "removing SVN directories..."
find . -type d -name .svn -exec \rm -rf {} \;

OS=`uname -s`
updateVersion
sleep 10

mkdir -p doc/html
mkdir -p doc/pdf
mkdir -p doc/epub

echoGrn "installing java Documentation"
cd src/java
ant docs || exit
#cp -pr healpixdocs ${here}/doc/html/java
cp -pr doc ${here}/doc/html/java || exit
\rm -r dist reports build # re-instated 2010-02-22
\rm -r META-INF
ant clean
cd $here
#cp -pr src/java/healpixdocs doc/html/java
#\rm -r src/java/healpixdocs/* # re-instated 2010-02-22

echoRed "in libsharp..."  # Healpix 3.60 and more
cd src/common_libraries/libsharp
autoreconf -i
cd $here

echoRed "cleaning up C++..."
cd src/cxx
autoreconf -i  # Healpix 3.60 and more
cd docsrc
doxygen cxxsupport.dox
mv htmldoc cxxsupport
doxygen Healpix_cxx.dox
mv htmldoc Healpix_cxx
\rm *.dox
\rm *.tag # ??? double check ???
\rm footer.html
ln -s index_code.html index_cxx.html
ln -s index_code.html index_cxx.htm
cd $here
mv    src/cxx/docsrc/* doc/html
rmdir src/cxx/docsrc
# export HEALPIX_TARGET=generic_gcc # Healpix 3.50 and before
# make doc || exit
# cd $here
# # rm -r src/cxx/doc
# mv src/cxx/doc/index.html    src/cxx/doc/index_cxx.htm
# cp src/cxx/doc/index_cxx.htm src/cxx/doc/index_cxx.html
# mv src/cxx/doc/*             doc/html
# rmdir src/cxx/doc
# cd src/cxx
# [ -r Makefile ] && make tidy
# \rm -rf ${HEALPIX_TARGET}
# \rm -rf build.${HEALPIX_TARGET}
#\rm -rf config/config.healpy # remove config.healpy (2011-01-31)
cd $here

oldwebsite="public_html_healpix"
newwebsite="healpix-dynamicPages"
docautodir=${newwebsite}

echoRed "remove old and new web sites"
[ -d ${oldwebsite} ]  && \rm -rf ${oldwebsite}
[ -d  ${newwebsite} ] && \rm -rf ${newwebsite}

echoRed "removing change logs..."
find . -name ChangeLog -exec \rm -f {} \;

echoRed "generating documentations"
# recreate directory receiving doc from doc/TeX/Makefile
mkdir -p ${docautodir}/pdf
mkdir -p ${docautodir}/html
mkdir -p ${docautodir}/epub
\rm -f ${docautodir}/pdf/*
\rm -f ${docautodir}/html/*
\rm -f ${docautodir}/epub/*
cd doc/TeX
ln -s ../../${docautodir}/pdf  .
ln -s ../../${docautodir}/html .
ln -s ../../${docautodir}/epub .
make crossref || exit
make crossref || exit
make crosshtml > /dev/null || exit
make epub || exit
cd $here

echoGrn "dealing with Ximview doc"
mkdir -p doc/html/ximview
cp -p src/idl/ximview/docs/* doc/html/ximview/


echoRed "removing doc/TeX and doc/*.txt..."
\rm -rf doc/TeX
\rm -f doc/*.txt
mv ${docautodir}/html/* doc/html
mv ${docautodir}/pdf/*  doc/pdf
mv ${docautodir}/epub/H* doc/epub
\rm -rf ${docautodir}/
\rm -rf DONE.txt TODO Bugs.txt
\rm -rf include bin lib

echoRed "copy favicons material"
mkdir -p doc/html/images/favicons
cp -p doc/www/htdocs/images/favicons/* doc/html/images/favicons/

echoRed "removing web site material ..."
\rm -rf doc/www

echoRed "removing non-releasable programs..."
for p in quad_ring; do
    echo "  $p..."
    \rm -rf src/f90/$p
    sed 's/'$p' *//' Makefile.in > Makefile.save
    mv Makefile.save Makefile.in
done
[ -d extra ] && \rm -rf extra

# # update copyright date in header
# cd ${here}
# updateCopyrightDate
# cd ${here}
# updateHEALPixWebSite
# cd ${here}
# cf : hand_editions.sh

p='gpl_header'
echoRed "removing $p directory..."
[ -d ${p} ] && \rm -rf ${p}

echoRed "removing C++ make_release..."
\rm -rf src/cxx/autotools/make_release
\rm -rf src/cxx/make_release # 2014-12-04

echoRed "removing java make_release..."
\rm -rf src/java/make_release

echoRed "removing C make_release..."
\rm -rf src/C/make_release


for p in getit hand_editions.sh; do
    echo "removing ${p} ..."
    \rm -f $p
done

echoRed "removing myself..."
\rm -f $0

echoBlu "--------------------"
echoBlu $version $reldate
echoBlu "--------------------"

relsuf="${version}_${shortdate}"
tar_package="Healpix_${relsuf}.tar.gz"
zip_package="Healpix_${relsuf}.zip"
rnfile="Healpix_${relsuf}.ReleaseNotes.txt"
#tar_opts="-cvzpf"
tar_opts="-czpf"
zip_opts="-r"
echoBlu "creating tar package ../${tar_package}"
cd ..
\rm -f ${tar_package}
tar ${tar_opts} ${tar_package} `basename ${here}`
cd ${here}

echoBlu "creating zip package ../${zip_package}"
cd ..
\rm -f ${zip_package}
zip ${zip_opts} ${zip_package} `basename ${here}`
cd ${here}

if [ $do_split == 1 ] ; then
    echo "splitting tar file"
    tar ${tar_opts} "../Hpx_java.tar.gz" src/java
    \rm -rf src/java
    tar ${tar_opts} "../Hpx_test.tar.gz" test
    \rm -rf test
    tar ${tar_opts} "../Hpx_doc.tar.gz"  doc
    \rm -rf doc
    tar ${tar_opts} "../Hpx_data.tar.gz" data
    \rm -rf data
    
    cd ..
    tar ${tar_opts} "Healpix_src_${relsuf}.tar.gz" `basename ${here}`
    cd ${here}
fi

cd ..
\rm -f ${rnfile}
echo "https://sourceforge.net/projects/healpix"  > ${rnfile}

allsums ${tar_package} >> ${rnfile}
allsums ${zip_package} >> ${rnfile}

echo "sources: https://healpix.sourceforge.io/src/${version}/" >> ${rnfile}
echo "SVN url: svn://svn.code.sf.net/p/healpix/code/branches/" >> ${rnfile}
echo "SVN revision: ${revision}" >> ${rnfile}
cat $rnfile
cd ${here}
tend=`date`
echo "$tstart ... $tend"

exit

