#!/bin/bash
###
### This utility will prep the build root
### install dependent packages, build the spec
### then remove the build dependencies.
###

if [ ! "$1" ]; then
  echo "Usage: buildrpm hello.spec"
  exit 1
else
  SPEC=$1
fi

if [ -e "/etc/sysconfig/buildnode" ]; then
  . /etc/sysconfig/buildnode
else
  echo "Please configure your build node (/etc/sysconfig/buildnode)"
  exit 1
fi 

if [ ! "${SUDO_USER}" ]; then
  echo "This tool must be run as a user, executed by sudo."
  exit 1
fi

ARCH="$(uname -m)"

### Ugly, fix later!
HOME=$(su ${SUDO_USER} -c "echo \$HOME")

### How many cores are available?  We'll use all of them.
CORES=$(/usr/bin/getconf _NPROCESSORS_ONLN)
MAKEOPTS="-j${CORES}"
LOGDIR="/var/tmp/build-logs/${SUDO_USER}"


echo "Building package using [${CORES}] core(s)."

STAGEPATH="${STAGEPATH}/${TARGET}"
echo "Staging path: [${STAGEPATH}]"

EXTRA_OPTIONS="--with \"firmware\" --without \"debug, debuginfo\" --target=\"${ARCH}\""

PACKAGE=$(cat ${SPEC} | grep -s -e "^Name:" -e "^Version:" -e "^Release:" | sed -s "s#^.*: ##" | awk '{printf $1"-"}' | sed -s -e "s#-\$##" -e "s#\%{.*\$##")

### Prep the staging area where we move the packages
if [ ! -d "${STAGEPATH}/RPMS" ]; then
  mkdir -p "${STAGEPATH}/RPMS" 2>/dev/null
fi

if [ ! -d "${STAGEPATH}/SRPMS" ]; then
  mkdir -p "${STAGEPATH}/SRPMS" 2>/dev/null
fi

echo "Storing build logs may be found in [${LOGDIR}]"
if [ ! -d "${LOGDIR}" ]; then
   mkdir -p "${LOGDIR}"
fi

chown -R ${SUDO_USER}:${BUILDGROUP} "${LOGDIR}"
chmod -R ${BUILDPERMS} "${LOGDIR}"

if [ -e "${LOGDIR}/${PACKAGE}.log" ]; then
  rm ${LOGDIR}/${PACKAGE}.log
fi

### Yum needs quotes around some deps to install, fix this later!
#DEPENDENCIES=$(for dep in $(grep ^BuildRequires: ${SPEC}  | sed -s -e "s#BuildRequires: ##"); do echo -n "\"${dep}\" "; done)
DEPENDENCIES=$(for dep in $(grep ^BuildRequires: ${SPEC}  | sed -s -e "s#BuildRequires: ##"); do echo -n "${dep} "; done)

### Some packages have commas in the specs, standardize people!
DEPENDENCIES=$(echo ${DEPENDENCIES} | sed -s -e "s#,##")

echo "Installing Dependencies: [${DEPENDENCIES}]"
if [ "$DEPENDENCIES" ]; then
  YUM_INSTALL=$((yum --skip-broken -y install ${DEPENDENCIES} >/dev/null 2>&1 && echo SUCCESS) || echo FAILED)
fi

echo "Dependency installation: [${YUM_INSTALL}]"

echo "Building SPEC [${SPEC}]"
WD=$(pwd)
time su ${SUDO_USER} -c "QA_RPATHS=$[ 0x0001|0x0002|0x0010|0x0020 ] /usr/bin/rpmbuild -ba ${EXTRA_OPTIONS} ${WD}/${SPEC} 2>&1 | /usr/bin/tee ${LOGDIR}/${PACKAGE}.log"

DOESITBLEND=( $(find ${HOME}/rpmbuild/ 2>/dev/null | grep "\.rpm$" 2>/dev/null) )

MVRPM=0
for r in ${DOESITBLEND[@]}; do
  if [ -e "$r" ]; then
    if [[ "$r" =~ src.rpm$ ]]; then
      echo "Moving [${r}] to SRPMS.."
      mv ${r} ${STAGEPATH}/SRPMS
    else
      echo "Moving [${r}] to RPMS.."
      MVRPM=1
      mv ${r} ${STAGEPATH}/RPMS
    fi
  fi
done

echo "Cleaning out debug packages (we don't use them at this time)"
rm -f ${STAGEPATH}/RPMS/*debug* >/dev/null 2>&1

echo "Uninstalling package build dependencies"
if [ "${DEPENDENCIES}" ]; then
  if [[ "${YUM_INSTALL}" =~ "SUCCESS" ]] && [ ! "${MVRPM}" = "0" ]; then
    yum -y history undo $(yum history | grep -v -e "^ID" -e "^-" |awk '{print $1}' | grep "^[0-9]" | head -n 1)
  fi
fi

if [ "${MVRPM}" = "0" ]; then
  echo "Unable to build package, please review [${LOGDIR}/${PACKAGE}.log]."
  exit 1
fi


echo "Updating repository metadata."
cd ${STAGEPATH}/RPMS
createrepo --update --deltas .
cd ../SRPMS
createrepo --update --deltas .

echo "Fixing permissions"
cd ..
chown -R ${BUILDOWNER}:${BUILDGROUP} RPMS SRPMS
chmod -R ${BUILDPERMS} RPMS SRPMS

echo "Please remember to sign the RPMs before uploading."
echo "*** Remember to SIGN THIS RPM!"
echo "*** If this is a kernel, remember the kmods!  WHAT ABOUT THE KMODS!! :)"
