#!/bin/bash
#
# hdrff: HDR für Faule - automatic processing of HDR image-sequences
# 
# This is the main driver script. Nothing really happens here, since 
# the driver only loads the available modules and executes them.
# 
# Modules are in $PREFIX/lib/hdrff/*.inc. Every module foo
# must implement the bash-functions
#   foo_help:  print module-help
#   foo_check: check prereqs of module
#   foo:       module-code
# User-modules are in $HOME/.hdrff/*.inc
# 
# Configuration-files:
#   - program defaults: $PREFIX/lib/hdrff/hdrff.conf
#   - system defaults:  /etc/hdrff.conf 
#   - user defaults:    ~/.hdrff/hdrff.conf
# 
# $Author: bablokb $
# $Revision: 1.26 $
#
# License: GPL3
# -----------------------------------------------------------------------------

pgmDir=`dirname $0`
libDir="$pgmDir"/../lib/hdrff

# load functions
. "$libDir/functions.inc"

# usage   ---------------------------------------------------------------------

usage() {
   echo -e "\n`basename $0`: create LDR/HDR-images from RAW or JPG images\n\
  \nusage: `basename $0` [options] [module ...]\n\
  possible options:\n\
    -C cfg-file  read configuration from file cfg-file\n\
    -c           check prerequisites of modules\n\
    -H           show module help\n\
    -q           output only error messages\n\
    -v           verbose output\n\
    -h           show this help\n\
    -d modules   list of modules to debug\n\
" >&2
  exit 3
}

# set defaults   --------------------------------------------------------------

setDefaults() {
  verbose=-1
  debug="nothing"
  modules="$MODULES"
  showModuleHelp=0
  checkPrereqs=0
  cfgFile=""
  RC_OK=0
  RC_WARN=1
  RC_ERR=3
  RC_EXIT=12
}

# parse arguments and set variables -------------------------------------------

parseArguments() {
  while getopts ":C:cHvqhd:" opt; do
    case $opt in
      C) cfgFile="$OPTARG";;
      c) checkPrereqs=1;;
      H) showModuleHelp=1;;
      v) verbose=1;;
      q) verbose=0;;
      h) usage;;
      d) debug="$OPTARG";;
      ?) usage;;
    esac
  done
  shift $((OPTIND-1))
  if [ -n "$1" ]; then
    modules="$@"
  elif [ $showModuleHelp -eq 1 -o $checkPrereqs -eq 1 ]; then
    modules="allModules"
  fi
}

# load all HDRFF-modules (every module is a bash-function)   ------------------

loadModules() {
  allModules=""
  local mod="hdrff"                     # to enable debugging, set DEBUG=hdrff
  for f in "$libDir"/*.inc; do
    if [ -f "$f" ]; then
      msg "debug" "loading $f"
      . "$f"
    fi
  done
  for f in "$HOME/.hdrff"/*.inc; do
    if [ -f "$f" ]; then
      msg "debug" "loading $f"
      . "$f"
    fi
  done
}

# run module-function   -------------------------------------------------------

runModuleFunc() {
  local name="$1" mod rc=0 rc_new
  for mod in $modules; do
    if declare -F "$mod$name" > /dev/null; then
      $mod$name
      rc_new=$?
      [ $rc_new -gt $rc ]      && rc=$rc_new
      [ $rc_new -eq $RC_EXIT ] && break
    else
      msg "error" "module-function $mod$name not implemented!"
      rc=$RC_ERR
    fi
  done
  return $rc
}

# show module-specific help   -------------------------------------------------

showModuleHelp() {
  local rc=0
  runModuleFunc "_help"
  rc=$?
  echo -e "\nModule groups:\n" >&2
  declare -p | cut -d" " -f 3- | grep Modules | tr -s '=" ' '\n\t '
  exit $rc
}

# check prereqs of every module   ---------------------------------------------

checkPrereqs() {
  runModuleFunc "_check"
  exit $?
}

# run modules   ---------------------------------------------------------------

runModules() {
  mod=hdrff msg "info" "processing modules $modules"
  runModuleFunc ""
  return $?
}

# main program (execute all modules) ------------------------------------------

setDefaults
parseArguments "$@"
loadConfiguration "$cfgFile"
fixConfiguration
loadModules
resolveModuleNames
[ $showModuleHelp -eq 1 ] && showModuleHelp
[ $checkPrereqs -eq 1 ] && checkPrereqs
runModules
rc=$?
mod=hdrff msg "info" "exiting with rc=$rc"
exit $rc

