#!/bin/sh
#
# #%L
# OME Files C++ libraries (shell wrapper)
# %%
# Copyright © 2015 Open Microscopy Environment:
#   - Massachusetts Institute of Technology
#   - National Institutes of Health
#   - University of Dundee
#   - Board of Regents of the University of Wisconsin-Madison
#   - Glencoe Software, Inc.
# %%
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and documentation are
# those of the authors and should not be interpreted as representing official
# policies, either expressed or implied, of any organization.
# #L%

set -e

# Installation prefix (if any)

INSTALL_PREFIX=""

# Relative installation paths

INSTALL_BINDIR="bin"
INSTALL_SBINDIR="sbin"
INSTALL_LIBEXECDIR="libexec"
INSTALL_SYSCONFDIR="etc"
INSTALL_SHAREDSTATEDIR="com"
INSTALL_LOCALSTATEDIR="var"
INSTALL_LIBDIR="lib"
INSTALL_INCLUDEDIR="include"
INSTALL_OLDINCLUDEDIR="/usr/include"
INSTALL_DATAROOTDIR="share"
INSTALL_DATADIR="share"
INSTALL_INFODIR="share/info"
INSTALL_LOCALEDIR="share/locale"
INSTALL_MANDIR="share/man"
INSTALL_DOCDIR="share/doc/ome-files"

OME_FILES_INSTALL_DATADIR=""
OME_FILES_INSTALL_PKGLIBEXECDIR="libexec/ome/files"

# Release version

VERSION_MAJOR="0"
VERSION_MINOR="2"
VERSION_PATCH="2"
VERSION_EXTRA=""
RELEASE_DATE="2016-10-04 15:20:57 +0100"

# Program invoked as and command requested (if any)

prog=$(readlink -f "$0" 2>/dev/null \
  || perl -MCwd -le 'print Cwd::abs_path(shift)' "$0" 2>/dev/null \
  || echo "$0")

if [ "$#" -gt 0 ]; then
  cmd=$1
  shift
fi

# Determine installation prefix if unset, then determine paths for
# programs, libraries, manpages etc.

realpath() {
  echo "$(cd "$(dirname "$1")"; pwd)/$(basename "$1")"
}

if [ -n "$INSTALL_PREFIX" ] && [ -d "$INSTALL_PREFIX" ]; then
    :
else
    INSTALL_PREFIX="$(dirname "$(realpath "$prog")")"
    INSTALL_PREFIX="${INSTALL_PREFIX%/${INSTALL_BINDIR}}"
    BINPATH="${INSTALL_PREFIX}/${INSTALL_BINDIR}"
fi

case "$(uname -s)" in
    Linux)
        INSTALL_PREFIX="$(readlink -f "$INSTALL_PREFIX")"
        ;;
    *BSD*)
        INSTALL_PREFIX="$(stat -f%R "$INSTALL_PREFIX")"
        ;;
esac

PATH="${INSTALL_PREFIX}/$INSTALL_BINDIR:$PATH"
LIBEXECDIR="${INSTALL_PREFIX}/$OME_FILES_INSTALL_PKGLIBEXECDIR"
MANDIR="${INSTALL_PREFIX}/$INSTALL_MANDIR"
if [ "$(uname -s)" = "Darwin" ]; then
  if [ -z "$DYLD_FALLBACK_LIBRARY_PATH" ]; then
    export DYLD_FALLBACK_LIBRARY_PATH="${INSTALL_PREFIX}/$INSTALL_LIBDIR"
  else
    export DYLD_FALLBACK_LIBRARY_PATH="${INSTALL_PREFIX}/$INSTALL_LIBDIR:$DYLD_FALLBACK_LIBRARY_PATH"
  fi
else
  if [ -z "$LD_LIBRARY_PATH" ]; then
    export LD_LIBRARY_PATH="${INSTALL_PREFIX}/$INSTALL_LIBDIR"
  else
    export LD_LIBRARY_PATH="${INSTALL_PREFIX}/$INSTALL_LIBDIR:$LD_LIBRARY_PATH"
  fi
fi

usage() {
  cat <<EOF
usage: $prog [--help] [--usage] <cmd> [<args>]

available ome-files commands in '$LIBEXECDIR'

EOF
  ls -C "$LIBEXECDIR" | sed -e 's;^;	;'
}

version() {
  cat <<EOF
ome-files (OME Files) $VERSION_MAJOR.$VERSION_MINOR.$VERSION_PATCH$VERSION_EXTRA ($RELEASE_DATE)
Copyright © 2006–2016 Open Microscopy Environment

This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
EOF
}

help() {
  exec man -M "$MANDIR" 1 ome-files
  exit 1
}

run() {
  runcmd="$1"
  shift

  realcmd="$runcmd"
  if [ "$realcmd" = "info" -o "$realcmd" = "showinf" ]; then
    realcmd=info
  fi
  if [ "$realcmd" = "view" -o "$realcmd" = "glview" ]; then
    realcmd=view
  fi
  realcmd="$LIBEXECDIR/$realcmd"

  if [ ! -x "$realcmd" ]; then
    echo "ome-files: '$runcmd' is not a ome-files-test command.  See 'ome-files-test --help'" >&2
    exit 1
  fi

  exec "$realcmd" "$@"
  exit 1
}

# Special case options

if [ -z "$cmd" ]; then
  usage >&2
  exit 1
fi
if [ "$cmd" = "--help" -o "$cmd" = "-h" ]; then
  help
  exit 1
fi

if [ "$cmd" = "--version" -o "$cmd" = "-V" ]; then
  version
  exit 0
fi

if [ "$cmd" = "--usage" -o "$cmd" = "-u" -o -z "$cmd" ]; then
  usage
  exit 0
fi

# Run subcommand
run "$cmd" "$@"
exit 1
