#!/bin/sh
# ====================[ oddmuse-test                       ]====================
#                     [ Time-stamp: "2008-12-20 18:02:38 leycec" ]             
#
# A Bourne shell script copying one Oddmuse module from the source to target
# repository and, if an appropriate unit test file is found for that module,
# running that unit test file.

# ....................{ CONFIGURATION                      }....................
SOURCE_ROOT="$HOME/mnt/nfsn/wiki"
TARGET_ROOT="$HOME/pub/code/oddmuse.org"

SOURCE_CORE="$SOURCE_ROOT/../wiki.pl"
TARGET_CORE="$TARGET_ROOT/wiki.pl"
  
SOURCE_MODULES="$SOURCE_ROOT/modules"
TARGET_MODULES="$TARGET_ROOT/modules"

TARGET_TESTS="$TARGET_ROOT/t"

# ....................{ CONSTANTS                          }....................
SCRIPT_NAME=$(basename "$0")
SCRIPT_VERSION="0.0.1"

# ....................{ I/O HANDLING                       }....................
mutter() {
  echo -n "${SCRIPT_NAME}: $*"
}

utter() {
  if [ -n "$IS_CRON" ]
  then logger -p cron.notice "${SCRIPT_NAME}: $*"
  else echo "${SCRIPT_NAME}: $*"
  fi
}

curse() {
  if [ -n "$IS_CRON" ]
  then logger -p cron.err "${SCRIPT_NAME}! $*"
  else echo "${SCRIPT_NAME}! $*" 1>&2
  fi
}

# ....................{ ERROR HANDLING                     }....................
try() {
  $*
  [ $? = 0 ] || exit 1
}

die() {
  curse $*
  exit 1
}

# ....................{ USAGE                              }....................
echo_usage_error() {
  echo_usage
  exit 1
}

echo_usage_noerror() {
  echo_usage
  exit 0
}

echo_usage() {
  echo "Usage: ${SCRIPT_NAME} [options] file...
             options
  -h, --help    Show this help.
  -c, --commit  Copy the passed files from the source to destination repository.
  -r, --revert  Copy the passed files from the destination to source repository.
  -t, --test    Copy the passed files from the source to destination repository,
                if needed, and test those files in the destination repository."
}

# ....................{ COPYING                            }....................
copy_oddmuse_file() {
  SOURCE_FILE_="$1"
  TARGET_FILE_="$2"

  if [ -e "$SOURCE_FILE_" ]; then
    if [ -d "$TARGET_FILE_" ]; then
      TARGET_FILE_="$TARGET_FILE_/"$(basename "$SOURCE_FILE_")
    fi
      
    if [ -e "$TARGET_FILE_" ]; then
      if [ $(get_md5sum "$SOURCE_FILE_") != $(get_md5sum "$TARGET_FILE_") ]; then
        utter "copying '$SOURCE_FILE_' -> '$TARGET_FILE_'..." 
        command cp --force "$SOURCE_FILE_" "$TARGET_FILE_"
      fi
    else die "target file '$TARGET_FILE_' not found!"
    fi
  else   die "source file '$SOURCE_FILE_' not found!"
  fi
}

copy_oddmuse_files() {
  SOURCE_CORE_="$1"
  TARGET_CORE_="$2"
  SOURCE_MODULES_="$3"
  TARGET_MODULES_="$4"

  if [ "$BASENAMES" = "*" ]; then
    copy_oddmuse_file "$SOURCE_CORE_" "$TARGET_CORE_"

    for SOURCE_MODULE in $SOURCE_MODULES_/*.pl; do
      copy_oddmuse_file "$SOURCE_MODULE" "$TARGET_MODULES_"
    done
  else
    for BASENAME in $BASENAMES; do
      if [ "$BASENAME" = "wiki" ]; then
        copy_oddmuse_file "$SOURCE_CORE_" "$TARGET_CORE_"
      else
        SOURCE_MODULE="$SOURCE_MODULES_/$BASENAME.pl"

        if [ -e "$SOURCE_MODULE" ]; then
          copy_oddmuse_file "$SOURCE_MODULE" "$TARGET_MODULES_"
        else
          utter "'$SOURCE_MODULE' not found; skipping!" 
        fi
      fi
    done
  fi
}

# ....................{ ACTIONS                            }....................
get_md5sum() {
  md5sum $1 | cut --fields=1 --delimiter=' '
}

test_oddmuse() {
  [ -e "$SOURCE_CORE" ] &&
    copy_oddmuse_file "$SOURCE_CORE" "$TARGET_CORE"

  if [ "$BASENAMES" = "*" ]; then
    utter "testing all Oddmuse modules..."

    pushd "$TARGET_ROOT"
    nice make test
    popd
  else
    for MODULE_NAME in $BASENAMES; do
      SOURCE_FILE="$SOURCE_MODULES/$MODULE_NAME.pl"
      TARGET_FILE="$TARGET_MODULES/$MODULE_NAME.pl"
      TARGET_TEST="$TARGET_TESTS/$MODULE_NAME.t"

      if [ -e "$SOURCE_FILE" ]; then
        copy_oddmuse_file "$SOURCE_FILE" "$TARGET_FILE"
      fi

      if [ -e "$TARGET_TEST" ]; then
        utter "testing '$TARGET_TEST'..." 
        pushd "$TARGET_ROOT"
        nice perl "$TARGET_TEST" 
        popd
      else
        utter "'$TARGET_TEST' not found; skipping!" 
      fi
    done
  fi
}

commit_oddmuse() {
  copy_oddmuse_files "$SOURCE_CORE"    "$TARGET_CORE" \
                     "$SOURCE_MODULES" "$TARGET_MODULES"
}

revert_oddmuse() {
  copy_oddmuse_files "$TARGET_CORE"    "$SOURCE_CORE" \
                     "$TARGET_MODULES" "$SOURCE_MODULES"
}

# ....................{ OPTIONS                            }....................
OPTIONS=$(getopt --unquoted \
  --longoption 'commit,revert,test,help' \
  --options    '+c,r,t,h' --shell sh -- "${@}")
[ $? = 0 ] || echo_usage_error

set -- $OPTIONS

while [ $# -gt 0 ]; do
  case "$1" in
    --help)   echo_usage_noerror;;
    -h)       echo_usage_noerror;;
    --commit) ACTION="commit"; shift;;
    -c)       ACTION="commit"; shift;;
    --revert) ACTION="revert"; shift;;
    -r)       ACTION="revert"; shift;;
    --test)   ACTION="test"; shift;;
    -t)       ACTION="test"; shift;;
    --)       shift; break;;
    -*)       echo_usage_error;;
    *)        break;;
  esac
done

BASENAMES="$@"

# ....................{ MAIN                               }....................
utter "v${SCRIPT_VERSION}"
echo ""

[ -n "$1" ] || die "no filename(s) provided!"

case "$ACTION" in
  "test")   test_oddmuse;;
  "commit") commit_oddmuse;;
  "revert") revert_oddmuse;;
esac

# --------------------( COPYRIGHT AND LICENSE              )--------------------
# The information below applies to everything in this distribution,
# except where noted.
#              
# Copyleft 2008 by B.w.Curry.
#   
#   http://www.raiazome.com
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
