#!/bin/bash

export TEST_HOME="/Users/janvier/Sites/data/sc_bal_mappings"
export LIVE_HOME="/Library/WebServer/Documents/data/sc_bal_mappings"

function copyDirectory {
  local srcDir=$1
  local dstDir=$2

  if [[ ! -d ${srcDir} ]]; then
    echo "Skipping ${srcDir}"
  elif [[ ! -d ${dstDir} ]]; then
    echo "Copying ${srcDir}"
    cp -R ${srcDir} ${dstDir}
    rm -rf ${srcDir}
  else
    echo "Replacing ${dstDir}"
    rm -rf ${dstDir}
    cp -R ${srcDir} ${dstDir}
    rm -rf ${srcDir}
  fi
}

crNumList=( `ps_print_seq_num.sh "$1"` )
if [[ $? -ne ${STATUS_OK} ]]; then
  ps_print_err.sh "$0" "Invalid string index(es): $1"
  exit 1
fi
res=$2
nameList=( ${3//,/ } )

for crNum in ${crNumList[@]}; do
  if [[ ${#nameList[@]} -eq 0 ]]; then
    for name in `ls -1 ${TEST_HOME}/cr${crNum}-${res} 2> /dev/null`; do
      copyDirectory\
          ${TEST_HOME}/cr${crNum}-${res}/${name}\
          ${LIVE_HOME}/cr${crNum}-${res}/${name}
    done
  else
    for name in ${nameList[@]}; do
      copyDirectory\
          ${TEST_HOME}/cr${crNum}-${res}/${name}\
          ${LIVE_HOME}/cr${crNum}-${res}/${name}
    done
  fi
done

exit 0
