#!/bin/tcsh -fe

# written by Ben Jung (June 30, 2020)
# additional contact people about NMT issues:
#    * Adam Messinger (re. template/data itself)
#    * Daniel Glen (of course)
#    * Paul Taylor

set stat = 0
set sdir = "$PWD"

goto PARSE
RET_PARSE:
if ("$nmt_ver" =~ 2*) then
    set template = NMT_v${nmt_ver}_${sym}
else
    set template = NMT_v${nmt_ver}
endif

if ( -d "${install_dir}/$template" ) then
  if (${overwrite} == "0") then
    echo ""
    echo "ERROR:"
    echo "   Directory ${install_dir}/$template exists already"
    echo "   If you want to reinstall the NMT, add -overwrite"
    echo "   to your @Install_NMT command"
    echo ""
    goto END
  else if (${overwrite} == "1") then
    echo "WARNING: Removing currently installed ${install_dir}/$template"
    \rm -r "${install_dir}/$template"
  endif
endif

if ($use_curl == -1) then
   which curl
   if ($status) then
      set use_curl = 0;
   else
      set use_curl = 1;
   endif
endif

cd "${install_dir}"

echo "Downloading https://afni.nimh.nih.gov/pub/dist/atlases/macaque/nmt/${template}.tgz"
if ($use_curl == 0) then
   wget https://afni.nimh.nih.gov/pub/dist/atlases/macaque/nmt/${template}.tgz
else
   curl -O https://afni.nimh.nih.gov/pub/dist/atlases/macaque/nmt/${template}.tgz
endif

\tar -xvzf $template.tgz
\rm $template.tgz

cd $template

cat <<EOF

********************************************************
Please read ${install_dir}/$template/README.txt for more information
about this template
********************************************************

EOF

### PT: user has to set up the specific format of the dataset they
### want to be in the env vars: *_05mm, *_fh, or 'default'.  This
### won't work anymore
#if ($set_env == 1) then
#  set atlas_dir = ${install_dir}
#  @AfniEnv -set AFNI_SUPP_ATLAS_DIR $atlas_dir
#  @AfniEnv -set AFNI_WHEREAMI_DEC_PLACES 2
#  @AfniEnv -set AFNI_TEMPLATE_SPACE_LIST NMT,NMT2,D99_Macaque,D99_Macaque_book
#  @AfniEnv -set AFNI_WEBBY_WAMI YES
#endif

cd -


goto END

PARSE:

#------------------------ process user options --------------------------
set use_curl = -1
set nmt_ver = 2.1
set sym = sym
set install_dir = "${sdir}"
set overwrite = 0
set ac = 1
set set_env = 0
while ($ac <= $#argv)
   if ("$argv[$ac]" == "-help" || "$argv[$ac]" == "-h") then
       goto HELP

   else if ("$argv[$ac]" == "-curl") then
        set use_curl = 1

   else if ("$argv[$ac]" == "-wget") then
        set use_curl = 0

   else if ("$argv[$ac]" == "-install_dir") then
       set this_opt = "$argv[$ac]"
       @ ac ++
       if ( $ac > $#argv ) then
           echo "** missing parameter for option '${this_opt}'"
           exit 1
       endif
       set install_dir =  "$argv[$ac]"

   else if ("$argv[$ac]" == "-overwrite") then
       set overwrite = "1"

   else if ("$argv[$ac]" == "-nmt_ver") then
       set this_opt = "$argv[$ac]"
       @ ac ++
       if ( $ac > $#argv ) then
           echo "** missing parameter for option '${this_opt}'"
           exit 1
       endif
       set nmt_ver = $argv[$ac]
       if (($nmt_ver != "1.2")  && \
           ($nmt_ver != "1.3")  && \
           ($nmt_ver != "2.0")  && \
           ($nmt_ver != "2.1")) then
             echo "nmt_ver $nmt_ver is not a valid choice"
             exit 1
       endif

   else if ("$argv[$ac]" == "-sym") then
       set this_opt = "$argv[$ac]"
       @ ac ++
       if ( $ac > $#argv ) then
           echo "** missing parameter for option '${this_opt}'"
           exit 1
       endif
       set sym = $argv[$ac]
       if ($sym == "symmetric") then
         set sym = sym
       else if ($sym == "asymmetric") then
         set sym = asym
       endif

   else if ("$argv[$ac]" == "-set_env") then
       #set set_env = "1"
       echo "+* WARNING: '-set_env' is no longer a usable option"
       echo "   Instead, after the datadownload has completed,"
       echo "   Run the setup*.tcsh script in your NMT data directory"
       echo "   of choice to setup the AFNI environment vars."

   else
       echo "Error: Option or parameter '$argv[$ac]' not understood"
       goto END
   endif
   @ ac ++
end

goto RET_PARSE

HELP:
cat << EOF

   Overview ~1~

   Installs the NIMH Macaque Template (NMT) with the accompanying
   Cortical Hierarchy Atlas of the Rhesus Macaque (CHARM).

   Note there are multiple versions and variants of the template to
   choose from (see the "-ver .." and "-sym .." options, below), with
   the default being the ${sym}metric NMT v${nmt_ver}.

   The NMT v2 and the CHARM are described in:

      Jung B, Taylor PA, Seidlitz PA, Sponheim C, Perkins P, Glen DR,
      Messinger A (2021). A Comprehensive Macaque FMRI Pipeline and
      Hierarchical Atlas. NeuroImage 235:117997.
      https://doi.org/10.1016/j.neuroimage.2021.117997.
      
   while the previous template versions (the NMT v1.2 and NMT v1.3) are
   described in:

      Seidlitz J, Sponheim C, Glen DR, Ye FQ, Saleem KS, Leopold DA,
      Ungerleider L, Messinger A (2018). A Population MRI Brain
      Template and Analysis Tools for the Macaque. NeuroImage 170:
      121–31. doi: 10.1016/j.neuroimage.2017.04.063.

   ** Please cite Jung et al. (2021) and/or Seidlitz et al. (2018) if
   you make use of the respective templates in your work. **

   After the archive is downloaded and unpacked (as the directory
   "NMT_*", depending on your selected version+variant), see its
   README.txt for details.

   --------------------------------------------------------------------------

   Options ~1~

   -install_dir DD  :the install location for the NMT
                     (default = the PWD)

   -nmt_ver VV      :the version of the NMT you wish to install
                     (default = ${nmt_ver}; valid = 2.1, 2.0, 1.3, 1.2)

   -sym SS          :the NMT v2 has both a symmetric and asymmetric variant.
                     Specify the desired variant SS as either "sym" or
                     "asym".  Option is valid for the NMT v2 only--in
                     other cases it is ignored. (default = "${sym}")

   -overwrite       :allows the script to remove a pre-existing NMT directory

   -wget            :use wget to download archive. Script chooses by default
                     with preference for curl

   -curl            :use curl to download archive. Script chooses by default
                     with preference for curl

   -help            :repeats this message

   --------------------------------------------------------------------------

   Questions/contact ~1~

   If you have any NMT or CHARM questions, please contact:

     Adam Messinger : messinga @ nih.gov
     Ben Jung       : benjamin.jung @ nih.gov

   For demo or processing (@animal_warper, afni_proc.py, etc.) questions,
   please contact:

     Daniel Glen    : glend @ mail.nih.gov
     Paul Taylor    : paul.taylor @ nih.gov

   or ask on the AFNI Message Board.

EOF

   goto END

BEND:
   echo "Failed"
   set stat = 1
   goto END

END:
   exit $stat
