#!/bin/bash
#
# SCalasca ARchiving scriptLET
#
# Author: Marc-Andre Hermanns <m.a.hermanns@fz-juelich.de>
#
######################################################################

echo $EPK_COMPRESSOR
export EPK_DIR=`echo $1 | sed -e "s/\(.*\)\/$/\1/"`

if [ ! -e $EPK_DIR/epik.conf ]; then
  echo "Unable to find EPIK configuration in $1."
fi

export EPK_MODE=`echo $EPK_DIR | sed -e "s/.*_\([a-z]*\)$/\1/"`
export EPK_PREFIX=`echo $EPK_DIR | sed -e "s/\(.*\)_[a-z]*$/\1/"`

# compress uncompressed cube files
echo Checking for uncompressed cube files
export EPK_UNCOMPRESSED=`find $EPK_DIR -name "*.cube"`
if [ ! -z "`echo $EPK_UNCOMPRESSED | sed -e "s/\s*//"`" ]; then
  echo $EPK_UNCOMPRESSED | xargs -n 1 gzip
fi


echo Archiving light-weight data
export EPK_LW_EPIK=`find $EPK_DIR -name "epik.*"`
export EPK_LW_SCOUT=`find $EPK_DIR -name "scout.*"`
export EPK_LW_CUBE=`find $EPK_DIR -name "*.cube.gz"`

export EPK_LW_FILES=`echo $EPK_LW_EPIK $EPK_LW_SCOUT $EPK_LW_CUBE | xargs -n 1 echo | sort -u`
echo $EPK_LW_FILES
tar cvf ${EPK_PREFIX}_cube.tar $EPK_LW_FILES
if [ ! -e "$EPK_COMPRESSOR" ]; then
  $EPK_COMPRESSOR ${EPK_PREFIX}_cube.tar
fi


if [ "$EPK_MODE" == "trace" ]; then
  echo Archiving complete trace
  tar cvf ${EPK_PREFIX}_trace.tar $EPK_DIR
fi
echo done.
