#! /bin/sh

### 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
#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
rele=`uname -r` # e.g., Darwin=8.11.0, Linux=2.6.23.17-88.fc7, IRIX=6.5
rnum=`uname -r | cut -f 1 -d .`  # Snow=10,Catalina=19,BigSur=20,Tahoe=25 FC7=2

### hacks
fixldcycle=0    # only needed Darwin 10.5 (9.* kernels)
use64proc=1     # newer Mac: 64-bit binaries default, even on 32-bit OS

### known
known="\
  Darwin-ppc Darwin-i386 Darwin-x86_64 Darwin-arm64 \
  Linux-i386 Linux-x86_64 \
  IRIX-mips IRIX64-mips"

### defaults
onearch=unknown
macx11gl=none    # none|xquart|systbrew|csurfbrew
macCGLparms=1    # 0|1

### my classify
if [ $syst = "Darwin" ] && [ $proc = "powerpc" ]; then
  onesyst=Darwin
  oneproc=ppc
  onearch=${onesyst}-${oneproc}
  macx11gl=xquartz
fi
if [ $syst = "Darwin" ] && [ $proc = "i386" ]; then
  onesyst=Darwin
  oneproc=i386
  onearch=${onesyst}-${oneproc}
  macx11gl=xquartz
fi
if [ $syst = "Darwin" ] && [ $proc = "i386" ] && [ $rnum -eq 9 ]; then
  onesyst=Darwin
  oneproc=i386
  onearch=${onesyst}-${oneproc}
  fixldcycle=1    # Apple-approved hack ldflag to fix link err cycle
  macx11gl=xquartz
fi
#--------------------------------------------------------------------------
if [ $syst = "Darwin" ] && [ $proc = "i386" ] && [ $use64proc -eq 1 ]; then
  onesyst=Darwin
  oneproc=x86_64  # def:32bitOS
  onearch=${onesyst}-${oneproc}
  macx11gl=xquartz
fi
if [ $syst = "Darwin" ] && [ "$mach" = "x86_64" ]; then
  onesyst=Darwin
  oneproc=x86_64  # def:32bitOS
  onearch=${onesyst}-${oneproc}
  macx11gl=xquartz
fi
#--------------------------------------------------------------------------
#Rosetta2 only until rnum=27 (Tahoe=25)
#if all universal, insttmp could symlink arm64->x86_64 (won't happen)
if [ $syst = "Darwin" ] && [ $proc = "arm" ] && [ $rnum -gt 19 ]; then
  onesyst=Darwin
  #oneproc=x86_64      # tmp for intel on arm using Rosetta2
  oneproc=arm64
  onearch=${onesyst}-${oneproc}
  #macx11gl=xquartz    # go back to this if XQuartz/Mesa fixed!
  macx11gl=systbrew    # afni/homebrew-mesa-25.3.4 brew tap def inst
  #macx11gl=csurfbrew  # change to this when loc libs in csurf dist
  macCGLparms=0        # CGL's disappeared in Catalina (10.15) -> Big Sur (11.0)
fi
#----------------------------------------------------------------------
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
#----------------------------------------------------------------------
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
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
# else onearch=unknown

### return onearch (or known or onesyst/oneproc/onearch)
if [ $# -lt 1 ]; then
  echo $onearch
else
  if [ $1 = "known" ]; then
    for onearch in $known; do
      echo "$onearch "
    done
  fi
  if [ $1 = "onesyst" ]; then echo $onesyst; fi
  if [ $1 = "oneproc" ]; then echo $oneproc; fi
  if [ $1 = "onearch" ]; then echo $onearch; fi
  if [ $1 = "macx11gl" ]; then echo $macx11gl; fi
  if [ $1 = "macCGLparms" ]; then echo $macCGLparms; fi
  if [ $1 = "fixldcycle" ]; then echo $fixldcycle; fi
  if [ $1 = "help" ] || [ $1 = "-help" ]; then
    echo "getonearch [known|onesyst|oneproc|macx11gl|fixldcycle|help]"
  fi
fi

