#!/bin/bash

# A simple script to Replace @VERSION@ in a kiwi file based on a pkg version
#  
# (C) 2018 by Dominique Leuenberger <dimstar@opensuse.org>
#  
# 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 2  
# of the License, or (at your option) any later version.  
# See http://www.gnu.org/licenses/gpl-2.0.html for full license text.  

# defaults
PKGNAME=""
FILENAME=""

while test $# -gt 0; do
  case $1 in
    *-package)
      PKGNAME="$2"
      shift
    ;;
    *-filename)
      FILENAME="$FILES ${2##*/}"
      shift
    ;;
    *-outdir)
      MYOUTDIR="$2"
      shift
    ;;
    *)
      echo Unknown parameter $1.
      echo 'Usage: pkg_version --package $PKGNAME --filename $FILENAME --outdir $OUT'
      exit 1
    ;;
  esac
  shift
done

PKGVER=$(find /usr/src/packages/SOURCES/repos -name *.rpm| while read RPM; do
  rpm -qp --qf "%{name}\t%{version}\n" $RPM | awk "/^${PKGNAME}\t/ {print \$2; exit 1}" || break;
done)

echo "Patching $FILENAME to contain Version $PKGVER"
sed -i "s|@VERSION@|${PKGVER}|g" $FILENAME

exit 0
