#!/bin/bash
#*******************************************************************************
# Copyright 2004-2007 LSI Logic Corporation.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation,  version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#*******************************************************************************
#
#  Script %name:	genfileattributes26p %
#  Instance:		WIC_1
#  %version:		2 %
#  Description:		
#  %created_by:		sdachepa %
#  %date_created:	Mon Jan 22 16:06:04 2007 %

###############
# set global environment variables
##################
initialize_fn()
{
    CMN_HEADER_DIR=mpp_linux_headers
    SYS_HEADER_DIR=mpp_linux_sys_headers
    UTIL_DIR=utility
    C_FILES="mppLnx26p_sysdep.c mppLnx26p_vhbalib.c MPP_hba.c mppLnx26p_vhbamisc.c mppLnx26p_upper.c 
        mppLnx26p_vhbaproc.c genuniqueid.c mppLnx26p_vhba.c mppLnx26p_vhbatask.c mppCmn_s2tos3.c 
        mppLnx26p_vhbaio.c mppCmn_SysInterface.c"
    CMN_HEADER_FILES="MPP_Common.h MPP_RdacInfo.h mppCmn_s2tos3.h scsiio.h 
        MPP_ProtoTypes.h mppUtil_Common.h mppCmn_SysInterface.h"
    SYS_HEADER_FILES="MPP_Sysdep.h mppLnx26p_shared.h mppUtil_Sysdep.h mppLnx26p_prototypes.h 
        mppLnx26p_vhba.h"
    UTIL_FILES="mppBusRescan26p mppUpdate26p mppUtil26p_sysdep.c mppUtil.c"
    VERSION_HEADER_FILE_NAME=mppLnx_$1_version.h
}


#####################
# write the header file's header
##########################
write_header_fn()
{
    echo "/**************************************"
    echo "* This is an auto-generated file."
    echo "* Do not edit it"
    echo "* Copyright 2004 Engenio Information Technologies, Inc."
    echo "* generated at $(/bin/date) by $(/bin/uname -n)"
    echo "***************************************/"
    echo ""
    echo ""
    VAR_NAME=$(/bin/basename $1 .h)
    echo "#ifndef __${VAR_NAME}_h_INC"
    echo "#define __${VAR_NAME}_h_INC"
    echo ""
    echo ""
    echo "mppLnx_FileAttributes_t ${VAR_NAME}_attributes [] = "
    echo "{"
}
########################
# write trailer of the header file
#########################
write_trailer_fn()
{
    echo "};"
    echo ""
    echo ""
    echo "#ifndef MPPLNX_NUM_FILE_ENTRY"
    NUM_FILES=$(echo ${C_FILES} ${CMN_HEADER_FILES} ${SYS_HEADER_FILES} \
        ${UTIL_FILES} | /usr/bin/wc -w )
    echo "#define MPPLNX_NUM_FILE_ENTRY"  ${NUM_FILES}
    echo "#endif"
    echo "#endif  /* end of the ifndef */"
}

###########################
# generate file attributes
###########################
gen_file_attributes()
{
    FILE_NAME=$(/bin/basename $1)
    FILE_VERSION=$(/bin/grep "%version:" $1|/bin/awk '{print $3}')
    FILE_DIGEST=$(/usr/bin/md5sum $1|/bin/awk '{print $1}')
    echo "    { "
    echo "        \"${FILE_NAME}\","
    echo "        \"${FILE_VERSION}\","
    echo "        \"${FILE_DIGEST}\","
    echo "    },"

}
##########################
# generate all file attributes
##########################
gen_all_files()
{

   for filename in ${C_FILES}
   do
      gen_file_attributes $filename
   done


   for filename in ${CMN_HEADER_FILES}
   do
      gen_file_attributes ${CMN_HEADER_DIR}/$filename
   done


   for filename in ${SYS_HEADER_FILES}
   do
      gen_file_attributes ${SYS_HEADER_DIR}/$filename
   done


   for filename in ${UTIL_FILES}
   do
      gen_file_attributes ${UTIL_DIR}/$filename
   done
}


main_worker()
{
   write_header_fn  $1
   gen_all_files
   write_trailer_fn
}

initialize_fn  $1


main_worker ${VERSION_HEADER_FILE_NAME} > ${SYS_HEADER_DIR}/${VERSION_HEADER_FILE_NAME}
