#! /bin/sh

### update-csurf: marty sereno
# 20 Sep 2022 -- 01a: initial hack (mac64/linux64 only)
# 21 Sep 2022 -- 01b: switch site if first down

# 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

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

if [ $# -ne 1 ]; then
  echo ""
  echo "Use: update-csurf [go]"
  echo ""
  echo "  Install or update csurf in current directory"
  echo ""
  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 suff, then install new"
  echo "        (e.g., mv csurf csurf-220812)"
  #echo "  If Mac, sudo xattr to remove quarantine (req's passwd)"
  echo ""
  echo "                                                          version: 01b"
  exit
fi

# require 'go' arg, check pwd writable
if [ "$1" != "go" ]; then
  echo "update-fsaverage: ### arg ($1) was not 'go'  ...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" ]; then
    echo \
      "update-csurf: ### download/install non-x86_64 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
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: 220917")
  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?
echo ""
if [ -d csurf ]; then
  echo "Ready to update csurf in current directory:"
  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) unquarantine using xattr (req's passwd)"
  #fi
else
  echo "Ready to install csurf in current directory:"
  echo ""
  echo "  (1) website: $distdir"
  echo "  (2) download and untar newest csurf"
  #if [ `uname` = "Darwin" ]; then
  #  echo "  (3) unquarantine using xattr (req's passwd)"
  #fi
fi
echo ""
echo " => ctrl-D to go"
echo " => ctrl-C to quit"
read resp
echo ""

### 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: unpack tarfile"
echo "update-csurf: tar xvfz $dist"
                    tar xvfz $dist

# if Mac: unquarantine (not needed w/curl,tar)
#echo ""
#echo "update-csurf: Mac: unquarantine"
#if [ `uname` = "Darwin" ]; then
#  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

# done
echo ""
echo "update-csurf: done (can remove $dist to save space)"
echo ""

