#!/usr/bin/env bash
# ==========================================================================
#              ____        _ _     _     _____           _
#             | __ ) _   _(_) | __| |   |_   _|__   ___ | |___
#             |  _ \| | | | | |/ _` |_____| |/ _ \ / _ \| / __|
#             | |_) | |_| | | | (_| |_____| | (_) | (_) | \__ \
#             |____/ \__,_|_|_|\__,_|     |_|\___/ \___/|_|___/
#
#                           --- Build-Tools ---
#                https://www.nntb.no/~dreibh/system-tools/
# ==========================================================================
#
# Packaging Scripts
# Copyright (C) 2002-2026 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: thomas.dreibholz@gmail.com

# Bash options:
set -eu


# These are the supported distributions by Ubuntu Launchpad and Debian Mentors:
DPUT_DISTRIBUTIONS="
   trusty
   $(ubuntu-distro-info --supported)
   $(ubuntu-distro-info --supported-esm)
   $(ubuntu-distro-info --devel 2>/dev/null || echo "resolute")
   unstable
"


# ====== Handle arguments ===================================================
COPR=0
DPUT=0
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

DIRNAME="$(dirname "$0")"
DPUT_DISTRIBUTIONS="$(echo "${DPUT_DISTRIBUTIONS}" | xargs -n1 | sort -u)"
if [ ${DPUT} -eq 0 ] && [ ${COPR} -eq 0 ] ; then
   COPR=1
   DPUT=1
fi
if [ ! -e debian/ ] ; then
   DPUT=0
fi
if [ ! -e rpm/ ] ; then
   COPR=0
fi
COPR_CLI=""
if [ ${COPR} -ne 0 ] ; then
   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
fi


# ====== Build Ubuntu/Debian source packages ================================
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
   buildForDistributions=""
   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
         if [ "${buildForDistributions}" == "" ] ; then
            buildForDistributions="${dputDistribution}"
         else
            buildForDistributions="${buildForDistributions} ${dputDistribution}"
         fi
      fi
   done

   echo "Building for distributions: ${buildForDistributions}"
   echo "=> "${DIRNAME}"/build-tool make-source-deb ${buildForDistributions}"
   "${DIRNAME}"/build-tool make-source-deb ${buildForDistributions}
fi


# ====== Build Fedora source package ========================================
if [ ${COPR} -ne 0 ] ; then
   if [ -e rpm ] ; then
      echo "=> "${DIRNAME}"/build-tool make-source-rpm"
      "${DIRNAME}"/build-tool make-source-rpm
   else
      echo >&2 "ERROR: RPM files not found!"
      exit 1
   fi
fi


# ====== Run 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


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

   echo "Using ${COPR_CLI}"
   ${COPR_CLI} build --nowait ppa "${SRPM}"
fi
