#!/bin/bash
#
# Find all bundles with multiple versions in the features files. 
#
# - we assume that all xml files that contain the artifactid and bundle version 
#   are features files
# - we assume this file follows the convention so that each bundle 
#   with artifact id and version) is on an seperate line)
# 
# Usage:
#   $ ./smx-multiple-bundle-versions.sh [path_to_system_folder]

if [ $# -eq 0 ]
  then
    echo "Please supply the path to the system folder" >&2
    exit 1
fi

export dir=$1
# Extract directories with multiple versions
for i in $( find $dir -name '*.jar' | sed -n 's/\(.*\)\/.*\/.*/\1/p' | uniq -d ); do
	
	# For each artifact folder with multiple versions

	for j in $(find $i -name '*.jar'); do
		
		# For each Jar in the artifact folder
	
		export result=$(echo $j | sed -n 's/.*\/\(.*\)\/\(.*\)-.*/\2:\1/p')
		export artifact=$(echo $result | cut -d : -f 1)
		export version=$(echo $result | cut -d : -f 2)

		echo $artifact - $version
		
		# Find each occurence of the artifactid and the version on the same line in all 
		# XML files. Filenames will only be printed once on multiple occurences.
		grep --include *.xml -r $artifact.*$version $dir | cut -d : -f 1 | uniq
		
		echo 
    done
    echo -----
    echo
done
