#!/bin/bash
# v0.1 iterate through my fossils
# v0.2 Add in some git projects
# v0.3 Added in some more sqlite-related items
# v0.3a TODO: split this up like fossilweb.sh
# v0.4 still working on new fossilstuff function - not live yet
# v0.5 newfossilstuff got the axe. I've got to build a function to
#      cycle through $1
# v0.6 I added tips to Tcl (Tcl Improvement Proposals)
# v0.6a Added extra line between downloads
# v0.7 add libfossil, correct mistakes
# v0.8 create a rarely section for docsrc, book and others
# v0.9 Added sqlite-wasm to the rarely section, also added althttpd
# v0.10 Moved sqlite-wasm back out of rarely, due to activity.
# v0.11 Added pikchr into rarely
# v0.12 Adding params to fossil.
# v0.13 Changed out MYHOME and other vars to FOSHOME etc.
# v0.14 Added sqlite-cloudsqlite, tidied up help
# v0.15 Put in fnc, should have been in there a while. Poor thing.
# v0.16 Added in pikmojicon, pretty rarely used
# v0.17 Added in inskinerator, makes skins for fossil
# v0.18 Made note about veryrarely (created for chisel project files and the like)
# v0.19 Shifted help strings for althttpd and wasm back to sqlite/ and added wapp to tcl.
# v0.20 Added wapp to rarely after cursory examination.
# v0.21 Added REAMDE, no changes to this source as a result.
# v0.22 Added LICENCE, no changes to this source as a result.
# v0.23 Tweaked help, correcting the references to chisel.
# v0.24 Silenced pushd/popd
# v0.25 Adding "common" to cover {fossil,sqlite}-{code,forum} and tcl/tk.
# v0.26 Fixes to above for some mistakes with cd.
# v0.27 Finally removed libfossil-forum, as forum has been merged into libfossil repo.
#########
# Notes #
#########
# Really needs to be run from the source directory first
# fossil has source code, forums, pikchr and a book.
# sqlite has source code, forums, docsrc, althttpd, wasm and a testing harness
# tcl and tk each have source code, and tcl has proposals (tcl-tip)
# Cannot get TH3 source without a commercial licence, so can't run tests for docsrc
# leave libfossil out of the main loop, but call them specifically, just like fossil-book, pikchr, sqlite-wasm and sqlite-testing

FOSHOME="${HOME}/src/c/"
# Obligatory help function
function dohelp() { # needed renaming, as "help" already exists somewhere else
  echo "Welcome to the $0 help page"
  echo "$0 {all}: fetches all fossil, all sqlite, all tcl except rarely/veryrarely"
  echo "$0 common: fetches {fossil,sqlite}-{code,forum}, althttpd and tcl/tk."
  echo "$0   leaves out things in rarely/veryrarely"
  echo "### fossil stuff"
  echo "$0 fossil-[code|forum|book]: fetches named section"
  echo "$0 fossil: fetches code and forum"
  echo "### wanderinghorse stuff"
  echo "$0 libfossil{-all}: fetches libfossil (libfossil-code) and fnc"
  echo "$0 [fsl|fnc|pikchr|pikmojicon]: fetches fsl, pikchr, pikmojicon or fnc"
  echo "### sqlite stuff"
  echo "$0 sqlite: fetches sqlite fossils (code, forum, docsrc, wasm, althttpd)"
  echo "$0 sqlite-{code|forum|docsrc|tests|althttpd|wasm|cloud}, fetches:"
  echo "     sqlite code, forum, docsrc, wasm, althttpd or test scripts"
  echo "$0 althttpd: fetches althttpd code"
  echo "$0 wasm: fetches wasm-related code"
  echo "### tcl/tk"
  echo "$0 tcl-code: fetches code"
  echo "$0 tcl-tip: fetches proposals"
  echo "$0 tk-code: fetches code"
  echo "### tangentsoft projects"
  echo "$0 inskin: fetches inskinerator code"
  echo "$0 tangentsoft-fossil: fetches local fossil for tangentsoft"
  echo "### chisel project"
  echo "$0 flint: fetches the flint code for chisel server"
  echo "=== Rarely-updated stuff"
  echo "$0 rarely: updates almost all the stuff not touched by [{fossil|sqlite|libfossil}-]all"
  echo "$0 Currently, this includes the following fossil, lifossil and sqlite projects:"
  echo " fossil-book, pikchr, pikmojicon, fsl, sqlite-tests, sqlite-cloud, and cwal."
  echo "$0 Also includes tangentsoft's inskinerator and james' flint"
  echo "This list will need modifying on occasion."
  echo "=== Very rarely updated stuff (less than yearly)"
  echo "$0 veryrarely: includes the flint, fuel and fsl projects"
  echo "======= TODO"
  echo "$0 fsl: fetch from the fsl project"
  echo "$0 remove inskinerator from rarely if project updates regularly"
# Need a section in here for pull
  exit 0
}

