#!/bin/bash
#  Copyright (C) 2014,2017-2019, 2021, 2023, 2025
#  Smithsonian Astrophysical Observatory
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#


tool="install_marx"
ver="24 February 2025"

#
# Version setup, paths, filenames
#
    
if test x"$ASCDS_INSTALL" == x
then
  echo "# $tool ($ver): ERROR: Please setup for CIAO first" 1>&2
  exit 1
fi






# Find the newest marx version on server

# TODO: why not just go ahead and use python to download it instead
# of playing the wget|curl|lwp-download game?

read -d '' pythonscript << EOF
from ftplib import FTP
from packaging import version
import re

pattern = re.compile('marx-dist-[0-9.]+\.tar\.gz$')

ftp = FTP('space.mit.edu')
ftp.login()
ftp.cwd('pub/cxc/marx')
marxdir = max([version.parse(n) for n in ftp.nlst() if n.startswith('v')])
ftp.cwd('v'+str(marxdir))
pattern = re.compile('marx-dist-(?P<marxver>[0-9.]+)\.tar\.gz$')
ll = [pattern.match(n).group('marxver') for n in ftp.nlst() if pattern.match(n)]
marxver = max([version.parse(n) for n in ll])
print("v{} {}".format(marxdir, marxver))
EOF

marxdirversion=(`python -c "$pythonscript"`)

marx_ftp=ftp://space.mit.edu/pub/cxc/marx/${marxdirversion[0]}
marx_ver=${marxdirversion[1]}
marx_file=marx-dist-${marx_ver}.tar.gz

#
# Look for wget, failing that try lwp-download, else error
#


#
# Parse command line arguments
#
if test $# -eq 0
then
    top=$ASCDS_INSTALL
else 
  if test $# -eq 1
  then
    top=$1
  else
    echo "Usage: ${tool} [install_dir]" 1>&2 
    exit 1
  fi
fi

# Need to use full/absolute path names
top=`python -c "import os; print(os.path.abspath('$top'))"`

echo "Installing MARX ${marx_ver} into ${top}"
echo ""
echo "If this is not where you want it installed, please"
echo "press Control+C to exit and specify the directory."
echo ""
echo "Press Enter to continue"
read foo



mkdir -p $top 
if test $? -ne 0
then
  exit 1
fi


#
# Look for wget or failing that lwp-download
#

cd "$top"
wget -O index.html ftp://space.mit.edu/pub/cxc/marx/ > /dev/null 2>&1 
if test $? -eq 0
then
  /bin/rm index.html
  WGET="wget"
else
  lwp-download ftp://space.mit.edu/pub/cxc/marx/ > /dev/null 2>&1 
  if test $? -eq 0
  then
    /bin/rm index
    WGET="lwp-download"    
  else
    echo "# $tool ($ver): ERROR: Please install wget or lwp-download" 1>&2
    exit 1
  fi
fi


# ------- 
# Build marx tools

mkdir -p "$top/marx-${marx_ver}"
cd "$top/marx-${marx_ver}"
/bin/rm -f ${marx_file}

( ${WGET} ${marx_ftp}/${marx_file} && tar xfz ${marx_file} )
if test $? -ne 0
then
  exit 1
fi



cd marx-${marx_ver}
(./configure --prefix="$top/marx-${marx_ver}" --with-cfitsio=$ASCDS_INSTALL && make)
if test $? -ne 0
then
  echo "# $tool ($ver): ERROR: Problem building MARX.  Contact MARX team for help: marx-help@space.mit.edu" 1>&2
 exit 1
fi

make marxrsp
if test $? -ne 0
then
  echo "# $tool ($ver): ERROR: Problem building marxrsp.  Skipping it." 1>&2
fi


make install
if test $? -ne 0
then
  echo "# $tool ($ver): ERROR: Problem building MARX. Contact MARX team for help: marx-help@space.mit.edu" 1>&2
 exit 1
fi

make -k clean


if test -e "$top/marx"
then
  rm "$top/marx"
fi

(cd "$top" ; ln -s "marx-${marx_ver}" "marx" )


cat << EOM

Builds are complete.  

For MARX documentation and support please visit 
http://space.mit.edu/ASC/MARX/


To setup for this installation:

EOM

cat << EOM
setenv MARX_ROOT $top/marx-${marx_ver}
setenv MARX_DATA_DIR \${MARX_ROOT}/share/marx/data
set path = ( \$path \${MARX_ROOT}/bin )
setenv PFILES \${PFILES}:\$MARX_ROOT/share/marx/pfiles
EOM


cat << EOM

These commands are scripted in 

(t)csh: source $top/marx-${marx_ver}/setup_marx.csh
bash: source $top/marx-${marx_ver}/setup_marx.sh

EOM


cat << EOM >> "$top/marx-${marx_ver}/setup_marx.csh"
# Auto generated by $tool ($ver)
setenv MARX_ROOT $top/marx-${marx_ver}
setenv MARX_DATA_DIR \${MARX_ROOT}/share/marx/data
set path = ( \$path \${MARX_ROOT}/bin )
setenv PFILES \${PFILES}:\$MARX_ROOT/share/marx/pfiles
EOM

cat << EOM >> "$top/marx-${marx_ver}/setup_marx.sh"
# Auto generated by $tool ($ver)
MARX_ROOT=$top/marx-${marx_ver}
MARX_DATA_DIR=\${MARX_ROOT}/share/marx/data
PATH=\${PATH}:\${MARX_ROOT}/bin
PFILES=\${PFILES}:\$MARX_ROOT/share/marx/pfiles
export MARX_ROOT MARX_DATA_DIR
EOM

exit 0


