#!/bin/bash -e
# $Id$
#
# Packaging Scripts
# Copyright (C) 2014-2017 by Thomas Dreibholz
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
#
# Contact: dreibh@iem.uni-due.de


# ---------------------------------------------------------------------------
# USAGE:
# ./make-rpm default            => for default distribution (F24, amd64)
# ./make-rpm fedora-24-x86_64   => F24, amd64
# ./make-rpm fedora-24-i386     => F24, i386
# ...
# ---------------------------------------------------------------------------


DISTRIBUTIONS=`\
( \
while [ x$1 != "x" ] ; do \
   echo $1
   shift
done \
) | sort -u`
if [ "$DISTRIBUTIONS" == "" ] ; then
   DISTRIBUTIONS="fedora-24-x86_64"
fi

PACKAGE=`grep "^Name:" rpm/*.spec | head -n1 | sed -e "s/Name://g" -e "s/[ \t]*//g"`
PACKAGE_VERSION=`grep "^Version:" rpm/*.spec | head -n1 | sed -e "s/Version://g" -e "s/[ \t]*//g"`
PACKAGE_RELEASE=`grep "^Release:" rpm/*.spec | head -n1 | sed -e "s/Release://g" -e "s/[ \t]*//g"`

echo -e "\x1b[34m###########################################\x1b[0m"
echo -e "\x1b[34mPACKAGE:           $PACKAGE\x1b[0m"
echo -e "\x1b[34mPACKAGE_VERSION:   $PACKAGE_VERSION\x1b[0m"
echo -e "\x1b[34mPACKAGE_RELEASE:   $PACKAGE_RELEASE\x1b[0m"
echo -e "\x1b[34m###########################################\x1b[0m"

# ====== Check with Debian package's version ================================
if [ -e debian/changelog ] ; then
   DEBIAN_CHANGELOG_HEADER="`head -n1 debian/changelog`"
   # The package name, e.g. MyApplication
   DEBIAN_PACKAGE=`echo $DEBIAN_CHANGELOG_HEADER | sed -e "s/(.*//" -e "s/ //g"`
   # The package's version, e.g. 1.2.3-1ubuntu1
   DEBIAN_PACKAGE_VERSION=`echo $DEBIAN_CHANGELOG_HEADER | sed -e "s/.*(//" -e "s/).*//" -e "s/ //g" -e "s/ //g"`
   # The package's output version, e.g. 1.2.3-1ubuntu
   OUTPUT_VERSION=`echo $PACKAGE_VERSION   | sed -e "s/\(ubuntu\|ppa\)[0-9]*$/\1/"`
   # The package's Debian version, e.g. 1.2.3-1
   DEBIAN_VERSION=`echo $OUTPUT_VERSION    | sed -e "s/\(ubuntu\|ppa\)$//1"`
   # The package's upstream version, e.g. 1.2.3
   DEBIAN_UPSTREAM_VERSION=`echo $DEBIAN_VERSION  | sed -e "s/-[0-9]*$//"`

   if [ "$PACKAGE-$PACKAGE_VERSION" != "$DEBIAN_PACKAGE-$DEBIAN_UPSTREAM_VERSION" ] ; then
      echo >&2 "ERROR: RPM version and Debian version do not match -> $PACKAGE-$PACKAGE_VERSION vs. $DEBIAN_PACKAGE-$DEBIAN_UPSTREAM_VERSION!"
      exit 1
   fi
   if [ ! -e ./debian.conf ] ; then
      echo >&2 "ERROR: debian.conf does not exist -> no configuration for the new package!"
      exit 1
   fi
fi

if [ -e rpm.conf ] ; then
   . ./rpm.conf
fi

