#!/bin/bash
#
# $Id$
#
# PBA localrpmbuild program
#
# (C) ITEG IT-Engineers GmbH 2010-2021
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#   http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# tries a local pba-build using /tmp/pba-rpmspool and /tmp/pba-rpmresults,
# logging to /tmp/pba-rpmrelease.log and /tmp/pba-rpmbuild.log
#
# requires local pba chroot tarballs installed and sudo privileges
#
# supports $* like: -o 8

sudo rm -rf /tmp/pba-rpmspool /tmp/pba-rpmresults /tmp/pba-rpmrelease.log /tmp/pba-rpmbuilder.log 

mkdir /tmp/pba-rpmspool /tmp/pba-rpmresults

echo "Calling   pba-rpmrelease -u -b /tmp/pba-rpmspool/ $*" |tee /tmp/pba-rpmrelease.log
pba-rpmrelease -u -b /tmp/pba-rpmspool/ $* 2>&1 |tee /tmp/pba-rpmrelease.log
if [ ${PIPESTATUS[0]} != "0" ] ; then
  echo "FAILED to   pba-rpmrelease -u -b /tmp/pba-rpmspool/ $*"
  sudo chmod -R ugo+rx /tmp/pba-rpmrelease.log 2>/dev/null
  ls -ld /tmp/pba-rpmrelease.log 
  echo "Evtl. call   gvim /tmp/pba-rpmrelease.log"
  exit 11
fi

#exit 99

echo "Calling   sudo pba-rpmbuilder -B /tmp/pba-rpmspool/ -b /tmp/pba-rpmresults/"
sudo pba-rpmbuilder -B /tmp/pba-rpmspool/ -b /tmp/pba-rpmresults/ $* 2>&1 |tee /tmp/pba-rpmbuilder.log
if [ ${PIPESTATUS[0]} != "0" ] ; then
  echo "FAILED to   sudo pba-rpmbuilder -B /tmp/pba-rpmspool/ -b /tmp/pba-rpmresults/"
  #exit 12
fi

sudo chmod -R ugo+rx /tmp/pba-rpmrelease.log /tmp/pba-rpmbuilder.log /tmp/pba-rpmresults 2>/dev/null

ls -ld /tmp/pba-rpmrelease.log /tmp/pba-rpmbuilder.log /tmp/pba-rpmresults/*rpm* 2>/dev/null

echo "Evtl. call   gvim /tmp/pba-rpmbuilder.log"

