#! /bin/sh
###############################################################################
# mk: get syst/vars from getonearch, chk prereq, setup env for make, run make
###############################################################################
# ...
# 20 Jul 2026 - v09: cleanup, doc sequence, allow override getonearch $macx11gl

### check arg cnt
if [ $# -ne 1 ]; then
  echo ""
  echo "use: mk <csurfprog>                        [onearch=`getonearch`]"
  echo ""
  echo " what it does:"
  echo "  (1) warn if you really wanted mk0, not mk"
  echo "  (2) check correct csurf code/directory context"
  echo "  (3) check usable OS architecture/version"
  echo "  (4) get onesyst,oneproc,oneprog,macx11gl,macCGLparms from getonearch"
  echo "  (5) export onesyst,oneproc,oneprog,macx11gl,macCGLparms for Makefiles"
  echo "          N.B.: to override getonearch's choice for \$macx11gl,"
  echo "          export new before run mk (OK:none|xquartz|optloc|csurfloc)"
  echo "  (6) make one csurf program with GNU make"
  echo "                                                              vers: 09"
  echo "  sequence: mk -> getonearch -> exportvars -> Makefile logic"
  echo ""
  exit
fi

### warn/quit if target is one that is instead intended for root makescript, mk0
targ=""
if [ $# -eq 1 ]; then targ=$1; fi
if [ "$targ" = "csurf" ] || \
   [ "$targ" = "csurf-afnilink" ] || \
   [ "$targ" = "csurf-noafni" ] || \
   [ "$targ" = "dist" ] || \
   [ "$targ" = "afni" ] || \
   [ "$targ" = "afnilink" ] || \
   [ "$targ" = "tcltktix" ] || \
   [ "$targ" = "libtiff" ] || \
   [ "$targ" = "libezxml" ] || \
   [ "$targ" = "libftgl" ] || \
   [ "$targ" = "mpeg" ] || \
   [ "$targ" = "progs" ] || \
   [ "$targ" = "help" ] || \
   [ "$targ" = "insttmp" ] || \
   [ "$targ" = "viewerlin" ] || \
   [ "$targ" = "viewermac" ] || \
   [ "$targ" = "fixinst" ]; then
  echo ""
  echo "mk: for action \"$1\", use toplevel make instead:"
  echo ""
  echo "  cd \$CSURF_DIR"
  echo "  ./mk0 $1"
  echo ""
  exit
fi

### check CSURF_DIR in env
echo ""
if [ -z "$CSURF_DIR" ]; then
  echo "mk: ### first do: export CSURF_DIR=<distribution_dir>"
  exit
fi
echo "mk: CSURF_DIR is exported to env"

### check CSURF_DIR context (getonearch, tksurfer.c, tcltktix/tiff/xml libs)
getonearchscript=$CSURF_DIR/bin/noarch/getonearch
if [ ! -e "$getonearchscript" ]; then
  echo "mk: ### getonearch is missing (perhaps not in a csurf program dir?)"
  echo ""
  exit
fi
if [ ! -e ../tksurfer/tksurfer.c ]; then
  echo "mk: ### ../tksurfer/tksurfer.c is missing (not in a csurf program dir?)"
  echo ""
  exit
fi
if [ "$targ" = "tksurfer" ] || \
   [ "$targ" = "tkmedit" ] || \
   [ "$targ" = "tkregister" ] || \
   [ "$targ" = "tkstrip" ]; then
  if [ ! -e ../tcltktix/include ]; then
    echo "mk: ### ../tcltktix/include is missing (tcl/tk/tix not compiled yet?)"
    echo ""
    exit
  fi
fi
if [ "$targ" = "tksurfer" ] || \
   [ "$targ" = "tkmedit" ] || \
   [ "$targ" = "tkregister" ] || \
   [ "$targ" = "tkstrip" ] || \
   [ "$targ" = "mytoppm" ] || \
   [ "$targ" = "nmovie" ]; then
  if [ ! -e ../libtiff-* ]; then
    echo "mk: ### ../libtiff-* is missing (tifflib not compiled yet?)"
    echo ""
    exit
  fi
fi
if [ "$targ" = "tksurfer" ]; then
  if [ ! -e ../libezxml-* ]; then
    echo "mk: ### ../libezxml-* is missing (libezxml compiled yet?)"
    echo ""
    exit
  fi
  if [ ! -e ../libezxml-* ]; then
    echo "mk: ### ../libezxml-* is missing (libezxml compiled yet?)"
    echo ""
    exit
  fi
  # don't check libfreetype/libftgl since maybe no-font gcc make
fi
echo "mk: found getonearch, ../tcltktix/include, ../tksurfer/tksurfer.c, ... "

### get my onearch
onearch=`"$getonearchscript"`
if [ "$onearch" = "unknown" ]; then
  echo "mk: ### don't recognize this arch (uname -a):"
  echo ""; uname -a; echo ""
  exit
fi
echo "mk: onearch = $onearch"

### check Makefile exists
if [ ! -e Makefile ]; then
  echo "mk: ### no Makefile found"
  exit
else
  mkfile=Makefile
fi
echo "mk: found Makefile: $mkfile"

### check install dir, make it if not there
instdir=$CSURF_DIR/bin/$onearch
if [ ! -e "$instdir" ]; then
  echo "mk: ### arch-specific instdir missing, making it:"; echo "  $instdir"
  mkdir $instdir
fi
echo "mk: found installdir = $instdir"

### check proper onearch tcltktix/tifflib there, if tk{surf/med/reg}
if [ "$targ" = "tksurfer" ] || \
   [ "$targ" == "tkmedit" ] || \
   [ "$targ" == "tkregister" ]; then
  if [ ! -e ../tcltktix/include/$onearch ] || \
     [ ! -e ../tcltktix/lib/$onearch ] || \
     [ ! -e ../libtiff-3.8.2/include/$onearch ] || \
     [ ! -e ../libtiff-3.8.2/libstatic/$onearch ]; then
    echo "mk: ### arch-specific include or lib dir missing:"
    echo "  ../{tcltktix,libtiff-3.8.2}/{include,lib}/$onearch"
    exit
  fi
  echo "mk: tcl{include,lib}dir = ../tcltktix/{include,lib}/$onearch"
  echo "mk: libtiff{include,lib}dir = ../libtiff-3.8.2/{include,lib}/$onearch"
fi

### call getonearch to export onesyst,oneproc,oneprog,macx11gl,macCGLparms
### these are used for Makefile ifeq logic
onesyst=`"$getonearchscript" onesyst`
export onesyst
echo "mk: export onesyst=$onesyst"

oneproc=`"$getonearchscript" oneproc`
export oneproc
echo "mk: export oneproc=$oneproc"

oneprog=$targ
export oneprog
echo "mk: export oneprog=$targ"

# only use getonearch choice here if not def'd in env (so mk0|user can override)
echo "mk: initial env val: macx11gl=$macx11gl"
if [ -z $macx11gl ]; then
  macx11gl=`"$getonearchscript" macx11gl`
  echo "mk: from getonearch: macx11gl=$macx11gl"
fi
if [ "$macx11gl" != "none"     ] && [ "$macx11gl" != "xquartz"   ] && \
   [ "$macx11gl" != "optloc" ]   && [ "$macx11gl" != "csurfloc" ]; then
  echo "mk: ### bad \$macx11gl: $macx11gl (OK: none,xquartz,optloc,csurfloc)"
  exit
fi
export macx11gl
echo "mk: export macx11gl=$macx11gl"

macCGLparms=`"$getonearchscript" macCGLparms`
export macCGLparms
echo "mk: export macCGLparms=$macCGLparms"

### set make prog
makeprog=make
if [ $onesyst = "IRIX" ]; then
  echo "mk: onesyst=IRIX: using gmake (need Makefile logic not in IRIX make)"
  makeprog=gmake
fi
echo "mk: make prog = `which $makeprog`"

### finally run make
if [ "$targ" = "clean" ]; then
  echo "mk: $makeprog clean"
  unset oneprog     # cant make target 'clean' a variable
  $makeprog clean
else
  # force clean every time for C or C++ libs
  echo "mk: rm -f $targ.o"
  rm -f $targ.o
  if [ "$targ" = "surfclust" ] || \
     [ "$targ" = "multiclust" ] || \
     [ "$targ" = "randsurfclust" ] || \
     [ "$targ" = "paint" ] || \
     [ "$targ" = "bresize" ]; then
    echo "mk: rm -f ../csurflibs/\*.o  (.c vs. .cpp)"
    rm -f ../csurflibs/*.o
  fi
  echo "mk: $makeprog -f $mkfile $targ"
  echo ""
  $makeprog -f $mkfile $targ
fi

