#!/bin/tcsh

@global_parse `basename $0` "$*" ; if ($status) exit 0

#set version   = "1.0";  set rev_dat   = "June 27, 2019"
#   + Start: glue imgs together vertically
#
set version   = "1.1";  set rev_dat   = "Feb 21, 2021"
#   + [rickr]: shift dependency tests until after -help
#
# ----------------------------------------------------------------

set this_prog = "@djunct_edgy_align_check"
set tpname    = "${this_prog:gas/@djunct_//}"
set here      = "$PWD"

# ----------------------- set defaults --------------------------

set imtop    = ""
set imbot    = ""

set odir     = "."
set opref    = "IMG"

set tpref    = "__tmp_gluing"

set DO_CLEAN       = 1                    # default: do remove

# ------------------- process options, a la rr ----------------------

if ( $#argv == 0 ) goto SHOW_HELP

set ac = 1
while ( $ac <= $#argv )
    # terminal options
    if ( ("$argv[$ac]" == "-h" ) || ("$argv[$ac]" == "-help" )) then
        goto SHOW_HELP
    endif
    if ( "$argv[$ac]" == "-ver" ) then
        goto SHOW_VERSION
    endif

    #  ---------- inputs: required ---------------

    if ( "$argv[$ac]" == "-imtop" ) then
        if ( $ac >= $#argv ) goto FAIL_MISSING_ARG
        @ ac += 1
        set imtop = "$argv[$ac]"

    else if ( "$argv[$ac]" == "-imbot" ) then
        if ( $ac >= $#argv ) goto FAIL_MISSING_ARG
        @ ac += 1
        set imbot = "$argv[$ac]"

    #  ---------- opts ---------------

    else if ( "$argv[$ac]" == "-prefix" ) then
        if ( $ac >= $#argv ) goto FAIL_MISSING_ARG
        @ ac += 1
        set opref = `basename "$argv[$ac]"`
        set odir  = `dirname  "$argv[$ac]"`

    else
        echo "\n\n** ERROR: unexpected option #$ac = '$argv[$ac]'\n\n"
        goto BAD_EXIT
        
    endif
    @ ac += 1
end

# ----------------- find AFNI and set viewer ---------------------

# check for dependency failures after -help    21 Feb 2021 [rickr]
# (this section is currently self-contained, variables are local)

# find AFNI binaries directory and viewer location
set adir      = ""
set my_viewer = ""
which afni >& /dev/null
if ( $status ) then
    echo "** Cannot find 'afni' (?!)."
    goto BAD_EXIT
else
    set aa   = `which afni`
    set adir = $aa:h
endif

# from @snapshot volreg
set nerr = 0
set errm = "** ERROR:"

set plist = ( djpeg cjpeg pnmcat )
foreach pppp ( $plist )
  set wwww = `which $pppp`
  if ( $status != 0 ) then
    @ nerr++
    set errm = "$errm $pppp"
  endif
end

if ( $nerr > 0 ) then
  echo "$errm -- not found in path -- @djunct_glue_imgs_vert fails"
  echo "** WARNING: this script cannot run without installing package netpbm11"
  exit 1
endif

# =======================================================================
# ============================ ** SETUP ** ==============================
# =======================================================================

if ( "$imtop" == "" ) then
    echo "** ERROR: missing top img! Use '-imtop ..'"
    goto BAD_EXIT
endif

if ( "$imbot" == "" ) then
    echo "** ERROR: missing bot img! Use '-imbot ..'"
    goto BAD_EXIT
endif

# ===================== output dir + wdir =======================
 
# check output directory, use input one if nothing given

if ( ! -e $odir ) then
    echo "++ Making new output directory: $odir"
    \mkdir -p $odir
endif

# =========================== Actual Plots ==============================

\mkdir -p $odir

# just stolen from @snapshot_volreg, basically
djpeg "${imbot}" > ${tpref}_imbot.pnm
djpeg "${imtop}" > ${tpref}_imtop.pnm

pnmcat -tb -jcenter -black              \
    ${tpref}_imtop.pnm                  \
    ${tpref}_imbot.pnm                  \
    | cjpeg -quality 100                \
    > ${odir}/${opref}

# ---------------------------------------------------------------------

if ( $DO_CLEAN == 1 ) then
    echo "\n+* Removing temporary files '${tpref}*'\n"
    \rm ${tpref}*
else
    echo "\n+* NOT removing temporary files '${tpref}*'\n"
endif

echo ""
echo "++ DONE! Image output:"
echo "       ${odir}/${opref}"
echo ""


goto GOOD_EXIT

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

SHOW_HELP:
cat << EOF
-------------------------------------------------------------------------

OVERVIEW ~1~

More helpful helpfile description coming (some day...)

This is just a helper script for other things: glue two images
together vertically.

written by PA Taylor, modelled heavily on RW Cox's '@snapshot_volreg'
script

# --------------------------------------------------------------------

COMMAND OPTIONS ~1~

-hview
-help
-ver
-imbot
-imtop
-prefix

EOF

# ----------------------------------------------------------------------

    goto GOOD_EXIT

SHOW_VERSION:
   echo "version  $version (${rev_dat})"
   goto GOOD_EXIT

FAIL_MISSING_ARG:
    echo "** ERROR! Missing an argument after option flag: '$argv[$ac]'"
    goto BAD_EXIT

BAD_EXIT:
    exit 1

GOOD_EXIT:
    exit 0