##### fossil-scm and related stuff
function fossil-code() {
  fossil ${FOSCMD} ${FOSCMDPARAMS[*]} fossil.fossil
  echo "==== fossil-code...done ==="
}

function fossil-forum() {
  fossil ${FOSCMD} ${FOSCMDPARAMS[*]} fossilforum.fossil
  echo "==== fossil-forum...done ==="
}

# Rarely updated
function fossil-book() {
  fossil ${FOSCMD} ${FOSCMDPARAMS[*]} fossil-book.fossil
  echo "==== fossil-book...done ==="
}

function fossil-all() { # Excludes fossil-book and pikchr
  cd fossil-scm # Yes, the directory name has -scm appended
  t=fossil
  ${t}-code
  sleep 5
  ${t}-forum
  cd ..
}

###### wanderinghorse items
function libfossil-code() {
  fossil ${FOSCMD} ${FOSCMDPARAMS[*]} libfossil.fossil
  echo "==== libfossil...done ==="
}

# Commented out for now (removal soon), as forums have been merged to master
# https://fossil.wanderinghorse.net/r/libfossil/forumpost/c58421800f
# function libfossil-forum {
#  fossil ${FOSCMD} ${FOSCMDPARAMS[*]} libfossil-forum.fossil
#  echo "==== libfossil-forum...done ==="
# }

# ncurses client for fossil
function fnc {
  fossil ${FOSCMD} ${FOSCMDPARAMS[*]} fnc.fossil
  echo "==== fnc...done ==="
}

# Rarely updated
function pikchr() {
  fossil ${FOSCMD} ${FOSCMDPARAMS[*]} pikchr.fossil
  echo "==== pikchr...done ===="
}

# Rarely updated
function pikmojicon() {
  fossil ${FOSCMD} ${FOSCMDPARAMS[*]} pikmojicon.fossil
  echo "==== pikmojicon...done ===="
}

function libfossil-all {
  cd libfossil
  libfossil-code
#  libfossil-forum
  fnc
  cd -
}

# Scripting engine without a language. Not sure where to put this.
function cwal {
  fossil ${FOSCMD} ${FOSCMDPARAMS[*]} cwal.fossil
  echo "==== cwal...done ==="
}

# cson - old, but seemingly still in use
function cson {
  fossil ${FOSCMD} ${FOSCMDPARAMS[*]} cson.fossil
  echo "==== cson...done ==="
}

###### end of wanderinghorse stuff

#### Some non-fossil-project (but fossil-related) ####
# Shell-based front end to fossil, append to rarely. Hasn't been updated since 2019.
function fsl {
  fossil ${FOSCMD} ${FOSCMDPARAMS[*]} fsl.fossil
  echo "==== fsl...done ==="
}

# This is spectacularly unlikely to update further, but is here for completeness' sake
function fuel {
  fossil ${FOSCMD} ${FOSCMDPARAMS[*]} fossil-fuel.fossil
  echo "==== fuel...done ==="
}

function flint {
  fossil ${FOSCMD} ${FOSCMDPARAMS[*]} flint.fossil
  echo "==== flint...done ==="
}

function fresh {
  fossil ${FOSCMD} ${FOSCMDPARAMS[*]} Fresh.fossil
  echo "==== fresh...done ==="
}

function inskin { # Shortened from Inskinerator
  fossil ${FOSCMD} ${FOSCMDPARAMS[*]} inskinerator.fossil
  echo "==== Inskinerator...done ==="
}

function tangentsoft-fossil() {
  fossil ${FOSCMD} ${FOSCMDPARAMS[*]} tangentsoft.fossil
}

##### sqlite-hosted or sqlite-related projects
function sqlite-code() {
  fossil ${FOSCMD} ${FOSCMDPARAMS[*]} sqlite.fossil
  echo "==== sqlite-code...done ==="
}

function sqlite-forum() {
  fossil ${FOSCMD} ${FOSCMDPARAMS[*]} sqliteforum.fossil
  echo "==== sqlite-forum...done ==="
}

