#!/bin/bash

# Copyright (c) 2020 Adrian Schröter <adrian@suse.de>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library  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 Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; see the file COPYING.LIB. If not,
# write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA  02111-1307, USA

#
# This just basic demo code for now, to be rewritten/completed later
#


out=/.build.packages/obsgendiff
outreleased=/.build.packages/obsgendiff.released

echo "Running obsgendiff data differ..."

# extract released obsgendiff aggregates
for obsgendiff in /.build.packages/SOURCES/*.obsgendiff; do
  [ -e "$obsgendiff" ] || continue
  mkdir -p "${outreleased}"
  tar xfv "$obsgendiff" -C "${outreleased}"
done

# create changelogs based on the packaged rpms
mkdir -p $out/{changelogs,disturl}
for report in /.build.packages/OTHER/*.report; do
  sed -n -e 's,.*<binary .*obs://\(.*\)</binary>,\1,p' "$report" | while read binary; do

     rpm="${binary##*/}"
     name="${rpm%-*}"
     name="${name%-*}"

     # only the worker knows where it was downloaded from....
     # the disturl may contained a different build repo
     file=`find /.build.packages/SOURCES/repos/ -name $rpm`

     rpm -qp "$file" --changelog 2>/dev/null > $out/changelogs/${name}
     rpm -qp "$file" --qf '%{DISTURL}\n' 2>/dev/null > $out/disturl/${name}
  done

  # create archive
  cd $out
  gendiff=${packages%.packages}.obsgendiff
  tar cfJ /.build.packages/OTHER/${gendiff##*/} *
  cd -
done

# create diff to released archive
# NOTE: it had to be published or it won't exist
if [ -d "${outreleased}" ]; then
  changelog=/.build.packages/OTHER/ChangeLog.txt
  echo ""> $changelog
  echo "Removed packages">> $changelog
  echo "================">> $changelog
  echo "">> $changelog

  # new packages
  find "$outreleased/" -type f | sort | sed "s,^$outreleased/,," | while read file; do
    [ -e "${out}/$file" ] || echo " - $file" >> $changelog
  done

  # new packages
  echo "">> $changelog
  echo "New packages">> $changelog
  echo "============">> $changelog
  echo "">> $changelog
  find "$out/" -type f | sort | sed "s,^$out/,," | while read file; do
    [ -e "${outreleased}/$file" ] || echo " - $file" >> $changelog
  done

  # changed packages
  echo "">> $changelog
  echo "Package updates">> $changelog
  echo "===============">> $changelog
  echo "">> $changelog
  diff -ur "${outreleased}/" "$out/" >> $changelog
fi

exit 0

