#!/usr/bin/env bash
#
# Packaging Scripts
# Copyright (C) 2002-2021 by Thomas Dreibholz
# SPDX-License-Identifier: GPL-3.0-or-later
#
# 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

# Bash options:
set -e

# ====== Path variables =========================================
FILE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
P4C_DIR="${FILE_DIR}/../.."
DEBIAN_DIR="${P4C_DIR}/debian"

COPR=0
DPUT=0

# NOTE: These are the supported distributions by Ubuntu Launchpad and Debian:
DPUT_DISTRIBUTIONS="precise trusty xenial bionic focal groovy hirsute   unstable"

while [ $# -gt 0 ] ; do
   if [ "$1" == "dput" ] ; then
      DPUT=1
   elif [ "$1" == "copr" ] ; then
      COPR=1
   elif [ "$1" == "all" ] ; then
      DPUT=1
      COPR=1
   else
      echo >&2 "Usage: $0 [dput] [copr] [all]"
      exit 1
   fi
   shift
done

if [ ${DPUT} -eq 0 -a ${COPR} -eq 0 ] ; then
   COPR=1
   DPUT=1
fi

if [ ! -e debian/ ] ; then
   DPUT=0
fi
if [ ! -e rpm/ ] ; then
   COPR=0
fi


# ====== Build source packages ==============================================
./clean-deb
if [ ${DPUT} -ne 0 ] ; then
   . ./packaging.conf
   if [ "${NOT_TARGET_DISTRIBUTIONS}" == "" ] ; then
      echo >&2 "ERROR: Define NOT_TARGET_DISTRIBUTIONS in packaging.conf!"
      exit 1
   fi
   buildForDistribution=""
   for dputDistribution in ${DPUT_DISTRIBUTIONS} ; do
      notATarget=0
      for notTargetDistribution in ${NOT_TARGET_DISTRIBUTIONS} ; do
         if [ "${notTargetDistribution}" == "${dputDistribution}" ] ; then
            notATarget=1
            break
         fi
      done
      if [ $notATarget -eq 0 ] ; then
         buildForDistribution="${buildForDistribution} ${dputDistribution}"
      fi
   done

   echo "Building for distributions:${buildForDistribution}"
   ./make-deb ${buildForDistribution}
fi

if [ ${COPR} -ne 0 ] ; then
   if [ -e rpm -o -e ./make-srpm ] ; then
      ./make-srpm || exit 1
   else
      echo >&2 "ERROR: RPM files not found!"
      exit 1
   fi
fi


# ====== dput ===============================================================
if [ ${DPUT} -ne 0 ] ; then
   changeFiles=`find . -mindepth 1 -maxdepth 1 -name "*.changes"`
   for changeFile in ${changeFiles} ; do
      if [[ ${changeFile} =~ -[0-9]_source.changes$ ]] ; then
         dput mentors ${changeFile}
      else
         dput ppa ${changeFile}
      fi
   done
fi


# ====== COPR ===============================================================
if [ ${COPR} -ne 0 ] ; then
   PACKAGE=`grep "^Name:" rpm/*.spec | head -n1 | sed -e "s/Name://g" -e "s/[ \t]*//g"`
   SRPM=`find ${HOME}/rpmbuild/SRPMS -name "${PACKAGE}-*.src.rpm"`
   if [ ! -e "${SRPM}" ] ; then
      echo >&2 "ERROR: ${PACKAGE}-*.src.rpm not found!"
      exit 1
   fi

   COPR_CLI=""
   for directory in /usr/bin /usr/local/bin ~/.local/bin ; do
      if [ -x "${directory}/copr-cli" ] ; then
         COPR_CLI="${directory}/copr-cli"
         break
      fi
   done
   if [ "${COPR_CLI}" == "" ] ; then
      echo >&2 "ERROR: copr-cli not found!"
      exit 1
   fi
   echo "Using ${COPR_CLI}"

   ${COPR_CLI} build --nowait ppa ${SRPM}
fi
