#! /bin/sh

############################################################################
# EMAIL-2409##.txt  NOTES archived here: not needed after offedge bug fixed
############################################################################
#-------------------------------------------
#hard-to-reproduce crash from running tksurfer
#MacOS 10.6 compile on MacOS 10.15
#-------------------------------------------
#
#I have long been compiling csurf binaries without
#problems on MacOS 10.6 for maximum compatibility.
#However, I recently noticed some occasional
#startup crashes of 10.6-compiled tksurfer running
#on MacOS 10.15 (tksurfer windows briefly appear
#then disappear with a shell error).  Once the
#OpenGL and tk windows are up, everything is fine
#(no crashes).  The crashes don't occur when
#running the 10.6 compile on Mac OS's before 10.15.
#
#Until I get to the bottom of this, I have added a
#MacOS 10.15 compile of tksurfer (exact same C
#code) to the distribution, which never crashes
#on MacOS 10.15.
#
#I also added a script to toggle a "tksurfer"
#symlink back and forth between using the 10.6 and
#10.15 compiles:
#
#  update-tksurfer
#
#Here's what it looks like to run it:
#
#  % update-tksurfer
#
#  update-tksurfer: found CSURF_DIR = /Users/sereno/local/csurf
#  update-tksurfer: found symlink: $CSURF_DIR/bin/Darwin-x86_64/tksurfer
#
#  tksurfer currently uses 10.6 (SnowLeopard) binary (default):
#  Change to use 10.15 (Catalina) binary instead?
#
#   => ctrl-D to go
#   => ctrl-C to quit
#
#  ^D
#  pwd = /Users/sereno/local/csurf/bin/Darwin-x86_64
#  rm tksurfer
#  ln -s tksurfer-10-15 tksurfer
#  update-tksurfer: made new symlink to 10.15/Catalina compile
#
#To go back to the 10.6 compile, run it again.
############################################################################

### update-tksurfer: marty sereno: toggle 10.6/10.15 symlink
# 5 Jul 2024: 01a: working

prog=tksurfer
if [ $# -ne 0 ]; then
  echo ""
  echo "Use: update-$prog"
  echo ""
  echo "  Toggle $prog symlink: 10.6/SnowLeopard vs. 10.15/Catalina) compile"
  echo ""
  echo "                                                          version: 01a"
  exit
fi

# Mac only
if [ `uname` != "Darwin" ]; then
  echo "update-$prog: ### Mac specific  ...quitting"
  exit
fi

# req FreeSurferEnv.{sh,csh,zsh} has been run
if [ -z "${CSURF_DIR+_}" ]; then
  echo "update-$prog: ### CSURF_DIR doesn't exist: 1st run FreeSurferEnv.*"
  exit
else
  echo ""
  echo "update-$prog: found CSURF_DIR = $CSURF_DIR"
fi

# $prog must be symlink, else quit
onearch=`getonearch`
cd $CSURF_DIR/bin/$onearch
lsout=`'ls' -l $prog`
patt="*-> ${prog}*"
found=0
case "$lsout" in ${patt}) found=1 ;; esac
if [ $found -eq 0 ]; then
  echo "update-$prog: ### following not a symlink  ...quitting"
  echo "update-$prog:   \$CSURF_DIR/bin/$onearch/$prog"
  exit
else
  echo "update-$prog: found symlink: \$CSURF_DIR/bin/$onearch/$prog"
fi

# classify which binary symlink currently points to
realbin=`echo $lsout | awk '{print $11}'`
found6=0
case $realbin in *-10-6) found6=1 ;; esac
found15=0
case $realbin in *-10-15) found15=1 ;; esac
if [ $found6 = 0 ] && [ $found15 = 0 ]; then
  echo "update-$prog: ### both 10-6 and 10-15 binary not found  ...quitting"
  exit
fi

# OK to go?
if [ $found6 -eq 1 ]; then
  echo ""
  echo "$prog currently uses 10.6 (SnowLeopard) binary (default):"
  echo "Change to use 10.15 (Catalina) binary instead?"
  echo ""
  echo " => ctrl-D to go"
  echo " => ctrl-C to quit"
  echo ""
  read resp
  echo ""
  echo "pwd = `pwd`"
  echo "rm $prog";                 rm $prog
  echo "ln -s $prog-10-15 $prog";  ln -s $prog-10-15 $prog
  echo "update-$prog: made new symlink to 10.15/Catalina compile"
  echo ""
fi
if [ $found15 -eq 1 ]; then
  echo ""
  echo "$prog currently uses 10.15 (Catalina) binary:"
  echo "Change to use 10.6 (SnowLeopard) binary instead?"
  echo ""
  echo " => ctrl-D to go"
  echo " => ctrl-C to quit"
  echo ""
  read resp
  echo ""
  echo "pwd = `pwd`"
  echo "rm $prog";                rm $prog
  echo "ln -s $prog-10-6 $prog";  ln -s $prog-10-6 $prog
  echo "update-$prog: made new symlink to 10.6/SnowLeopard compile (default)"
  echo ""
fi

