#!/bin/bash
#
#  YUP2ZLM - "Import YUP tree into ZLM 7.2" 
#  Copyright (C) 2006,2007 SUSE Linux Products GmbH and Novell Belgium BV
#
#  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; either version 2 of the License, or
#  (at your option) any later version.
#
#  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.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#  This script serves merely as an example. It was deliberately
#  kept as simple as possible and probably should be improved. 
#
#  use the '-s' option to skip the initial folder and group
#  checking and creation. Never use this option for the first run.
#  use the '-o' option to only perform the initial folder and
#  group checking and creation. If you don't change the setup and
#  mirror variables below you should always run yup2zlm manually
#  the first time, using the "-o" option.
#
#  $Id$
#

if [ -e /etc/sysconfig/yup ] ; then
	. /etc/sysconfig/yup
fi

YUP2ZLM_YUP_TREE=$YUP_DEST_DIR

if [ -e /etc/sysconfig/yup2zlm ] ; then
	. /etc/sysconfig/yup2zlm
fi

if [ "$YUP2ZLM_SLES10_PATCHES_FOLDER" = "$YUP2ZLM_SLED10_PATCHES_FOLDER" ] ; then
 echo Server related patches folder and desktop related patches folder must differ.
 exit 1
fi

ENV_LOAD=`echo $YUP2ZLM_ENV_LOAD | tr ',' ' '`
ENV_NOTLOAD=`echo $YUP2ZLM_ENV_NOTLOAD | tr ',' ' '`
ENVIRONMENTS="$ENV_LOAD"" ""$ENV_NOTLOAD"

if [ "$1" = "-s" ] ; then
  YUP2ZLM_PERFORM_SETUP="false"
  YUP2ZLM_PERFORM_MIRROR="true"
elif [ "$1" = "-o" ] ; then
  YUP2ZLM_PERFORM_SETUP="true"
  YUP2ZLM_PERFORM_MIRROR="false"
fi

if [ "$YUP2ZLM_PERFORM_SETUP" = "true" ] ; then
  FORMEDPATH="/"
  for PATHELEMENT in `echo $YUP2ZLM_SLES10_PATCHES_FOLDER | tr '/' ' '` ; do
    $YUP2ZLM_ZLMAN_CMD bfc "$PATHELEMENT" "$FORMEDPATH" 2>/dev/null 
    FORMEDPATH="${FORMEDPATH}/$PATHELEMENT"
  done    
  FORMEDPATH="/"
  for PATHELEMENT in `echo $YUP2ZLM_SLED10_PATCHES_FOLDER | tr '/' ' '` ; do
    $YUP2ZLM_ZLMAN_CMD bfc "$PATHELEMENT" "$FORMEDPATH" 2>/dev/null
    FORMEDPATH="${FORMEDPATH}/$PATHELEMENT"
  done    
