#! /bin/bash
# This script compares SecureFileList.txt against the release source tree to
# identify any files that would be stripped by the docsafe.
# To use this script execute it from the directory where tarball with the 
# release source is located. Alternatively, execute this script from the
# HD DTA source root - a directory containing dta directory. Alternatively,
# directory should contain source.lst file that has list of all the files in
# the release.
# The scrpt requres tar, grep, awk and the recent version of bash.

secure_name_list="SecureFileList.txt"
source_file_list="source.lst"
match_file_list="match.lst"
module_file_list="modules.lst"

function create-source-list()
{
    local archive_name=$(compgen -G "*.tar.gz")
# select first name from the list
    for name in ${archive_name} ; do
        archive_name=${name}
        break
    done
    if [ -n "${archive_name}" ] ; then
        echo -n "Found archive ${archive_name}... "
        tar -tzf ${archive_name} > ${source_file_list}
    else 
        if [ -d ./dta ] ; then
            echo -n "Found dta directory... "
            find . > ${source_file_list}
        else
            echo "Unable create source list, exiting."
            exit 254
        fi
    fi
    echo "created source list."
}


if [ ! -e ${secure_name_list} ] ; then
 echo "Secure file list can not be found."
 exit 255
fi

if [ ! -e ${source_file_list} ] ; then
 echo "Source file list can not be found, attempting to create."
 create-source-list
fi


sed -e 's/[ \t]*$//' -e 's/\\/\//g' -e '/^$/{d}' -e 's/\(^[^\/#]\)/\/\1/' ${secure_name_list} > u_${secure_name_list}

grep -Fo -f u_${secure_name_list} ${source_file_list} > ${match_file_list}

echo "###" >> ${match_file_list}
grep -F -f ${match_file_list} u_${secure_name_list} > ${module_file_list}

awk '/^###/{header=$0}; /^[^#]/{if(header != "") {print header; header=""}; print $0}' ${module_file_list}

rm ${source_file_list} ${match_file_list} ${module_file_list} u_${secure_name_list}