function sqlite-wasm() {
  fossil ${FOSCMD} ${FOSCMDPARAMS[*]} wasm.fossil
  echo "==== sqlite-wasm...done ==="
}

function sqlite-docsrc() {
  fossil ${FOSCMD} ${FOSCMDPARAMS[*]} docsrc.fossil
  echo "==== sqlite-docsrc...done ==="
}

function sqlite-althttpd() {
  fossil ${FOSCMD} ${FOSCMDPARAMS[*]} althttpd.fossil
  echo "==== sqlite-althttpd...done ==="
}

### These fossils are rarely updated. ###
function sqlite-tests() {
  fossil ${FOSCMD} ${FOSCMDPARAMS[*]} sqllogictest.fossil
  echo "==== sqlite-tests...done ==="
}

function sqlite-cloud() {
  fossil ${FOSCMD} ${FOSCMDPARAMS[*]} cloudsqlite.fossil
  echo "==== sqlite-cloudsqlite...done ==="
}

# Does everything not in "rarely" section
function sqlite-all() {
  t="sqlite"
  cd ${t}
  ${t}-code
  sleep 5
  ${t}-forum
  sleep 5
  ${t}-docsrc
  sleep 5
  ${t}-wasm
  sleep 5
  ${t}-althttpd # shifted from rarely()
#  ${t}-tests # shifted to rarely()
#  ${t}-cloud # shifted to rarely()
  unset $t
  cd .. # gets us back to ${MYHOME}
}

# Updates tcl sourcecode
function tcl-code() {
  fossil ${FOSCMD} ${FOSCMDPARAMS[*]} tcl.fossil
  echo "==== tcl-code...done ==="
}

# Updates tcl TIP database
function tcl-tips() {
  fossil ${FOSCMD} ${FOSCMDPARAMS[*]} tips.fossil
  echo "==== tcl-tips...done ==="
}

function wapp() {
  fossil ${FOSCMD} ${FOSCMDPARAMS[*]} wapp.fossil
  echo "==== wapp...done ==="
}

# Updates tk sourcecode
function tk-code() {
  fossil ${FOSCMD} ${FOSCMDPARAMS[*]} tk.fossil
  echo "==== tk code...done ==="
}

# Updates both tcl and tk sources
function tcl-all() {
  cd tcl
  tcl-code
  sleep 5
  tcl-tips   # TCL Improvement Proposals, rarely updated
  sleep 5
  wapp
  sleep 5
  cd ../tk
  tk-code
  cd .. # gets us back to ${MYHOME}
}

# these don't get updated very often, if at all
function rarely() {
# fossil stuff
  cd fossil-scm
  fossil-book
# sqlite stuff
  cd ../sqlite
  sqlite-tests
  sqlite-cloud
# wanderinghorse - a.k.a. libfossil
  cd ../libfossil
  cwal
  pikchr
  # fsl and pikmojicon moved to veryrarely
# other stuff
  cd ../Fresh
  fresh
  cd ..
# tangentsoft (not tangentsoft-fossil)
  cd ${HOME}/src/perl/hosts/tangentsoft
  inskin
  cd -  # should land me back in /h/v/src/c
}

function veryrarely() { # for stuff that hasn't updated in years
  cd fossil-scm
  fuel # really really rare chance to update
  cd -
  cd libfossil
  fsl
  pikmojicon
  cd -
  cd ${HOME}/src/php/hosts/james
  flint
  cd -
}

function tangentsoft-all() {
  # only one entry at the moment
  cd ${HOME}/src/perl/hosts/tangentsoft
  inskin
  cd - # should land me back in /h/v/src/c
  cd ${HOME}/src/c/hosts/tangentsoft/
  tangentsoft-fossil
  cd -
}

# Meant to be like all, but misses out sqlite docs, tips and other stuff
function common() {
  # First fossil
  fossil-all # this changes dir at end of fossil-all
  # then sqlite - can't use sqlite-all as it includes too much
  t="sqlite"
  cd ${t}
  ${t}-code
  sleep 5
  ${t}-forum
  sleep 5
  ${t}-althttpd
  sleep 5
  # now tcl/tk
  cd ../tcl
  tcl-code
  sleep 5
  cd ../tk
  tk-code
  cd .. # gets us back to ${MYHOME}
}

