#!/bin/bash
containsElement () {
    local e match="$1"
    shift
    for e; do [[ "$e" == "$match" ]] && return 0; done
    return 1
}
ARCHIVE_GZ=../re_utilities.tar.gz
TMP_TAR=`mktemp`
echo $TMP_TAR
ARCHIVE_GZ_CLEAN=../re_utilities_clean.tar.gz
zcat $ARCHIVE_GZ > $TMP_TAR
ARCHIVE_FILE_LIST=`tar -tz -f ../re_utilities.tar.gz`
ls -l $TMP_TAR
GIT_LIST=`git ls-files`

for FILE in $ARCHIVE_FILE_LIST
do
    containsElement $FILE ${GIT_LIST[@]}
    if [  $? != 0 ]
    then
        echo "Deleting ${FILE}"
        tar --delete -f $TMP_TAR $FILE
    fi 
done
ls -l $TMP_TAR
echo "compressing.."
gzip -c  $TMP_TAR > $ARCHIVE_GZ_CLEAN
echo "output in ${ARCHIVE_GZ_CLEAN}"
