5ddc162
#!/bin/bash -x
5ddc162
# Generates the 'source tarball' for JDK 8 projects and update spec infrastructure
5ddc162
# By default, this script regenerate source as they are currently used. 
5ddc162
# so if the version of sources change, this file changes and is pushed
5ddc162
#
5ddc162
# In any case you have to set PROJECT_NAME REPO_NAME and VERSION. eg:
5ddc162
# PROJECT_NAME=jdk8u   OR   aarch64-port 
5ddc162
# REPO_NAME=jdk8u60    OR   jdk8u60 
5ddc162
# VERSION=jdk8u60-b27  OR aarch64-jdk8u65-b17 OR for head, keyword 'tip' should do the job there
5ddc162
# 
5ddc162
# If you don't, default are used and so already uploaded tarball regenerated
5ddc162
# They are used to create correct name and are used in construction of sources url (unless REPO_ROOT is set)
5ddc162
# 
5ddc162
# For other useful variables see generate_source_tarball.sh
5ddc162
#
5ddc162
# the used values are then substituted to spec and sources
5ddc162
2ef1e20
if [ ! "x$PR2126" = "x" ] ; then
2ef1e20
  if [ ! -f "$PR2126" ] ; then
2ef1e20
    echo "You have specified PR2126 as $PR2126 but it does not exists. exiting"
2ef1e20
    exit 1
2ef1e20
  fi
2ef1e20
fi
2ef1e20
5ddc162
set -e
5ddc162
5ddc162
if [ "x$PROJECT_NAME" = "x" ] ; then
5ddc162
    PROJECT_NAME="aarch64-port"
5ddc162
fi
5ddc162
if [ "x$REPO_NAME" = "x" ] ; then
72739d1
    REPO_NAME="jdk8u"
5ddc162
fi
5ddc162
if [ "x$VERSION" = "x" ] ; then
21d84da
    VERSION="aarch64-jdk8u172-b11"
5ddc162
fi
5ddc162
5ddc162
if [ "x$COMPRESSION" = "x" ] ; then
5ddc162
# rhel 5 needs tar.gz
5ddc162
    COMPRESSION=xz
5ddc162
fi
5ddc162
if [ "x$FILE_NAME_ROOT" = "x" ] ; then
5ddc162
    FILE_NAME_ROOT=${PROJECT_NAME}-${REPO_NAME}-${VERSION}
5ddc162
fi
5ddc162
if [ "x$PKG" = "x" ] ; then
5ddc162
    URL=`cat .git/config  | grep url`
