#!/bin/bash -x

set -e

D1_LOG_DIR=/var/log/dataone
D1_LOG_FILE=dataone-cn-dev-scripts.install.log
SOURCE_DIR=/usr/share/dataone_cn_dev_scripts
DEST_DIR=/usr/local/bin/CnScripts

function log()
{
  #
  # Set Up logging
  # Reminder: don't echo to stdout, it messes up debconf
  #
  if [ ! -e ${D1_LOG_DIR} ]; then
      mkdir -p ${D1_LOG_DIR}
      chown tomcat7:tomcat7 ${D1_LOG_DIR}
  fi
  now=$(date "+%Y-%m-%d %H:%M:%S %Z: ")
  echo -e "${now} postinst $@" >> ${D1_LOG_DIR}/${D1_LOG_FILE}
}

if [ ! -e ${DEST_DIR} ] 
then
  if (mkdir -p ${DEST_DIR}  >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then
	log "mkdir -p ${DEST_DIR} success"
  fi
fi

## backup the old war file
if [ -e ${DEST_DIR} ]
then
  if (cp ${SOURCE_DIR}/bash/* ${DEST_DIR} >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then 
    log "copied all bash scripts" 
  else
    exit 1
    log "failed to copy bash scripts"
  fi
  if (cp ${SOURCE_DIR}/perl/* ${DEST_DIR} >> ${D1_LOG_DIR}/${D1_LOG_FILE} 2>&1); then 
    log "copied all perl scripts" 
  else
    log "failed to copy perl scripts"
    exit 1
  fi
fi

exit 0
