#!/bin/bash
#
# This file is part of the Collabora Office project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# create debuginfo and debugsource packages from LO rpm & deb files
# (generated by using --enable-symbols)

# build path
export BUILD_PATH=$PWD
export BUILD_LOG=$BUILD_PATH/pack-debug.log

if type -P pigz &>/dev/null; then
    GZIP=pigz
else
    GZIP=gzip
fi

if [ $# -gt 0 ]
then
 for i in "$@"
 do
  case $i in
--only-rpm) ONLY_RPM=1;;
--only-deb) ONLY_DEB=1;;
*) echo "Usage: pack-debug [--only-rpm | --only-deb]" && exit 1;;
  esac
 done
fi

# set install dirname and product version
if [ ! -f config.log ]; then
    echo "config.log not found. Run this script from build root."
    exit 1;
fi

# create pack-debug.log file
echo create debug packages >$BUILD_LOG

eval $(grep ^INSTALLDIRNAME config.log)

# set package base name, eg. collaboraoffice
DEBUGSRC_PACKAGENAME=$INSTALLDIRNAME

#################################
# Function for re-build RPM files
#################################
function repack_rpm {

# set environment based on config.log
# for find-requires-x11.sh used by rpm __find_requires
eval $(grep ^PLATFORMID config.log)
export PLATFORMID
eval $(grep ^build_cpu config.log)
export build_cpu

####################################
echo create RPM debug source package
####################################

DEBUGSRC="$(find workdir -name ${DEBUGSRC_PACKAGENAME}.spec.log)"

# create spec file, based on the spec file of the brand package

cat $DEBUGSRC | awk '
	/^Name:/ { print "Summary: Debug source for package "$2; print $0"-debugsource";next }
	/^Group:/ { print $1" Development/Debug";next }
	/^Brand module/ { print gensub("Brand module", "Source files", "");next }
	/^%attr/ || /^Summary:/ { next }
	{print}
	END {
		print "%defattr(-,root,root)"
	}
 ' > ${DEBUGSRC}-debugsource
 buildroot=$(cat $DEBUGSRC-debugsource | awk '/^BuildRoot/{print$2}')
 topdir=$(dirname $(dirname $buildroot))
 mkdir -p $buildroot $topdir/RPMS/BUILD $topdir/RPMS/RPMS
 rm -rf $buildroot

 # create source file list

 find $BUILD_PATH -name '*[.][hc]xx' -o -name '*[.][hc]' | grep -Ev '/(instdir|qa|DEBS)/' |

 # list all directories for complete rpm remove

 awk -v home=$BUILD_PATH '
	{
		split($0, a, home "/")
		n=split(a[2], b, "/")
		c=home
		for(i=1;i<n;i++) {
			c=c"/"b[i]
			if(mem[c]!=1) print "%dir "c
			mem[c]=1
		}
		print $0
	}' >> ${DEBUGSRC}-debugsource

 # start rpmbuild for debug source package

 ln -s / $buildroot

 # debug build source package

 rpmbuild -bb --define "_unpackaged_files_terminate_build  0" --define "_binary_payload w2.xzdio" --define "_binary_filedigest_algorithm 8" ${DEBUGSRC}-debugsource --target $build_cpu --buildroot=$buildroot


echo Update RPM download tar.gz
rpmdir=$(echo $topdir | sed 's/_inprogress$//')
mv $topdir/RPMS/RPMS/*/*.rpm $rpmdir/RPMS/
cd $rpmdir/..
TARGET_RPM=$(ls *_download/*.tar.gz)
TARGET_DEBUG=$(echo $TARGET_RPM | sed 's/.tar.gz$/-debug.tar.gz/')
SOURCE_RPM=$(find *_rpm -type f | grep -v debug)
SOURCE_DEBUG=$(find *_rpm -type f | grep -E '(debug|readme|README)')
tar c $SOURCE_RPM | $GZIP >$TARGET_RPM
tar c $SOURCE_DEBUG | $GZIP >$TARGET_DEBUG
cd $BUILD_PATH
rm -rf $topdir
}

#################################
# Function for re-build DEB files
#################################
function repack_deb {

####################################
echo create DEB debug source package
####################################

DEBUGSRC=$BUILD_PATH/workdir/installation/CollaboraOffice/deb/listfile/en-US/epm_gid_Module_Root_Brand.lst

echo Base spec file: $DEBUGSRC

# create spec file, based on the spec file of the brand package

cat $DEBUGSRC | awk '
	/^%product/ { print gensub("Brand module", "Debug source package", "", $0) ;next }
	/^%description/ { print gensub("Brand module", "Debug source package", "", $0) ;next }
	/^[cdf] / { next }
	{print}
 ' > ${DEBUGSRC}-debugsource

 # create source file list

 find $BUILD_PATH -name '*[.][hc]xx' -o -name '*[.][hc]' | grep -Ev '/(instdir|qa|DEBS)/' |

 # list all directories

 awk -v home=$BUILD_PATH '
	{
		split($0, a, home "/")
		n=split(a[2], b, "/")
		c=home
		for(i=1;i<n;i++) {
			c=c"/"b[i]
			if(mem[c]!=1) print "d 755 root root "c" -"
			mem[c]=1
		}
		print "f 644 root root "$0" "$0
	}' >> ${DEBUGSRC}-debugsource

 echo Spec file of debug source package: ${DEBUGSRC}-debugsource

 # debug build source package

 $BUILD_PATH/workdir/UnpackedTarball/epm/epm -f deb -g ${INSTALLDIRNAME}-debugsource ${DEBUGSRC}-debugsource --output-dir DEBS -v

echo Update DEB download tar.gz

debdir=$(ls -d $BUILD_PATH/workdir/installation/CollaboraOffice/deb/install/*_deb)
mv $BUILD_PATH/DEBS/*.deb $debdir/DEBS/
cd $debdir/..
TARGET_DEB=$(ls *_download/*.tar.gz)
TARGET_DEBUG=$(echo $TARGET_DEB | sed 's/.tar.gz$/-debug.tar.gz/')
SOURCE_DEB=$(find *_deb -type f | grep -v debug)
SOURCE_DEBUG=$(find *_deb -type f | grep -E '(debug|readme|README)')
tar c $SOURCE_DEB | $GZIP >$TARGET_DEB
tar c $SOURCE_DEBUG | $GZIP >$TARGET_DEBUG

cd $BUILD_PATH
rm -rf DEBS
}

# start deb re-build
test -z "$ONLY_RPM" -a "$(find workdir/installation/CollaboraOffice/deb/listfile -name '*.lst')" != "" && repack_deb >$BUILD_LOG 2>&1 || \
	echo 'Skip DEB debug package generation (--only-rpm or missing EPM lst files).'

# start rpm re-build
test -z "$ONLY_DEB" -a "$(find workdir -name '*spec.log')" != "" && repack_rpm >>$BUILD_LOG 2>&1 || \
	echo 'Skip RPM debug package generation (--only-deb or missing RPM spec files).'

