#!/bin/sh

###################################################
# A dubious script which commits generated website
# contents to xml-site/targets/$MODULE 
###################################################
. `dirname $0`/local-vars

function log() {
  echo "%% $@"
}

MODULE=${1:-xml-forrest}
case $MODULE in
  xml-forrest)          REPOS="xml-site"    ; TARGET="forrest" ;;
  xml-fop)          REPOS="xml-site"    ; TARGET="fop" ;;
  xml-forrest-template) REPOS="xml-site"    ; TARGET="forrest-sample" ;;
  xml-site)             REPOS="xml-site"    ; TARGET="/" ;;
  avalon-phoenix)       REPOS="avalon-site" ; TARGET="phoenix" ;;
  avalon-site)       REPOS="avalon-site" ; TARGET="/" ;;
#  cocoon-site)       REPOS="xml-site"    ; TARGET="cocoon"
esac
log "MODULE is $MODULE"
[ "$MODULE" = "xml-site" ] && ARGS=-l
[ "$MODULE" = "avalon-site" ] && ARGS=-l
log "ARGS is $ARGS"

SRC_DIR=$WEBAPP/sites/$MODULE
[ "$REPOS" = "xml-site" ] && LIVECVS=$XML_SITE/targets/$TARGET
[ "$REPOS" = "avalon-site" ] && LIVECVS=$AVALON_SITE/site/$TARGET

function sanity_checks()
{
  [ -z "$TARGET" ] && echo "Unknown module: $MODULE" && exit;

  if [ "$REPOS" = "xml-site" -a -z "$XML_SITE" ]; then
    echo "Please set the XML_SITE var in local-vars-`uname -n`"
    exit
  fi	
  if [ "$REPOS" = "avalon-site" -a -z "$AVALON_SITE" ]; then
    echo "Please set the AVALON_SITE var in local-vars-`uname -n`"
    exit
  fi	
  if [ ! -d "$SRC_DIR" ]; then
    echo "Expected website contents in $SRC_DIR"
    exit
  fi
  if [ ! -d "$LIVECVS" ]; then
    echo "Expected checked-out $REPOS/ HTML in non-existent directory $LIVECVS"
    exit
  fi
}

function copy_contents()
{
  log "Updating $LIVECVS from $SRC_DIR"
  pushd .
  cd $LIVECVS
  cvsco $ARGS
  cvs up $ARGS -dP
  log "Copying generated docs to $REPOS.."
  log "  src  : $SRC_DIR/*"
  log "  dest : $PWD" 
  cp -r -p $SRC_DIR/* .
}


function addfiles()
{
  log "addfiles '$@'"
  local TXTFILES
  local BINFILES
  local DIRS
  for i in $@; do
    #echo "Processing $i.."
    if [ -d "$i" ]; then
      log "Directory: $i"
      DIRS="$DIRS $i"
      continue
    fi
    local MIME=`file -bi $i 2>/dev/null`
    unset istext
    log $MIME | grep text > /dev/null && istext="yes"
    if [ "$istext" = "yes" ]; then
      log "Adding as TEXT ($MIME): $i"
      TXTFILES="$TXTFILES $i"
    else
      log "Adding as BINARY ($MIME): $i"
      BINFILES="$BINFILES $i"
    fi
  done
  [ ! -z "$TXTFILES" ] && cvsdo add $TXTFILES
  [ ! -z "$BINFILES" ] && cvs add -kb $BINFILES
  if [ ! -z "$DIRS" ]; then
    log "Processing dirs $DIRS"
    for d in $DIRS; do
      log "Processing dir $d"
      unset newfiles ; newfiles=`find $d -type f -not -path "*CVS*"`
      unset newdirs ; newdirs=`find $d -type d -not -name "CVS" | tr '\n' ' '`
      log "  dirs: '$newdirs'"
      log "  files: '$files'"
      cvs add $newdirs
      addfiles $newfiles
    done
  fi
  unset TXTFILES BINFILES DIRS
}

function update_rootfiles()
{ 
  log "Generating list of new-to-CVS $REPOS files in $PWD.."
  NEW_FILES=`(cvs up -l ;cvs up images skin) | grep '^\?' | cut -d\  -f 2 | tr '\n' ' '`
  log "Adding new files to CVS: '$NEW_FILES'"
  [ ! -z "$NEW_FILES" ] && addfiles $NEW_FILES
}

function commit()
{
  log "Now committing to CVS"
  [ "$MODULE" = "xml-site" ] && commitfiles="`(cvs up -l ;cvs up images skin) | cut -d\  -f 2 | tr '\n' ' '`"
  [ "$MODULE" = "avalon-site" ] && commitfiles="`(cvs up -l ;cvs up components developing history pmc project seca containers whoweare) | cut -d\  -f 2 | tr '\n' ' '`"
  log Committing $commitfiles
  cvs ci -m "Automatic publish at `date` from http://forrestbot.cocoondev.org."
  popd
  log "$REPOS update of '$MODULE' complete!"
}

######################################################
## Script body starts here
######################################################


# Check the module exists and all required variables are set
sanity_checks

# Copy generated content across to to the CVS module checkout
copy_contents

if [ "$MODULE" = "xml-site" -o "$MODULE" = "avalon-site" ]; then
  # Special handling for the xml.apache.org pages, which are directly in the
  # targets/ directory
  update_rootfiles

else 
  log "Generating list of new-to-CVS $REPOS files in $PWD.."
  NEW_FILES=`cvs up | grep '^\?' | cut -d\  -f 2`
  log "Adding new files to CVS: $NEW_FILES"
  [ ! -z "$NEW_FILES" ] && addfiles $NEW_FILES
fi

echo $PWD
echo cvs up $ARGS
cvs up $ARGS > /tmp/cvslist 2>&1

# Commit copied and added files
commit