function all() { # These each have a cd in them
# Note: does not include fsl/libfossil
  fossil-all # code, forum, not book
  sleep 5
  sqlite-all # code, forum, althttpd, not tests
  sleep 5
  tcl-all    # code (tcl/tk), TCL Improvement Proposals (tip)
  sleep 5
  # tangentsoft-all has two paths, so we're not using it here
  cd ${HOME}/src/c/hosts/tangentsoft/
  tangentsoft-fossil # doesn't include inskinerator (under rarely)
  cd -
}
# Takes path arg

pushd "${FOSHOME}" >/dev/null
# Let's add something to FOSCMD to make the operation specific
# the caseloop below processes in order. -u needs to be first if
# we want to sync unversioned artifacts
declare FOSCMDPARAMS
# Was here for getting everything and breadcrumbing where I was
# FOSCMDPARAMS=("-v" "--verily" "-R")
# Strip it back a bit, I think.
FOSCMDPARAMS=("-R")

FOSCMD="pull" # default value unless it gets changed by -u
if [ ${#*} -lt 1 ]; then # I want it all
  all # sleep is built in between stages
else #iterate, chuck it in if keyword isn't recognised.
  for t in ${*}; do
    case "${t}" in
    "-u"|"--unversioned")
      # sync can't push without user perms on remote, but can still pull unversioned artifacts
      FOSCMDPARAMS=("-u" "-v" "--verily" "-R")
      FOSCMD="sync"
#      dohelp
    ;;
    "help"|"-h"|"--help") # might as well support the long option too
      dohelp # exits
    ;;
# Each one of these handles path changing for itself
    "rarely"|"veryrarely"|"all"|"common")
      "${t}"
    ;;
    "fossil"|"sqlite"|"tcl"|"libfossil")
      "${t}"-all
    ;;
# All fossil-related stuff
    "fossil-forum"|"fossil-code"|"fossil-book"|"fuel")
      cd fossil-scm
      "${t}" # calls the function directly. I'll have to see if this works.
      cd ..
    ;;
    "book")
      cd fossil-scm
      fossil-book
      cd -
    ;;

# Some non-fossil stuff (hosted in a fossil)
    "fresh"|"Fresh")
      cd Fresh
      fresh
      cd ..
    ;;
# the host software for chisel (very rarely updated)
    "flint")
      cd ${HOME}/src/php/hosts/james
      flint
      cd -
    ;;
# and tangentsoft
    "inskin"|"Inskin"|"inskinerator"|"Inskinerator")
      cd ${HOME}/src/perl/hosts/tangentsoft
      inskin
      cd -
    ;;
    "tangentsoft-fossil")
      cd ${HOME}/src/c/hosts/tangentsoft
      tangentsoft-fossil
      cd -
    ;;
    "tangentsoft-all") 
      cd ${HOME}/src/c/hosts/tangentsoft
      tangentsoft-fossil
      cd -
    ;;

# All wanderinghorse stuff
    "libfossil-all")
      libfossil-all # should do libfossil-code and fnc but not libfossil-forum
    ;;
    "libfossil-code"|"cwal"|"cson"|"fsl"|"fnc"|"pikchr"|"pikmojicon")
      cd libfossil
      ${t} # Seems a bit redundant to make this a full function call
      cd ..
    ;;
# All sqlite-related stuff
    "sqlite-code"|"sqlite-forum"|"sqlite-docsrc"|"docsrc"|"sqlite-tests"|"sqlite-althttpd"|"sqlite-wasm")
      cd sqlite
      "${t}"
      cd -
    ;;
    "althttpd"|"httpd") # whoops, is this duplicated with the above section?
      cd sqlite
      sqlite-althttpd
      cd ..
    ;;
    "docsrc")
      cd sqlite
      sqlite-docsrc
      cd ..
    ;;
    "wasm")
      cd sqlite
      sqlite-wasm
      cd ..
    ;;
    "cloud"|"cloudsqlite"|"sqlite-cloud")
      cd sqlite
      sqlite-cloud
      cd ..
    ;;
# All tcl/tk-related stuff
    "tcl"|"tcl-all")
      tcl-all
    ;;
    "tcl-code"|"tcl-tips"|"wapp")
      cd tcl
      "${t}"
      cd -
    ;;
    "tk")
      cd "${t}"
      "${t}"-code  # Only the code at the moment.
      cd -
    ;;
    "tk-code")
      cd tk; pwd
      "${t}"
      cd -
    ;;
    esac # end of case ${t}
  done
fi
popd >/dev/null
