#!/bin/bash
# 
# Copies the T-Coffee source and header file to the distribution target directory
# 
# Parameters 
# 1) 'code_list' file containing all the C source files as generated by build process -- see 'makefile' for more details
# 2) The target folder where the files have to be copiated 
# 
# Some directive declarations
# -e: Stop at the first error 
# -u: Report an error if a variable is not declared
# -x: Echo the execute command 
set -e 
set -u

CODE_LIST=${1:-code_list}
TARGET=${2}
LIB_DIR=../../lib

rm -rf folders
for it in `cat $CODE_LIST`; do
  #remove the LIB_DIR prefix 
  entry=${it/$LIB_DIR/}
  # filename parts 
  path=`dirname $entry`
  file=`basename $entry`

  # make sure exists the target path
  mkdir -p $TARGET/$path
  
  # copy '.c' source 
  cp $it $TARGET/$path 
  
  echo "$path" >> folders
  
done

# Always add this folders
echo "." >> folders
echo "data_headers" >> folders

for path in `cat folders | uniq`; do 
  mkdir -p $TARGET/$path
  cp $LIB_DIR/$path/*.h $TARGET/$path 
done

rm -rf folders
