#! /bin/bash

function usage() {
cat << \EoF

The idea here is to have a private 'development' version of gnumeric,
separate from the standard system version. Rationale:
-- This allows testing of new features, including comparing the
various versions.
-- Users not involved in testing see only the standard version and are
not bothered by the testing.
-- Root privileges are not required for routine development work.

This requires a lot of steps, some of which are not obvious and not
documented anywhere AFAICT.

Note that gnumeric depends on having goffice /installed/ and goffice
depends on having libgsf /installed/. That is to say, merely compiling
the libraries is not sufficient.

Required installs include:
  libgtk-3-dev
  librsvg2-dev
  libxslt1-dev
  intltool
  itstool
  gtk-doc-tools

Configuration options are controlled by ./confect
The development versions can be invoked via: ./SS and ./GG

Usage:
  ./WHENCE [verb ...] [-s option ...]

Multiple verbs can be specified; possibilities include:
    getlibs     # needed only once; subjects (if any) are ignored
    clone       # needed only once per subject
    pull
    confect     # the default
    make        # for libraries (but not main prog) implies install
    install     # assumes subject is 'gnumeric';
                #     explicit subjects (if any) are ignored

Multiple subjects can be specified; the default is all of these:
    libgsf
    goffice
    gnumeric

Typical workflow:
    ./WHENCE pull confect make install

The ordering matters for verbs as well as subjects.
EoF
}

# Also needed:
function my_getlibs() {
  apt-get install libgirepository1.0-dev
}

# Initially, clone and initialize the three repos:

function my_clone () {
  top="$PWD"
  for repo in $*; do
    cd $top
    git clone git@gitlab.gnome.org:GNOME/$repo   # or $repo.git
    cd ./$repo

  # branch for tracking upstream activities:
    git branch -m upstream_master
  # branch for local work:
    git checkout -b master
# Do not apply git-init+; it just makes a mess.
#
# Especially not to the upstream tracking branch;
# otherwise git-pull will refuse to merge unrelated histories
  done
}

function my_confect () {
  top="$PWD"
  for repo in $* ; do
    cd ./$repo
    if test -r ../confect-$repo ; then
      ln -fs ../confect-$repo ./confect
    else
      ln -fs ../confect-proto ./confect
    fi
    ./confect
  done
}

# After the first time, that initialization is not repeated.
# Instead:

function my_pull() {
  top="$PWD"
  for repo in $* ; do
    cd $top/$repo
  # pull latest updates into upstream_master:
    git-pull+
  done
}

function my_confect() {
  top="$PWD"
  for repo in $* ; do
    cd $top/$repo
    ../confect          # writes its own log file
  done
}

function my_make() {
  top="$PWD"
  for repo in $* ; do
    cd $top/$repo
    cmd='time make'
    script -c "echo ':; $cmd' ; $cmd" make.logg
# Libraries must be installed before
# making the next step.
# In contrast, the final gnumeric install is not done here
# because it may require root privileges:
    if ! test -f gnumeric.desktop ; then
      cmd='time make install'
      script -c "echo ':; $cmd' ; $cmd" install.logg
    fi
  done
}

function my_install() {
# keep in mind that my_make does a "make install"
# do only one install here, no matter how many subjects are specified:
  if test -r .did_install ; then return ; fi
  for repo in gnumeric ; do
    echo "Currently PWD: $PWD ... about to cd"
    cd $repo
    script -c 'time make -k install' install.logg
  done
# Are we root?
  if test -w /etc ; then
    ldconfig        # otherwise .so files may be not found
  fi
  touch .did_install
}

set -e
subjects=
verbs=

# parse command-line options:
while test -n "$*" ; do
  cmd="$1" ; shift
  case "$cmd" in
    -s)
      subjects="$subjects $1" ; shift
    ;;
    *)
      verbs="$verbs $cmd"
    ;;
  esac
done    # end of parsing

# apply the defaults:
: ${subjects:=libgsf goffice gnumeric}
: ${verbs:=confect}

## echo "verbs: '$verbs'  subjects: '$subjects'"

# outer product: apply every verb to every subject
rm -f .did_install
for verb in $verbs ; do
  case $verb in
    help|--help|-h)
      usage
      exit
  esac
  (     # protect against errant cd
    my_$verb $subjects
  )
done
