#! /bin/sh

### mk: get syst/proc, check prereq's, setup env for make, run make

### warn/quit if target is one intended for root mk0
targ=""
if [ $# -eq 1 ]; then targ=$1; fi
if [ "$targ" = "onearch" ] || \
   [ "$targ" = "onearch-afnilink" ] || \
   [ "$targ" = "onearch-noafni" ] || \
   [ "$targ" = "dist" ] || \
   [ "$targ" = "afni" ] || \
   [ "$targ" = "afnilink" ] || \
   [ "$targ" = "tcltktix" ] || \
   [ "$targ" = "libtiff" ] || \
   [ "$targ" = "libezxml" ] || \
   [ "$targ" = "mpeg" ] || \
   [ "$targ" = "progs" ] || \
   [ "$targ" = "help" ] || \
   [ "$targ" = "insttmp" ] || \
   [ "$targ" = "viewerlin" ] || \
   [ "$targ" = "viewermac" ] || \
   [ "$targ" = "fixinst" ]; then
  echo ""
  echo 'mk: use $CSURF_DIR/mk0 instead'
  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 (not in csurf program dir?)"
  echo ""
  exit
fi
if [ ! -e ../tksurfer/tksurfer.c ]; then
  echo "mk: ### ../tksurfer/tksurfer.c is missing (not in 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 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
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 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 arg cnt
if [ $# -ne 1 ]; then
  echo ""
  echo "use: mk <target>             [onearch=$onearch]"
  echo ""
  exit
fi

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

### export onesyst,oneproc,oneprog for Makefiles w/arch logic
echo 'mk: export onesyst=`"$getonearchscript onesyst"`'
export onesyst=`"$getonearchscript" onesyst`

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

echo 'mk: export fixldcycle=`"$getonearchscript" fixldcycle`'
export fixldcycle=`"$getonearchscript" fixldcycle`

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

### get 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    # can't 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

