#!/bin/bash
#
# Autodist script for updating specfiles
# 
# Copyright (c) 2007-2024 by Silvan Calarco <silvan.calarco@mambasoft.it>
# Released with the same license as autodist
#

SPECFILE=
NEWVERSION=
BUILDREQNUM=0

for ((i=1; i<=$#; i++)); do
   case ${!i} in
      --buildreq) let i+=1
                  if [ ! "${!i}" ]; then
                     echo "ERROR: --buildreq requires at least an argument; aborting."
                     exit 1
                  fi
                  let BUILDREQNUM+=1
                  BUILDREQ[$BUILDREQNUM]=${!i}
                  if [ "${!i+1}" -a "${!i+1:0:1}" != "-" ]; then
                     let i+=1
                     BUILDREQREL[$BUILDREQNUM]=${!i}
                     if [ ! "${!i+1}" -o "${!i+1:0:1}" = "-" ]; then
                        echo "ERROR: update-specfile: --buildreq requires one or three arguments; aborting."
                        exit 1
                     fi
                     let i+=1
                     BUILDREQVER[$BUILDREQNUM]=${!i}
                  fi
                  ;;
      *) if [ ! "$SPECFILE" ]; then
            SPECFILE=${!i}
         elif [ ! "$NEWVERSION" ]; then
            NEWVERSION=${!i}
         else
            echo "ERROR: update-specfile: unrecognized script option: '${!i}'; aborting."
            exit 1
         fi
   esac
done

[ "$SPECFILE" ] || {
   echo "ERROR: update-specfile: specfile not given; aborting."
   exit 1
}

[ -e $SPECFILE ] || {
   echo "Error: update-specfile: file $1 does not exist; aborting."
   exit 1
}

# Distribution global changes
sed -i "s|^Vendor:.*|Vendor:        openmamba|;
        s|^Distribution:.*|Distribution:  openmamba|;
        /^%debug_package.*/d;
        /^#%patch.*/d;
        s|^%patch -p|%patch 0 -p|;
        s|^%patch0|%patch 0|;
        /BuildRequires:[[:space:]]*libffmpeg-devel/d;
        /^BuildRequires:[[:space:]]*libkdegames-devel/d;
        /^BuildRequires:[[:space:]]*libkdegames5-devel/d;
        /^BuildRequires:[[:space:]]*python3.7dist/d;
        /^BuildRoot:[[:space:]]*.*/d;
        \,%post.* -p /sbin/ldconfig$,d;
        s|\(BuildRequires:[[:space:]]*\)libmysql-devel|\1libmysql5-devel|;
        s|\(BuildRequires:[[:space:]]*\)libdb42-devel|\1libdb47-devel|;
        s|\(BuildRequires:[[:space:]]*\)libdb51-devel|\1libdb53-devel|;
        s|\(BuildRequires:[[:space:]]*\)firefox-devel|\1xulrunner-devel|;
        s|^PreReq:[[:space:]]*/sbin/install-info|Requires(post):%{__install_info}|;
        s|^PreReq:[[:space:]]*%{__install_info}|Requires(post):%{__install_info}|;
        s|^PreReq:[[:space:]]*|Requires(pre): |;
        s|^\(Requires:[[:space:]]*kde-workspace\)$|#\1|;
        s|^%patch\([0-9][0-9]*\)|%patch \1|;
        s|http://.*.dl\.sourceforge\.net/|https://downloads.sourceforge.net/|;
        s|http://ftp.kde.org/stable/|https://download.kde.org/stable/|;
        s|ftp://ftp.kde.org/pub/kde/|https://download.kde.org/|;
        s|update-mime-database %{|update-mime-database -n %{|;
        s|http://download.kde.org/stable/%{version}|https://download.kde.org/stable/applications/%{version}|;
        s|http[s]*://pypi.python.org/packages/source/[^/]*/|https://pypi.debian.net/|;
        s|http://ftp.gnome.org/pub/GNOME/sources/|https://download.gnome.org/sources/|;
        s|ftp://ftp.gnome.org/pub/gnome/sources/|https://download.gnome.org/sources/|" $SPECFILE
#        s|;s,.*/man/.*,&.gz,g||;
#        s|http.*.cpan.org/.*/\([^/-]*\)\(-.*\)|https://cpan.metacpan.org/modules/by-module/\1/\1\2|;
#sed -i "s|^\(Source.*:[[:space:]]*ftp://ftp.kde.org/pub/kde/stable/.*.tar.\)bz2|\1xz|" $SPECFILE

if [ $BUILDREQNUM -gt 0 ]; then
   grep "^## AUTOBUILDREQ-END" $SPECFILE > /dev/null || {
      echo "ERROR: update-specfile: missing AUTOBUILDREQ block; aborting."
      exit 1
   }
   for b in `seq 1 $BUILDREQNUM`; do
      line=${BUILDREQ[$b]}
      if [ "${BUILDREQREL[$b]}" ]; then
         line="$line ${BUILDREQREL[$b]} ${BUILDREQVER[$b]}"
      fi
      sed -i "/^## AUTOBUILDREQ-END/,9999{/BuildRequires:[[:space:]]*${BUILDREQ[$b]}$/d}" $SPECFILE
      sed -i "/^## AUTOBUILDREQ-END/,9999{/BuildRequires:[[:space:]]*${BUILDREQ[$b]}[[:space:]]/d}" $SPECFILE
      sed -i "/^## AUTOBUILDREQ-END/a BuildRequires: $line" $SPECFILE
   done
fi
exit 0
