#! /bin/sh
###############################################################################
# getonearch: get my generic architecture type
#  (1) return csurf bin/lib/include arch subdir (e.g., Darwin-x86_64) used by:
#    mk0 (cmpl,inst), mk (cmpl), csurf (bindir), FreeSurferEnv (bindir,libdir)
#  (2) return arch,loclib vars used by mk to set env vars for Makefile logic
###############################################################################
# 0 args: return my std arch assembled from uname: e.g., "Darwin-x86_64"
# 1 arg:  return vars from OS check so mk can set env vars for Makefile logic
#    onearch  -> e.g., "Darwin-x86_64" (e.g., to pick $CSURF_DIR/bin/$onearch)
#    onesyst  -> e.g., "Darwin"
#    oneproc  -> e.g., "x86_64"
#    macx11gl -> e.g., "xquartz" (tksurfer,tkmedit,tkregister,tkstrip)
#    macCGLparms -> e.g., "0" (tkregister only)
#    fixldcycle  -> e.g., "0" (fix ancient mac 10.5 make bug)
###############################################################################

### onearch: find my generic architecture (onearch)
## used by mk0, mk, FreeSurferEnv.{sh,csh}, update-tksurfer (hack), mksurface
#Linux: uname -m=i686/x86_64            -i=i386/x86_64 -p=i386/i686/x86_64/unk
#Mac:   uname -m=i386/"Power Macintosh" -i=[illegal]   -p=i386/powerpc
#IRIX:  uname -m=IP22/IP30              -i=[illegal ]  -p=mips/...

### get uname info to be compressed into my generic $oneproc
#set hdrw = `uname -i` # Linux: i386/x86_64, Mac/IRIX: illegal option
syst=`uname -s` # Darwin, Linux, IRIX, IRIX64
proc=`uname -p` # powerpc, i386, i686, athlon, x86_64, mips, arm, Ubun->unknown
mach=`uname -m` # Power Macintosh, i686, IP30, x86_64, arm64

### get kernel release number
#uname -r:  e.g., Darwin=8.11.0/25.5.0, Linux=2.6.23.17-88.fc7, IRIX=6.5
# Mac: SnowLeop=10,Yosem=14,ElCap=15,Catalina=19,BigSur=20,Sequoia=24,Tahoe=25
kernelreleasenum=`uname -r | cut -f 1 -d .`  # e.g. 19.6.0 -> get 19
uselocalx11gl=0
if [ $syst = "Darwin" ] && [ $kernelreleasenum -ge 20 ]; then   # or 25
  # current included precompiled Mac X11/GL libs for compile on BigSur+:
  #   arm64:  mesa-25.3.4 homebrew tap (gallium/spirv/llvm w/hardware suport)
  #   x86_64: TODO: mesa-26.1.4 macports (gallium/software render)
  uselocalx11gl=1
fi
# uncomment next to use Rosetta2 on ARM to run Intel binaries on BigSur->Sequoia
#uselocalx11gl=0  # Rosetta2 to run Intel binaries on ARM worked until Tahoe

### hack for very old
macuse64proc=1    # Mac: make/use 64-bit bins, even on old capable 32-bit OS's

### known arch's
allknown="\
  Darwin-ppc Darwin-i386 Darwin-x86_64 Darwin-arm64 \
  Linux-i386 Linux-x86_64 \
  IRIX-mips IRIX64-mips"

### defaults
onearch=unknown   # [see allknown above]
onesyst=unknown   # Darwin|Linux|IRIX
oneproc=unknown   # ppc|i386|x86_64|arm64|mips
macx11gl=none     # none|xquartz|optloc|csurfloc
macCGLparms=1     # 0|1 - CGL's disappear Catalina (10.15) -> Big Sur (11.0)
fixldcycle=0      # 0|1 - only needed Darwin 10.5 (9.* kernels)

### my classify
#--- Mac PPC ------------------------------------------------------------------
if [ $syst = "Darwin" ] && [ $proc = "powerpc" ]; then
  onesyst=Darwin
  oneproc=ppc
  macx11gl=xquartz
fi
#--- Mac Intel i386 -----------------------------------------------------------
if [ $syst = "Darwin" ] && [ $proc = "i386" ]; then
  onesyst=Darwin
  oneproc=i386
  macx11gl=xquartz
fi
if [ $syst = "Darwin" ] && [ $proc = "i386" ] && \
   [ $kernelreleasenum -eq 9 ]; then
  onesyst=Darwin
  oneproc=i386
  fixldcycle=1    # Apple-approved hack ldflag to fix link err cycle
  macx11gl=xquartz
fi
#--- Mac Intel x86_64 (32bit OS) ----------------------------------------------
if [ $syst = "Darwin" ] && [ $proc = "i386" ] && [ $macuse64proc -eq 1 ]; then
  onesyst=Darwin
  oneproc=x86_64   # N.B.: default on a 32bitOS
  macx11gl=xquartz
fi
if [ $syst = "Darwin" ] && [ "$mach" = "x86_64" ]; then
  onesyst=Darwin
  oneproc=x86_64
  macx11gl=xquartz
fi
#--- Mac Intel x86_64 ---------------------------------------------------------
if [ $syst = "Darwin" ] && [ $proc = "i386" ] && [ $uselocalx11gl -eq 0 ]; then
  onesyst=Darwin
  oneproc=x86_64
  macx11gl=xquartz
fi
if [ $syst = "Darwin" ] && [ $proc = "i386" ] && [ $uselocalx11gl -eq 1 ]; then
  onesyst=Darwin
  oneproc=x86_64
  macx11gl=csurfloc   # macports mesa-26.1.4 llvmpipe (not working!)
  macCGLparms=0       # CGL's gone after Catalina (tkregister)
