#! /bin/sh

### mkcsurflist: update webserver distro list of latest tgz for each arch
# 22 Sep 2022 -- 01a: initial hack
# 05 Oct 2023 -- 01b: don't req irritating 'go', but can use

# location of distribution on webserver
csurfdir=~/public_html/csurf           # pages
#csurfdir=~/public_html/sereno/csurf   # rohancp

# update-csurf currently looks for "$csurfdir/.latestcsurf.txt"
listfile=.latestcsurf.txt

tmpfile=/tmp/TmpCsurfDistList.txt
# use
echo ""
echo "Use: mkcsurflist [go]      [mk $listfile w/newest csurf dist each arch]"
echo ""
if [ -e $csurfdir/$listfile ]; then
  echo "here is EXISTING $listfile:"
  echo "---------------------------------------------------"
  echo "`cat $csurfdir/$listfile`"
  echo "---------------------------------------------------"
  echo ""
fi

if [ ! -e $csurfdir ]; then
  echo "mkcsurflist: ### local dist dir: $csurfdir not found"
  echo ""
  exit
fi

# OK to go?
askgo=1
if [ $# -eq 1 ] && [ "$1" = "go" ]; then askgo=0; fi
if [ $askgo = 1 ]; then
  echo "OK to update $listfile to newest dated files?"
  echo ""
  echo " => ctrl-D to go"
  echo " => ctrl-C to quit"
  read resp
  echo ""
fi

# find latest
cd $csurfdir
ls csurf0.8-*.tgz > $tmpfile   # ls sorts
# grep may return several
mac=`grep -s "\-mac\-" $tmpfile`
for file in $mac; do
  macn=$file
done
macPPC=`grep -s "\-macPPC\-" $tmpfile`
for file in $macPPC; do
  macPPCn=$file
done
linux32=`grep -s "\-linux32\-" $tmpfile`
for file in $linux32; do
  linux32n=$file
done
linux64=`grep -s "\-linux64\-" $tmpfile`
for file in $linux64; do
  linux64n=$file
done
irix=`grep -s "\-irix\-" $tmpfile`
for file in $irix; do
  irixn=$file
done

# write listfile in csurfdir
datetxt=`date`
echo "# ${listfile}: $datetxt" > $listfile
if [ "$mac" != "" ];     then echo $macn     >> $listfile; fi
if [ "$macPPC" != "" ];  then echo $macPPCn  >> $listfile; fi
if [ "$linux64" != "" ]; then echo $linux64n >> $listfile; fi
if [ "$linux32" != "" ]; then echo $linux32n >> $listfile; fi
if [ "$irix" != "" ];    then echo $irixn    >> $listfile; fi

echo "made NEW $csurfdir/$listfile"
echo "---------------------------------------------------"
cat $listfile
echo "---------------------------------------------------"
echo ""

rm -f $tmpfile

