#!/bin/bash

#rh_erratas=/mnt/koji/repos/perl-5.24-tmp/errata/rh-errata.modify
# Fix , add post fix
rh_erratas=/mnt/koji/repos/perl-5.24-tmp/errata/rh-errata.modify.with_level.withpostfix

rh_module_stream_file=/mnt/koji/repos/perl-5.24-tmp/errata/rh_errata_name_stream

an_module_srpm_name_stream=/mnt/koji/repos/perl-5.24-tmp/errata/tags_all_srpm_overall.with_name_stream

#for rh_id in $(cat $rh_erratas | grep 'module+' | awk -F':' '{print $1}' | sort -u );do
for rh_id in RHSA-2022-0418;do

        # TODO:new logic to get module anolis srpm and anolis id	
        #      - 1st step: for one rh errata, need make sure all package match, then use corrspoding an_srpm
        #      - if 1st step pass, then check if have corresponding cve 	
	# 
	rh_module_name=$(cat $rh_module_stream_file | grep $rh_id | awk -F'|' '{print $NF}' | awk -F':' '{print $1}')
	rh_module_stream=$(cat $rh_module_stream_file | grep $rh_id | awk -F'|' '{print $NF}' | awk -F':' '{print $2}')
	rh_module_stream_modify=$(echo $rh_module_stream | sed 's/rhel/an/g')
	is_ansrpm_exist=yes
	global_ansrpm_exist=no
	_an_module_final_hash=
	# for one rh_id, get all srpm
	# check if have an module have corresponding all srpm
        # - check if have corresponding an modules have one packages
        #   if have, to compare if all rh_id module pkgs in this an modules
        _modify_rh_modify_postfix=$(cat $rh_erratas | grep $rh_id | awk -F: '{print $3":"$4}' | head -n1)
	cat $an_module_srpm_name_stream | grep -sq ":$_modify_rh_modify:$rh_module_name:$rh_module_stream_modify"
	if [ $? == 0 ];then
            is_ansrpm_exist=yes		
	    for _an_module_hash in $(cat $an_module_srpm_name_stream | grep ":$_modify_rh_modify:$rh_module_name:$rh_module_stream_modify" | awk -F: '{print $1}');do
		>/tmp/list_an_module_hash    
	        cat $an_module_srpm_name_stream | grep $_an_module_hash > /tmp/list_an_module_hash
	    
	        for _modify_rh_modify in $(cat $rh_erratas | grep $rh_id | awk -F: '{print $3}');do
                    cat /tmp/list_an_module_hash | grep -sq ":$_modify_rh_modify:"
	            if [ $? != 0 ];then
                        is_ansrpm_exist=no
		        break
		    fi	
	        done	    
		if [ "$is_ansrpm_exist" == "yes" ] ;then
	            echo $rh_id:$_an_module_hash
		    global_ansrpm_exist=yes 
		fi  
	    done	
	fi	    
        [ "$global_ansrpm_exist" == "no" ] && echo $rh_id:no
done