fi
#--- Mac ARM arm64 ------------------------------------------------------------
if [ $syst = "Darwin" ] && [ $proc = "arm" ] && [ $uselocalx11gl -eq 0 ]; then
  onesyst=Darwin
  ### use Intel bins w/Rosetta2 until Tahoe llvm mesa compile tested pre-Tahoe
  #oneproc=arm64
  oneproc=x86_64
  macx11gl=xquartz
fi
if [ $syst = "Darwin" ] && [ $proc = "arm" ] && [ $uselocalx11gl -eq 1 ]; then
  onesyst=Darwin
  oneproc=arm64
  macx11gl=csurfloc   # homebrew mesa-25.3.4 tap llvm/spirv from Peter Molfese
  macCGLparms=0       # CGL's gone after Catalina (tkregister)
fi
#--- Linux 32bit --------------------------------------------------------------
if [ $syst = "Linux" ] && [ $proc = "i386" ]; then
  onesyst=Linux
  oneproc=i386
  onearch=${onesyst}-${oneproc}
fi
if [ $syst = "Linux" ] && [ $proc = "i686" ]; then
  onesyst=Linux
  oneproc=i386
  onearch=${onesyst}-${oneproc}
fi
if [ $syst = "Linux" ] && [ $proc = "athlon" ]; then
  onesyst=Linux
  oneproc=i386
  #oneproc=x86_64
  onearch=${onesyst}-${oneproc}
fi
if [ $syst = "Linux" ] && [ $proc = "unknown" ] && [ "$mach" = "i386" ]; then
  onesyst=Linux
  oneproc=i386
  onearch=${onesyst}-${oneproc}
fi
if [ $syst = "Linux" ] && [ $proc = "unknown" ] && [ "$mach" = "i686" ]; then
  onesyst=Linux
  oneproc=i386
  onearch=${onesyst}-${oneproc}
fi
#--- Linux 64bit --------------------------------------------------------------
if [ $syst = "Linux" ] && [ $proc = "x86_64" ]; then
  onesyst=Linux
  oneproc=x86_64
  onearch=${onesyst}-${oneproc}
fi
if [ $syst = "Linux" ] && [ $proc = "unknown" ] && [ "$mach" = "x86_64" ]; then
  onesyst=Linux
  oneproc=x86_64
  onearch=${onesyst}-${oneproc}
fi
#--- IRIX 32/64bit ------------------------------------------------------------
if [ $syst = "IRIX" ] && [ $proc = "mips" ]; then
  onesyst=IRIX    # -n32 vs -32
  oneproc=mips
  onearch=${onesyst}-${oneproc}
fi
if [ $syst = "IRIX64" ] && [ $proc = "mips" ]; then
  onesyst=IRIX    # -64
  oneproc=mips
  onearch=${onesyst}-${oneproc}
fi
#------------------------------------------------------------------------------

### get generic type for lib/include arch subdirs
if [ $onesyst != "unknown" ] && [ $oneproc != "unknown" ]; then
  onearch=${onesyst}-${oneproc}
fi  # else onearch=unknown

### return onearch OR allknown OR onesyst/oneproc/onearch/macx11gl/macGGLparms
if [ $# -lt 1 ]; then   # no arg
  echo $onearch
else                    # one arg
  if [ $1 = "known" ]; then
    for onearch in $allknown; do echo "$onearch "; done
  elif [ $1 = "onesyst" ]; then
    echo $onesyst
  elif [ $1 = "oneproc" ]; then 
    echo $oneproc
  elif [ $1 = "onearch" ]; then
    echo $onearch
  elif [ $1 = "macx11gl" ]; then
    echo $macx11gl
  elif [ $1 = "macCGLparms" ]; then
    echo $macCGLparms
  elif [ $1 = "fixldcycle" ]; then
    echo $fixldcycle
  elif [ $1 = "help" ] || [ $1 = "-help" ] || [ $1 == "--help" ]; then
    echo ""
    echo "getonearch [known|onesyst|oneproc|macx11gl|fixldcycle|help]"
    echo ""
    echo "Return csurf generic architecture type:"
    echo "  (1) get csurf bin/lib/include arch subdir (e.g.,Darwin-x86_64) for"
    echo "     mk0 (cmpl,inst), mk (cmpl) csurf (bin), FreeSurferEnv (bin,lib)"
    echo "  (2) get arch,loclib vars used by mk to set env for Makefile logic"
    echo ""
    echo "  known:"
    echo "    Darwin-ppc"
    echo "    Darwin-i386"
    echo "    Darwin-x86_64"
    echo "    Darwin-arm64"
    echo "    Linux-i386"
    echo "    Linux-x86_64"
    echo "    IRIX-mips"
    echo "    IRIX64-mips "
    echo ""
    echo "  onesyst:"
    echo "    Darwin"
    echo "    Linux"
    echo "    IRIX"
    echo ""
    echo "  oneproc:"
    echo "    ppc"
    echo "    i386"
    echo "    x86_64"
    echo "    arm_64"
    echo "    mips"
    echo ""
    echo "  macx11gl:"
    echo "    none"
    echo "    xquartz"
    echo "    optloc"
    echo "    csurfloc"
    echo ""
    echo "  macCGLparms:"
    echo "    0"
    echo "    1"
    echo ""
    echo "  fixlcycle:"
    echo "    0"
    echo "    1"
    echo ""
  else
    echo "getonearch: ### arg '$1' not recognized"
  fi
fi