# ====== Create upstream package ============================================
echo -e ""
echo -e "\x1b[34m`date +%FT%H:%M:%S`: ====== Creating upstream package ======================\x1b[0m"
echo -e ""
if [ "$UPSTREAM_PACKAGE" = "" ] ; then
   UPSTREAM_PACKAGE="`find . -maxdepth 1 -name "$PACKAGE-*.gz" -printf '%f'`"
   UPSTREAM_PACKAGE_TYPE="gz"
   if [ ! -e "$UPSTREAM_PACKAGE" ] ; then
      UPSTREAM_PACKAGE="`find . -maxdepth 1 -name "$PACKAGE-*.bz2" -printf '%f'`"
      UPSTREAM_PACKAGE_TYPE="bz2"
   fi
   if [ ! -e "$UPSTREAM_PACKAGE" ]; then
      rm -f $PACKAGE-*.gz $PACKAGE"_"*.gz $PACKAGE-*.bz2 $PACKAGE"_"*.bz2
      echo -e "\x1b[34m------ Running MAKE_DIST ... ------\x1b[0m"
      echo "\$ $MAKE_DIST"
      eval "$MAKE_DIST"

      echo -e "\x1b[34m------ Looking for upstream package ... ------\x1b[0m"
      UPSTREAM_PACKAGE="`find . -maxdepth 1 -name $PACKAGE-*.gz -printf '%f'`"
      UPSTREAM_PACKAGE_TYPE="gz"
      if [ ! -e "$UPSTREAM_PACKAGE" ] ; then
         UPSTREAM_PACKAGE="`find . -maxdepth 1 -name $PACKAGE-*.bz2 -printf '%f'`"
         UPSTREAM_PACKAGE_TYPE="bz2"
      fi
      if [ ! -e "$UPSTREAM_PACKAGE" ] ; then
         UPSTREAM_PACKAGE="`find . -maxdepth 1 -name $PACKAGE-*.xz -printf '%f'`"
         UPSTREAM_PACKAGE_TYPE="xz"
      fi
   fi
fi
if [ ! -e "$UPSTREAM_PACKAGE" ] ; then
   echo -e "\x1b[34mERROR: No upstrem package found!\x1b[0m"
   exit 1
fi
echo -e ""
echo -e "\x1b[34m==> Upstream package is $UPSTREAM_PACKAGE (type is $UPSTREAM_PACKAGE_TYPE)\x1b[0m"
echo -e ""


# ====== Create RPMs ========================================================
echo -e ""
echo -e "\x1b[34m`date +%FT%H:%M:%S`: ====== Creating source RPM ============================\x1b[0m"
echo -e ""

# Remove old files
rpmdev-setuptree
find $HOME/rpmbuild/SRPMS -name "$PACKAGE*.rpm" | xargs --no-run-if-empty rm -f
find $HOME/rpmbuild/RPMS  -name "$PACKAGE*.rpm" | xargs --no-run-if-empty rm -f
# Copy upstream sources
cp $UPSTREAM_PACKAGE $HOME/rpmbuild/SOURCES/
# Patches
if [ -e original ] ; then
   find original -name "*" -type f | grep -v "~$" | grep -v ".spec$" | xargs -n1 -i§ cp "§" $HOME/rpmbuild/SOURCES/
fi
# Further patches
find rpm -name "*" -type f | grep -v "~$" | grep -v ".spec$" | xargs -n1 -i§ cp "§" $HOME/rpmbuild/SOURCES/
# The .spec file
cp rpm/$PACKAGE.spec $HOME/rpmbuild/SPECS/

# Create source RPM
rpmbuild -bs rpm/$PACKAGE.spec
PACKAGE_SRPM=`find $HOME/rpmbuild/SRPMS/ -name "$PACKAGE-*$PACKAGE_VERSION*-*.src.rpm"`
if [ ! -e "$PACKAGE_SRPM" ] ; then
   echo >&2 "ERROR: Cannot find SRPM $PACKAGE_SRPM!"
   exit 1
fi

# Create binary RPMs
for DISTRIBUTION in $DISTRIBUTIONS ; do
   echo -e ""
   echo -e "\x1b[34m`date +%FT%H:%M:%S`: ====== Creating binary RPM for $DISTRIBUTION ==========\x1b[0m"
   echo -e ""

   # Remove old files
   find /var/lib/mock/$DISTRIBUTION/result -name "$PACKAGE-*.rpm" | xargs --no-run-if-empty rm -f

   # Build the binary RPM
   mock -r $DISTRIBUTION --init
   mock -r $DISTRIBUTION --installdeps $PACKAGE_SRPM
   mock -r $DISTRIBUTION --install openssl pesign
   mock -r $DISTRIBUTION --no-clean --rebuild $PACKAGE_SRPM

   # Check whether files are at the right location
   PACKAGE_RPMS=`find /var/lib/mock/$DISTRIBUTION/result -name "$PACKAGE-*$PACKAGE_VERSION*-*.rpm" | grep -v "$PACKAGE-*$PACKAGE_VERSION*-*.src.rpm" || true`
   if [ "$PACKAGE_RPMS" == "" ] ; then
      echo >&2 "ERROR: Cannot find RPMs!"
      exit 1
   fi
done