5ddc162
    PKG=${URL##*/}
5ddc162
fi
5ddc162
if [ "x$SPEC" = "x" ] ; then
5ddc162
    SPEC=${PKG}.spec
5ddc162
fi
5ddc162
if [ "x$RELEASE" = "x" ] ; then
5ddc162
    RELEASE=1
5ddc162
fi
5ddc162
5ddc162
FILENAME=${FILE_NAME_ROOT}.tar.${COMPRESSION}
5ddc162
5ddc162
if [ ! -f ${FILENAME} ] ; then
5ddc162
echo "Generating ${FILENAME}"
5ddc162
. ./generate_source_tarball.sh
5ddc162
else 
5ddc162
echo "${FILENAME} already exists, using"
5ddc162
fi
5ddc162
5ddc162
5ddc162
echo "Touching spec: $SPEC"
5ddc162
sed -i "s/^%global\s\+project.*/%global project         ${PROJECT_NAME}/" $SPEC 
5ddc162
sed -i "s/^%global\s\+repo.*/%global repo            ${REPO_NAME}/" $SPEC 
5ddc162
sed -i "s/^%global\s\+revision.*/%global revision        ${VERSION}/" $SPEC 
5ddc162
# updated sources, resetting release
5ddc162
sed -i "s/^Release:.*/Release: $RELEASE.%{buildver}%{?dist}/" $SPEC
5ddc162
5ddc162
#https://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#Bash
5ddc162
function levenshtein {
5ddc162
	if [ "$#" -ne "2" ]; then
5ddc162
		echo "Usage: $0 word1 word2" >&2
5ddc162
	elif [ "${#1}" -lt "${#2}" ]; then
5ddc162
		levenshtein "$2" "$1"
5ddc162
	else
5ddc162
		local str1len=$((${#1}))
5ddc162
		local str2len=$((${#2}))
5ddc162
		local d i j
5ddc162
		for i in $(seq 0 $(((str1len+1)*(str2len+1)))); do
5ddc162
			d[i]=0
5ddc162
		done
5ddc162
		for i in $(seq 0 $((str1len)));	do
5ddc162
			d[$((i+0*str1len))]=$i
5ddc162
		done
5ddc162
		for j in $(seq 0 $((str2len)));	do
5ddc162
			d[$((0+j*(str1len+1)))]=$j
5ddc162
		done
5ddc162
5ddc162
		for j in $(seq 1 $((str2len))); do
5ddc162
			for i in $(seq 1 $((str1len))); do
5ddc162
				[ "${1:i-1:1}" = "${2:j-1:1}" ] && local cost=0 || local cost=1
5ddc162
				local del=$((d[(i-1)+str1len*j]+1))
5ddc162
				local ins=$((d[i+str1len*(j-1)]+1))
5ddc162
				local alt=$((d[(i-1)+str1len*(j-1)]+cost))
5ddc162
				d[i+str1len*j]=$(echo -e "$del\n$ins\n$alt" | sort -n | head -1)
5ddc162
			done
5ddc162
		done
5ddc162
		echo ${d[str1len+str1len*(str2len)]}
5ddc162
	fi
5ddc162
}
58c4028
# generate shenandoah hotspot
58c4028
# that means supply the underlying script with new values
58c4028
# to new filename.
58c4028
MAIN_VERSION=$VERSION
58c4028
if [ "x$VERSION" = "xtip" ] ; then
58c4028
    VERSION="tip"
58c4028
else
58c4028
	#hardcoding version for anything else except tip
21d84da
    VERSION="aarch64-shenandoah-jdk8u172-b11--shenandoah-merge-2018-05-15"
58c4028
fi
58c4028
MAIN_REPO_NAME=$REPO_NAME
58c4028
REPO_NAME=jdk8u-shenandoah
58c4028
MAIN_FILE_NAME_ROOT=$FILE_NAME_ROOT
58c4028
FILE_NAME_ROOT=${PROJECT_NAME}-${REPO_NAME}-${VERSION}
58c4028
FILENAME_SH=${FILE_NAME_ROOT}.tar.${COMPRESSION}
58c4028
REPOS="hotspot"
58c4028
58c4028
if [ ! -f ${FILENAME_SH} ] ; then
58c4028
echo "Generating ${FILENAME_SH}"
58c4028
. ./generate_source_tarball.sh
58c4028
else 
58c4028
echo "${FILENAME_SH} already exists, using"
58c4028
fi
58c4028
93b65a6
sed -i "s/^Source1:.*/Source1: ${FILENAME_SH}/" $SPEC
58c4028
git --no-pager diff $SPEC
5ddc162
5ddc162
# find the most similar sources name and replace it by newly generated one.
5ddc162
echo "Old sources"
5ddc162
cat sources
2ef1e20
a_sources=`cat sources | sed "s/.*(//g" | sed "s/).*//g" | sed "s/.*\s\+//g"`
5ddc162
winner=""
5ddc162
winnerDistance=999999
5ddc162
for x in $a_sources ; do
5ddc162
  distance=`levenshtein $x ${FILENAME}`
5ddc162
  if [ $distance -lt $winnerDistance ] ; then
5ddc162
    winner=$x
5ddc162
    winnerDistance=$distance
5ddc162
  fi
5ddc162
done
5ddc162
sum=`md5sum ${FILENAME}`
5ddc162
sed -i "s;.*$winner;$sum;" sources
58c4028
# now shenandoah hotspot
58c4028
winner=""
58c4028
winnerDistance=999999
58c4028
for x in $a_sources ; do
58c4028
  distance=`levenshtein $x ${FILENAME_SH}`
58c4028
  if [ $distance -lt $winnerDistance ] ; then
58c4028
    winner=$x
58c4028
    winnerDistance=$distance
58c4028
  fi
58c4028
done
58c4028
sum=`md5sum ${FILENAME_SH}`
58c4028
sed -i "s;.*$winner;$sum;" sources
58c4028
5ddc162
echo "New sources"
5ddc162
cat sources
2ef1e20
a_sources=`cat sources | sed "s/.*(//g" | sed "s/).*//g" | sed "s/.*\s\+//g"`
5ddc162
echo "    you can get inspired by following %changelog template:"
5ddc162
user_name=`whoami`
5ddc162
user_record=$(getent passwd $user_name)
5ddc162
user_gecos_field=$(echo "$user_record" | cut -d ':' -f 5)
5ddc162
user_full_name=$(echo "$user_gecos_field" | cut -d ',' -f 1)
5ddc162
spec_date=`date +"%a %b %d %Y"`
5ddc162
# See spec:
58c4028
revision_helper=`echo ${MAIN_VERSION%-*}`
5ddc162
updatever=`echo ${revision_helper##*u}`
58c4028
buildver=`echo ${MAIN_VERSION##*-}`
07a5b28
echo "* $spec_date $user_full_name <$user_name@redhat.com> - 1:1.8.0.$updatever-$RELEASE.$buildver" 
58c4028
echo "- updated to $MAIN_VERSION (from $PROJECT_NAME/$MAIN_REPO_NAME)"
58c4028
echo "- updated to $VERSION (from $PROJECT_NAME/$REPO_NAME) of hotspot"
5ddc162
echo "- used $FILENAME as new sources"
58c4028
echo "- used $FILENAME_SH as new sources for hotspot"
5ddc162
5ddc162
echo "    execute:"
5ddc162
echo "fedpkg/rhpkg new-sources "$a_sources
5ddc162
echo "    to upload sources"
5ddc162
echo "you can verify by fedpkg/rhpkg prep --arch XXXX on all architectures: x86_64 i386 i586 i686 ppc ppc64 ppc64le s390 s390x aarch64 armv7hl"
5ddc162