#! /bin/sh

### update-csurf: marty sereno
# 20 Sep 2022 -- 01a: initial hack (mac64/linux64 only)
# 21 Sep 2022 -- 01b: switch site if first down
# 06 May 2025 -- 01c: Mac always warns run xattr (Mac tar can quarantine)
# 05 Jan 2026 -- 01d: add 'ggo' for no-ask, non-verbose

# download website (req's .latestcsurf.txt list of newest tarfiles!)
distdir1=https://pages.ucsd.edu/~msereno/csurf  # UCSD
distdir2=https://mri.sdsu.edu/sereno/csurf      # SDSU copy
distdir=$distdir1

# server dotfile with list of latest tarfiles for each arch
listfile=.latestcsurf.txt

if [ $# -ne 1 ]; then
 echo ""
 echo "Use: update-csurf [go,ggo]"
 echo ""
 echo "      go: setup install, ask to confirm"
 echo "     ggo: go ahead without confirm"
 echo ""
 echo "  Install/update csurf into CURRENT DIRECTORY:"
 echo ""
 echo "    `pwd`"
 echo ""
 echo "  Actions:"
echo "    (1) if no csurf in current dir, install latest version (mac or linux)"
echo "    (2) if csurf there, move it aside to release-date suffix, install new"
 echo "        (e.g., mv csurf csurf-251126)"
 echo ""
 if [ `uname` = "Darwin" ]; then
   echo "  (MacOS) HOWTO un-quarantine if 'wish8.5 not opened/damaged' warning:"
   echo "  run xattr to remove quarantine attribute (req's passwd):"
   echo ""
   echo "    cd `pwd`"
   echo "    sudo xattr -r -d com.apple.quarantine csurf"
   echo ""
 fi
 echo "                                                          version: 01d"
 exit
fi

# require 'go' (or no-ask 'ggo') arg, check pwd writable
if [ ! -z $1 ]; then arg=$1; else arg=""; fi
if [ "$arg" != "go" ] && [ "$arg" != "ggo" ]; then
  echo "update-fsaverage: ### arg ($arg) was not 'go' (or 'ggo')  ...quitting"
  exit
fi
if [ ! -w `pwd` ]; then
  echo "update-fsaverage: ### current directory not writable  ...quitting"
  exit
fi

# check linux/mac 64-bit, warn other (TODO: PPC mac, IRIX, 32-bit Linux)
if [ `uname` = "IRIX" ] || [ `uname` = "IRIX64" ]; then
  echo "update-csurf: ### download/install IRIX tarfile by hand  ...quitting"
  exit
fi
if [ `uname` = "Linux" ]; then
  if [ `uname -p` != "x86_64" ]; then
    echo "update-csurf: ### download/install 32-bit Linux by hand  ...quitting"
    exit
  fi
fi
if [ `uname` = "Darwin" ]; then
  if [ `uname -m` != "x86_64" ] && [ `uname -m` != "arm64" ]; then
    echo \
 "update-csurf: ### download/install non-x86_64/arm64 Mac by hand  ...quitting"
    exit
  fi
fi
if [ `uname` != "Darwin" ] && [ `uname` != "Linux" ]; then
  echo "update-csurf: ### unrecognized OS type: `uname`"
  exit
fi

# check first site up (UCSD), if not, try second (SDSU), else fail
if [ `uname` = "Linux" ]; then
  wget -q --spider $distdir
  err=$?
  if [ $err -ne 0 ]; then
    echo ""
    echo "update-fsaverage: ### $distdir not responding"
    echo "                  ### change to $distdir2"
    distdir=$distdir2
    wget -q --spider $distdir
    err=$?
    if [ $err -ne 0 ]; then
      echo ""
      echo "update-fsaverage: ### $distdir not responding  ...quitting"
      exit
    fi
  fi
fi
if [ `uname` = "Darwin" ]; then
  # curl errs: 6="couldn't resolve host, 7="failed to connect", 28="timeout"
  curl -sf --connect-timeout 5 $distdir > /dev/null
  err=$?
  if [ $err = 6 ] || [ $err = 7 ] || [ $err = 28 ]; then
    echo ""
    echo "update-fsaverage: ### $distdir not responding"
    echo "                  ### change to $distdir2"
    distdir=$distdir2
    curl -sf --connect-timeout 5 $distdir > /dev/null
    err=$?
    if [ $err = 6 ] || [ $err = 7 ] || [ $err = 28 ]; then
      echo ""
      echo "update-fsaverage: ### $distdir not responding  ...quitting"
      exit
    fi
  fi
fi

# prepare move-aside name
renamed=""
if [ ! -d csurf ] && [ -e csurf ]; then
  echo ""
  echo "update-csurf: ### a *file* named 'csurf' exists in current directory"
  echo "              ### move it aside to install csurf *directory* here"
  echo ""
  exit
fi
if [ -d csurf ]; then
  # try to find tardate for suffix in existing (e.g., "tarfile made: 260104")
  tardate=`grep -s "tarfile made" csurf/README.txt | awk '{print $3}'`
  if [ "$tardate" = "" ]; then tardate=old; fi
  # in case re-download same
  renamed0="csurf-$tardate"
  renamed=$renamed0
  i=1
  while [ -d $renamed ]; do
    renamed=${renamed0}-$i
    i=`expr $i + 1`
  done
fi

# find latest dist for current arch
if [ `uname` = "Linux" ]; then
  echo ""
  echo "update-csurf: download latest distribution list"
  echo "update-csurf: wget -q $distdir/$listfile"
                      wget -q $distdir/$listfile
  dist=`grep -s "\-linux64\-" $listfile`
  if [ "$dist" = "" ]; then
    echo ""
    echo "update-csurf: ### Linux tarfile not found in $listfile  ...quitting"
    exit
  fi
fi
if [ `uname` = "Darwin" ]; then
  echo ""
  echo "update-csurf: download latest distribution list"
  echo "update-csurf: curl -Os $distdir/$listfile"
                      curl -Os $distdir/$listfile
  dist=`grep -s "\-mac\-" $listfile`
  if [ "$dist" = "" ]; then
    echo ""
    echo \
      "update-csurf: ### Mac tarfile not found in $listfile  ...quitting"
    exit
  fi
fi
rm -f $listfile

# OK to go? (else 'ggo' no-ask)
echo ""
if [ -d csurf ]; then
  echo "Ready to update csurf in current directory:"
  echo ""
  echo "  `pwd`"
  echo ""
  echo "  (1) source website: $distdir"
  echo "  (2) rename existing csurf install:  mv csurf $renamed"
  echo "  (3) download and untar newest csurf ($dist)"
  if [ `uname` = "Darwin" ]; then
    echo "  (4) user: maybe manually unquarantine using xattr (req's passwd)"
  fi
else
  echo "Ready to install csurf in current directory:"
  echo ""
  echo "  `pwd`"
  echo ""
  echo "  (1) website: $distdir"
  echo "  (2) download and untar newest csurf"
  if [ `uname` = "Darwin" ]; then
    echo "  (3) user: maybe manually unquarantine using xattr (req's passwd)"
  fi
fi

echo ""
if [ "$arg" != "ggo" ]; then
  echo " => ctrl-D to go"
  echo " => ctrl-C to quit"
  read resp
else
  echo " => ggo: going ahead without confirm"
  sleep 1
fi

### go
# rename existing
if [ -d csurf ]; then
  echo ""
  echo "update-csurf: rename existing"
  echo "update-csurf: mv csurf $renamed"
                      mv csurf $renamed
fi

# download latest tarfile
if [ `uname` = "Linux" ]; then
  echo ""
  echo "update-csurf: download tarfile"
  echo "update-csurf: wget $distdir/$dist"
                      wget $distdir/$dist
fi
if [ `uname` = "Darwin" ]; then
  echo ""
  echo "update-csurf: download tarfile"
  echo "update-csurf: curl -O $distdir/$dist"
                      curl -O $distdir/$dist
fi

# untar it
echo ""
echo "update-csurf: pwd=`pwd`"
echo "update-csurf: unpack tarfile"
if [ "$arg" = "ggo" ]; then
  echo "update-csurf: tar xfz $dist"  # omit 'v' to not clog csurf.log
                      tar xfz $dist
else
  echo "update-csurf: tar xvfz $dist"
                      tar xvfz $dist
fi

# done
echo ""
if [ "$renamed" != "" ]; then
  echo "update-csurf: done (can remove $dist and $renamed to save space)"
else
  echo "update-csurf: done (can remove $dist to save space)"
fi

## if Mac: auto-unquarantine (N.B.: req's passwd)
#if [ `uname` = "Darwin" ]; then
#  echo ""
#  echo "update-csurf: unquarantine with xattr (sudo will prompt for passwd)"
#  echo "update-csurf: sudo xattr -r -d com.apple.quarantine csurf"
#                      sudo xattr -r -d com.apple.quarantine csurf
#fi
## if Mac: just warn, since sudo req's passwd: let user do instead
if [ `uname` = "Darwin" ]; then
  echo ""
  echo \
"update-csurf: Mac: HOWTO unquarantine if 'wish8.5 not opened/damaged' warning:"
  echo "update-csurf: run this command in terminal in dir containing csurf:"
  echo ""
  echo "  sudo xattr -r -d com.apple.quarantine csurf"
fi
echo ""

