#!/bin/bash
set +o posix
shopt -s extglob

source /usr/share/slackdev/buildkit.sh

# Script : slackwarearm-current/source/x/x11/increment-localpkgs-build-numbers
# Purpose: Handle the package version & build numbers automatically
#          for the locally maintained X11 modular packages.
#
# by Stuart Winter <mozes@slackware.com>
#
# Copyright 2021  Stuart Winter, Surrey, England.
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

cd $PORTSRC/source/x/x11/build-nums/localpkgs || { echo "ERROR: couldn't change dir to the local build details" ; exit 1 ;}

# Find each local build details file and bump the build number.
# For version upgrades, these are handled manually since these
# are locally maintained.
find . -type f -mindepth 1 -maxdepth 1 -not -name '*EXCLUDE*' -printf "%f\n" | while read PKGNAME ; do
   printf "Package name.....: $PKGNAME\n"

   # Obtain existing details:
   if [ -s $PORTSRC/source/x/x11/build-nums/localpkgs/$PKGNAME ]; then
       source $PORTSRC/source/x/x11/build-nums/localpkgs/$PKGNAME
   fi

   [ -z "${MODBUILD}" ] && MODBUILD=1
   [ -z "${_PKGVER}" ] && _PKGVER="ERROR: No version found!"
   printf "\tPackage version..: $_PKGVER\n"
   printf "\tExisting build # : $MODBUILD\n"
   let MODBUILD++
   printf "\tNew build #......: $MODBUILD\n"
   # Update:
   sed -i 's?^MODBUILD=.*?'"MODBUILD=$MODBUILD"'?g' $PORTSRC/source/x/x11/build-nums/localpkgs/$PKGNAME
done