fi
# ----------------------------------------------------------------------------------------------------------------------
# ------------------------------- For every product in the YUP tree ... ------------------------------------------------
# ----------------------------------------------------------------------------------------------------------------------
for PRODUCT_FOLDER in `ls -d ${YUP2ZLM_YUP_TREE}/SLE*Updates` ; do
  echo $PRODUCT_FOLDER
  PRODUCT_FOLDER=${PRODUCT_FOLDER##*/}
  PRODUCT_VERSION=`echo $PRODUCT_FOLDER | cut -d '-' -f 2`
  if [ $PRODUCT_VERSION = "Updates" ] ; then
    PRODUCT_VERSION="GA"
  fi
  SHORT_PRODUCT=${PRODUCT_FOLDER%%-*}
  CURRENT_PAT_BUNDLE=""

  if echo "$YUP2ZLM_PRODUCTS" | grep "$SHORT_PRODUCT" - 1>/dev/null 2>&1 ; then
    PRODUCT_OK="true"
  else
    PRODUCT_OK="false"
  fi

  if echo "$YUP2ZLM_SUBVERSIONS" | grep "$PRODUCT_VERSION" - 1>/dev/null 2>&1 ; then
    VERSION_OK="true"
  else
    VERSION_OK="false"
  fi

  if  [ $PRODUCT_OK = "true" -a $VERSION_OK = "true" ] ; then
    
    if [ "$SHORT_PRODUCT" = "SLES10" ] ; then
      PRODUCT="sles-10"
      CURRENTFOLDER="$YUP2ZLM_SLES10_PATCHES_FOLDER"
      DEVICE_GROUPS="$YUP2ZLM_SERVER_GROUPS"
    elif [ "$SHORT_PRODUCT" = "SLED10" ] ; then
      PRODUCT="sled-10"
      CURRENTFOLDER="$YUP2ZLM_SLED10_PATCHES_FOLDER"
      DEVICE_GROUPS="$YUP2ZLM_DESKTOP_GROUPS"
    fi
# ----------------------------------------------------------------------------------------------------------------------
# ------------------------------- For every architecture for that product in the YUP tree ... --------------------------
# ----------------------------------------------------------------------------------------------------------------------
    for ARCH_FOLDER in `ls ${YUP2ZLM_YUP_TREE}/${PRODUCT_FOLDER}`; do
      SHORT_ARCH=${ARCH_FOLDER##*-}
      echo Now processing $ARCH_FOLDER for $PRODUCT
      if echo "$YUP2ZLM_ARCHS" | grep "$SHORT_ARCH" - 1>/dev/null 2>&1 ; then
        PRODARCH="${PRODUCT}-${SHORT_ARCH}"
        cd ${YUP2ZLM_YUP_TREE}/${PRODUCT_FOLDER}/${ARCH_FOLDER}/repodata
        THIS_ARCH_OK="true"
	if [ "$YUP2ZLM_PERFORM_SETUP" = "true" ] ; then
	  $YUP2ZLM_ZLMAN_CMD bfc "$PRODUCT" "$CURRENTFOLDER" 2>/dev/null
	  $YUP2ZLM_ZLMAN_CMD bfc "$SHORT_ARCH" "$CURRENTFOLDER"/"$PRODUCT" 2>/dev/null
          $YUP2ZLM_ZLMAN_CMD bfc "$PRODUCT_VERSION" "$CURRENTFOLDER"/"$PRODUCT"/"$SHORT_ARCH" 2>/dev/null
          $YUP2ZLM_ZLMAN_CMD bfc  "$YUP2ZLM_TRANS_FOLDER" "$CURRENTFOLDER"/"$PRODUCT"/"$SHORT_ARCH"/"$PRODUCT_VERSION" 2>/dev/null
          DEVICE_KIND=""
          for DEVICE_KIND in `echo $DEVICE_GROUPS | tr ',' ' '` ; do
	    if [ -n "$DEVICE_KIND" ] ; then 
	      $YUP2ZLM_ZLMAN_CMD bfc "$DEVICE_KIND" "$CURRENTFOLDER" 2>/dev/null
	      $YUP2ZLM_ZLMAN_CMD bfc "$PRODUCT" "$CURRENTFOLDER"/"$DEVICE_KIND" 2>/dev/null
	      $YUP2ZLM_ZLMAN_CMD bfc  "$SHORT_ARCH" "$CURRENTFOLDER"/"$DEVICE_KIND"/"$PRODUCT" 2>/dev/null
              $YUP2ZLM_ZLMAN_CMD bfc  "$PRODUCT_VERSION""$CURRENTFOLDER"/"$DEVICE_KIND"/"$PRODUCT"/"$SHORT_ARCH" 2>/dev/null
	      for ENVIRONMENT in $ENVIRONMENTS ; do
		if [ -n "$ENVIRONMENT" ] ; then
	          $YUP2ZLM_ZLMAN_CMD bfc "$ENVIRONMENT" "$CURRENTFOLDER"/"$DEVICE_KIND"/"$PRODUCT"/"$SHORT_ARCH"/"$PRODUCT_VERSION" 2>/dev/null
		fi
	      done
            fi
	  done
  	  BUNDLE="" 
          for BUNDLE in "$YUP2ZLM_SEC_PATCH_BUNDLE_EXT"_"$YUP2ZLM_PATCH_BUNDLE_BASE_NAME" \
				  "$YUP2ZLM_REC_PATCH_BUNDLE_EXT"_"$YUP2ZLM_PATCH_BUNDLE_BASE_NAME" \
				  "$YUP2ZLM_OPT_PATCH_BUNDLE_EXT"_"$YUP2ZLM_PATCH_BUNDLE_BASE_NAME" \
				  "$YUP2ZLM_KER_SEC_PATCH_BUNDLE_EXT"_"$YUP2ZLM_PATCH_BUNDLE_BASE_NAME" \
				  "$YUP2ZLM_KER_NONSEC_PATCH_BUNDLE_EXT"_"$YUP2ZLM_PATCH_BUNDLE_BASE_NAME" ; do
  	    if $YUP2ZLM_ZLMAN_CMD bgm "$CURRENTFOLDER"/"$PRODUCT"/"$SHORT_ARCH"/"$PRODUCT_VERSION"/"$BUNDLE" 1>/dev/null 2>&1; then
              :	
  	    else
  	      if $YUP2ZLM_ZLMAN_CMD bgc "$BUNDLE" "$CURRENTFOLDER"/"$PRODUCT"/"$SHORT_ARCH"/"$PRODUCT_VERSION" ; then
                :  	
  	      else
  	        echo "$CURRENTFOLDER"/"$PRODUCT"/"$SHORT_ARCH"/"$PRODUCT_VERSION"/"$BUNDLE" could not be created - check folder path.
  	        THIS_ARCH_OK="false" 
	        break
  	      fi
  	    fi
  	  done
          if [ "$THIS_ARCH_OK" = "false" ] ; then
            echo Errors found - skipping architecture "$SHORT_ARCH" for product "$PRODUCT" in version "$PRODUCT_VERSION".
            continue 
          fi
	    for ENVIRONMENT in $ENVIRONMENTS ; do
	      if [ -n "$ENVIRONMENT" ] ; then
              for DEVICE_KIND in `echo $DEVICE_GROUPS | tr ',' ' '` ; do
	        if [ -n "$DEVICE_KIND" ] ; then
                  BUNDLE=""
                  for BUNDLE in "$YUP2ZLM_SEC_PATCH_BUNDLE_EXT"_"$YUP2ZLM_PATCH_BUNDLE_BASE_NAME" \
				  "$YUP2ZLM_REC_PATCH_BUNDLE_EXT"_"$YUP2ZLM_PATCH_BUNDLE_BASE_NAME" \
				  "$YUP2ZLM_OPT_PATCH_BUNDLE_EXT"_"$YUP2ZLM_PATCH_BUNDLE_BASE_NAME" \
				  "$YUP2ZLM_KER_SEC_PATCH_BUNDLE_EXT"_"$YUP2ZLM_PATCH_BUNDLE_BASE_NAME" \
				  "$YUP2ZLM_KER_NONSEC_PATCH_BUNDLE_EXT"_"$YUP2ZLM_PATCH_BUNDLE_BASE_NAME" ; do
                    if $YUP2ZLM_ZLMAN_CMD bgm "$CURRENTFOLDER"/"$DEVICE_KIND"/"$PRODUCT"/"$SHORT_ARCH"/"$PRODUCT_VERSION"/"$ENVIRONMENT"/"$DEVICE_KIND"_"$ENVIRONMENT"_"$BUNDLE" 1>/dev/null 2>&1; then
                      :
                    else
                      if $YUP2ZLM_ZLMAN_CMD bgc "$DEVICE_KIND"_"$ENVIRONMENT"_"$BUNDLE" "$CURRENTFOLDER"/"$DEVICE_KIND"/"$PRODUCT"/"$SHORT_ARCH"/"$PRODUCT_VERSION"/"$ENVIRONMENT" ; then
                        :
                      else
                        echo "$CURRENTFOLDER"/"$DEVICE_KIND"/"$PRODUCT"/"$SHORT_ARCH"/"$PRODUCT_VERSION"/"$ENVIRONMENT"/"$DEVICE_KIND"_"$ENVIRONMENT"_"$BUNDLE" could not be created - check folder path.
                        THIS_ARCH_OK="false"
                        break
                      fi
                    fi
                  done
                  if [ "$THIS_ARCH_OK" = "false" ] ; then
	            break  
                  fi
	        fi
              done
	    fi
            if [ "$THIS_ARCH_OK" = "false" ] ; then
	      break  
            fi
       	  done
        fi
        if [ "$THIS_ARCH_OK" = "false" ] ; then
          echo Errors found - skipping architecture "$SHORT_ARCH" for product "$PRODUCT" in version "$PRODUCT_VERSION".
          continue 
        fi
        if [ "$YUP2ZLM_PERFORM_MIRROR" = "true" ] ; then
	  EXISTING_PATCHES=`$YUP2ZLM_ZLMAN_CMD bl "$CURRENTFOLDER"/"$PRODUCT"/"$SHORT_ARCH"/"$PRODUCT_VERSION"/"$YUP2ZLM_TRANS_FOLDER" 2>/dev/null | grep '.*-[0-9][0-9][0-9][0-9][ 0-9].*' | grep 'Software Bundle' | cut -d '|' -f 1 | tr -d ' ' | sort -n`
# ----------------------------------------------------------------------------------------------------------------------
# ------------------------------- End of preliminary work - we start adding patches now --------------------------------
# ----------------------------------------------------------------------------------------------------------------------
          for PATCHFILE in patch-*.xml ; do
            if [ -f "$PATCHFILE" ] ; then
  	      PATCHID=`grep -i patchid $PATCHFILE | cut -d '"' -f 2`
  	      PATCHNAME=${PATCHID%-*}
  	      PATCHNUMBER=${PATCHID##*-}
  	      KIND=""
  	      KIND=`grep '<category>' "$PATCHFILE" | cut -d '>' -f 2 | cut -d '<' -f 1`
  	      PATCHBUNDLE="${PATCHID}"
              if echo "$EXISTING_PATCHES" | grep "$PATCHBUNDLE" - 2>/dev/null 1>&2 ; then
		echo "Patch "$PATCHBUNDLE" was already processed correctly - skipping."
  	      else
  	        START=`grep -n '<description lang="en">' "$PATCHFILE" | cut -d ':' -f 1`
  	        GOODSTART=`expr $START + 1`
  	        START2=`tail +${GOODSTART} "$PATCHFILE" | grep -n  '</description>' | head  -1 | cut -d ':' -f 1`
  	        GOODEND=`expr $START + $START2 - 1`
	        PATCHINFOID=`grep 'PATCHINFO' "$PATCHFILE" | cut -d '"' -f 2`
  	        DESCRIPTION=`sed -n "$START,${GOODEND}p" "$PATCHFILE" | cut -d '>' -f 2 | tr -d '\r\n' `
	        DESCRIPTION_FILTERED=`echo $DESCRIPTION | tr -d '\'`
	        TOTALDESCRIPTION="$PATCHINFOID"" - ""$DESCRIPTION_FILTERED"
                patchbundleok=true
	        ATOMLIST=""
	        for ATOM in `cat "$PATCHFILE" | grep atom | grep rpm | cut -d '"' -f 4,8,10 | tr '"' '-'` ; do
                  for ATOMFILE in `ls ../rpm/*/$ATOM*.rpm` ; do
		    if [ "$YUP2ZLM_INCLUDE_PATCH_RPMS" = "true" ] ; then
		      if ls $ATOMFILE ; then
		        ATOMLIST="$ATOMLIST $ATOMFILE"
		      else
           	        echo Could not locate rpm corresponding to atom "$ATOM".
    		        patchbundleok=false
		        break
		      fi
		    else
                      if echo "$ATOMFILE" | grep -i 'patch.rpm$' -  ; then
		        echo Skipping patch rpm $ATOMFILE
		      else
		        if ls $ATOMFILE > /dev/null 2>/dev/null ; then
		          ATOMLIST="$ATOMLIST $ATOMFILE"
		        else
           	          echo Could not locate rpm corresponding to atom "$ATOM".
    		          patchbundleok=false
		          break
		        fi
 		      fi
		    fi
		  done
		  if [ "$patchbundleok" = "false" ] ; then
	            break
		  fi
	        done
	        if [ "$patchbundleok" = "false" ] ; then
                  THIS_ARCH_OK="false"
                  break 
                fi
		PATCH_BUNDLES_TO_DO="$PATCHBUNDLE"  
                DO_KERNEL_SPLIT="false" 
                if echo "$PATCHID" | grep 'kernel-' > /dev/null ; then
		  if [ "$YUP2ZLM_SPLIT_KERNEL" = "true" ] ; then
		    PATCH_BUNDLES_TO_DO=""  
                    DO_KERNEL_SPLIT="true"
		    for KERNEL_RPM in $ATOMLIST ; do
		      if [ -n "$KERNEL_RPM" ] ; then
			KERNEL_KIND=`echo $KERNEL_RPM | cut -d '/' -f 4 | cut -d '-' -f 2`
  	                if $YUP2ZLM_ZLMAN_CMD bc "$PATCHBUNDLE"-"$KERNEL_KIND" "$CURRENTFOLDER"/"$PRODUCT"/"$SHORT_ARCH"/"$PRODUCT_VERSION" --description="$TOTALDESCRIPTION" ; then
		          :
  	                else
	                  $YUP2ZLM_ZLMAN_CMD bd "$CURRENTFOLDER"/"$PRODUCT"/"$SHORT_ARCH"/"$PRODUCT_VERSION"/"$PATCHBUNDLE"-"$KERNEL_KIND"
		          if $YUP2ZLM_ZLMAN_CMD bc "$PATCHBUNDLE"-"$KERNEL_KIND" "$CURRENTFOLDER"/"$PRODUCT"/"$SHORT_ARCH"/"$PRODUCT_VERSION" --description="$TOTALDESCRIPTION" ; then
		            echo Replaced uncommitted bundle "$CURRENTFOLDER"/"$PRODUCT"/"$SHORT_ARCH"/"$PRODUCT_VERSION"/"$PATCHBUNDLE"-"$KERNEL_KIND" 
 		          else
  	                    echo Could not create bundle "$PATCHBUNDLE"-"$KERNEL_KIND" in folder "$CURRENTFOLDER"/"$PRODUCT"/"$SHORT_ARCH"/"$PRODUCT_VERSION".
  	                    THIS_ARCH_OK="false"
  	                    break
		          fi
  	                fi
	                if $YUP2ZLM_ZLMAN_CMD bap --force-nevra --installtype=install "$CURRENTFOLDER"/"$PRODUCT"/"$SHORT_ARCH"/"$PRODUCT_VERSION"/"$PATCHBUNDLE"-"$KERNEL_KIND" $PRODARCH $KERNEL_RPM ; then
		          echo Added rpm "$KERNEL_RPM" to bundle "$CURRENTFOLDER"/"$PRODUCT"/"$SHORT_ARCH"/"$PRODUCT_VERSION"/"$PATCHBUNDLE"-"$KERNEL_KIND" .
			  PATCH_BUNDLES_TO_DO="$PATCH_BUNDLES_TO_DO ""$PATCHBUNDLE"-"$KERNEL_KIND"
		        else
  		          echo Could not add rpm "$KERNEL_RPM" to bundle "$CURRENTFOLDER"/"$PRODUCT"/"$SHORT_ARCH"/"$PRODUCT_VERSION"/"$PATCHBUNDLE"-"$KERNEL_KIND" . 
		          THIS_ARCH_OK="false"
		          break 
		        fi
                      fi
		    done  
                    if [ "$THIS_ARCH_OK" = "false" ] ; then
		      break
                    fi
		  fi
		fi
		if [ "$DO_KERNEL_SPLIT" = "false" ] ; then 		  
  	          if $YUP2ZLM_ZLMAN_CMD bc "$PATCHBUNDLE" "$CURRENTFOLDER"/"$PRODUCT"/"$SHORT_ARCH"/"$PRODUCT_VERSION" --description="$TOTALDESCRIPTION" ; then
		    :
  	          else
	            $YUP2ZLM_ZLMAN_CMD bd "$CURRENTFOLDER"/"$PRODUCT"/"$SHORT_ARCH"/"$PRODUCT_VERSION"/"$PATCHBUNDLE"
		    if $YUP2ZLM_ZLMAN_CMD bc "$PATCHBUNDLE" "$CURRENTFOLDER"/"$PRODUCT"/"$SHORT_ARCH"/"$PRODUCT_VERSION" --description="$TOTALDESCRIPTION" ; then
		      echo Replaced uncommitted bundle "$CURRENTFOLDER"/"$PRODUCT"/"$SHORT_ARCH"/"$PRODUCT_VERSION"/"$PATCHBUNDLE" 
 		    else
  	              echo Could not create bundle "$PATCHBUNDLE" in folder "$CURRENTFOLDER"/"$PRODUCT"/"$SHORT_ARCH"/"$PRODUCT_VERSION".
  	              THIS_ARCH_OK="false"
  	              break
		    fi
  	          fi
	          if $YUP2ZLM_ZLMAN_CMD bap --force-nevra --freshen --installtype=upgrade "$CURRENTFOLDER"/"$PRODUCT"/"$SHORT_ARCH"/"$PRODUCT_VERSION"/"$PATCHBUNDLE" $PRODARCH $ATOMLIST ; then
		    echo Added rpms "$ATOMLIST" to bundle "$CURRENTFOLDER"/"$PRODUCT"/"$SHORT_ARCH"/"$PRODUCT_VERSION"/"$PATCHBUNDLE" .
		  else
  		    echo Could not add rpms "$ATOMLIST" to bundle "$CURRENTFOLDER"/"$PRODUCT"/"$SHORT_ARCH"/"$PRODUCT_VERSION"/"$PATCHBUNDLE" . 
		    THIS_ARCH_OK="false"
		    break 
		  fi
	 	fi
	        ADD_LIST=""
  	        case "$KIND" in
  	        security)
		  if echo "$PATCHID" | grep 'kernel-' ; then
		    echo "Skipping kernel patch\(es based on\) $PATCHID as security patch\(es\) and adding as kernel security patch\(es\)." 
                    ADD_LIST="$ADD_LIST $YUP2ZLM_KER_SEC_PATCH_BUNDLE_EXT"
                  else
                    ADD_LIST="$ADD_LIST $YUP2ZLM_SEC_PATCH_BUNDLE_EXT"
	          fi
  		  ;;
  	        recommended)
		  if echo "$PATCHID" | grep 'kernel-' ; then
		    echo "Skipping kernel patch\(es based on\) $PATCHID as recommended patch\(es\) and adding as kernel non-security patch\(es\)." 
                    ADD_LIST="$ADD_LIST $YUP2ZLM_KER_NONSEC_PATCH_BUNDLE_EXT"
                  else
                    ADD_LIST="$ADD_LIST $YUP2ZLM_REC_PATCH_BUNDLE_EXT"
	          fi
  		  ;;
  	        *)
		  if echo "$PATCHID" | grep 'kernel-' ; then
		    echo "Skipping kernel patch\(es based on\) $PATCHID as optional patch\(es\) and adding as kernel non-security patch\(es\)." 
                    ADD_LIST="$ADD_LIST $YUP2ZLM_KER_NONSEC_PATCH_BUNDLE_EXT"
                  else
                    ADD_LIST="$ADD_LIST $YUP2ZLM_OPT_PATCH_BUNDLE_EXT"
	          fi
  	  	  ;;
  	        esac
	        EXTENSION=""
	        for EXTENSION in $ADD_LIST ; do
		  PATCH_BUNDLE_TO_DO=""
		  for PATCH_BUNDLE_TO_DO in $PATCH_BUNDLES_TO_DO ; do
		    if [ -n "$PATCH_BUNDLE_TO_DO" ] ; then
  	              if $YUP2ZLM_ZLMAN_CMD bga "$CURRENTFOLDER"/"$PRODUCT"/"$SHORT_ARCH"/"$PRODUCT_VERSION"/"$EXTENSION"_"$YUP2ZLM_PATCH_BUNDLE_BASE_NAME" "$CURRENTFOLDER"/"$PRODUCT"/"$SHORT_ARCH"/"$PRODUCT_VERSION"/"${PATCH_BUNDLE_TO_DO}" ; then
		        :
  	              else
  	                echo Could not add patch ${PATCH_BUNDLE_TO_DO} to bundle "$EXTENSION"_"$YUP2ZLM_PATCH_BUNDLE_BASE_NAME" in folder "$CURRENTFOLDER"/"$PRODUCT"/"$SHORT_ARCH"/"$PRODUCT_VERSION".
  	                THIS_ARCH_OK="false"
  	                break
  	              fi
		      for ENVIRONMENT in "$ENV_LOAD" ; do
		        if [ -n "$ENVIRONMENT" ] ; then
                          for DEVICE_KIND in `echo $DEVICE_GROUPS | tr ',' ' '` ; do
		            if [ -n "$DEVICE_KIND" ] ; then
		              if $YUP2ZLM_ZLMAN_CMD bga "$CURRENTFOLDER"/"$DEVICE_KIND"/"$PRODUCT"/"$SHORT_ARCH"/"$PRODUCT_VERSION"/"$ENVIRONMENT"/"$DEVICE_KIND"_"$ENVIRONMENT"_"$EXTENSION"_"$YUP2ZLM_PATCH_BUNDLE_BASE_NAME" "$CURRENTFOLDER"/"$PRODUCT"/"$SHORT_ARCH"/"$PRODUCT_VERSION"/"${PATCH_BUNDLE_TO_DO}" ; then
                                :
                              else
                                echo Could not add patch ${PATCH_BUNDLE_TO_DO} to bundle "$DEVICE_KIND"_"$ENVIRONMENT"_"$EXTENSION"_"$YUP2ZLM_PATCH_BUNDLE_BASE_NAME" in folder "$CURRENTFOLDER"/"$DEVICE_KIND"/"$PRODUCT"/"$SHORT_ARCH"/"$PRODUCT_VERSION"/"$ENVIRONMENT".
                                THIS_ARCH_OK="false"
                                break 
                              fi
		            fi
		          done
		        fi
                        if [ "$THIS_ARCH_OK" = "false" ] ; then
		          break
                        fi
		      done
                      if [ "$THIS_ARCH_OK" = "false" ] ; then
		        break
                      fi
		    fi	
		  done
                  if [ "$THIS_ARCH_OK" = "false" ] ; then
		    break
                  fi
	        done
                if [ "$THIS_ARCH_OK" = "false" ] ; then
                  break
                fi
	        if $YUP2ZLM_ZLMAN_CMD bc "$PATCHBUNDLE" "$CURRENTFOLDER"/"$PRODUCT"/"$SHORT_ARCH"/"$PRODUCT_VERSION"/"$YUP2ZLM_TRANS_FOLDER" ; then
	        :
	        else
	          echo Could not add patch "$PATCHBUNDLE" to transaction folder.
	          THIS_ARCH_OK="false"
	          break
	        fi
              fi
  	    fi
# ----------------------------------------------------------------------------------------------------------------------
# ------------------------------- Patch was processed correctly - adding to transacion log folder ... ------------------
# ----------------------------------------------------------------------------------------------------------------------
          done
	fi
        if [ "$THIS_ARCH_OK" = "false" ] ; then
          echo Errors found - skipping architecture "$SHORT_ARCH" for product "$PRODUCT" in version "$PRODUCT_VERSION".
  	  continue
        fi
      else
        echo REFUSED ARCH : "$SHORT_ARCH"
      fi
    done
  else
    echo REFUSED PRODUCT in VERSION : "$SHORT_PRODUCT" - "$PRODUCT_VERSION"
  fi
done
