#!/bin/bash
########################################################################
# build_all
#
# Do the full package and RPM build. Should be invoked from gratia/probe
# area with:
#
#   build/build_all <options> <args>
#
# See build/build_all -h for details.
#
# 2007/09/11 CHG for the Gratia project.
########################################################################

# Defaults
#rpmbuild_top=`grep -e '^%_topdir' ~/.rpmmacros | cut -d' ' -f2`
rpmbuild_top=`rpm --eval %{_topdir}`
[[ -n "$rpmbuild_top" ]] || rpmbuild_top=~/rpmbuild

specfile="`pwd`/build/gratia-probe.spec"
[[ -f "$specfile" ]] || specfile=~/gratia/probe/build/gratia-probe.spec

usage() {
  cat 1>&2 <<EOF
usage: build_all -h
       build_all [-q ] [-d <rpm-macro-definition>] [-P [-Y]] [-p] [-i] \
                 [-r <rpmbuild-top>] [-s <specfile>] [--] [<version> [<release>]]

Options:
  [-q]
    Quiet: suppress RPM build output.
  [-d <rpm-macro-definition>]
    Define an RPM macro
  [-P]
    Publish to AFS (gratia.fnal.gov/Files)
  [-Y]
    Create or freshen yum repository (requires -P)
  [-p]
    Prepare-only (no RPM build)
  [-i]
    Install-only (no RPM packaging)
  [-r <rpmbuild-top>]
    Top of RPM build area
  [-s <specfile>]
    Specfile to use for RPM build (default $specfile)

Defaults:
  rpmbuild-top=${rpmbuild_top}
  specfile=${specfile}
  release=1
EOF
  exit 1
}

installopt="-ba"

while getopts :hYd:r:s:piPq OPT; do
    case $OPT in
  d)
      if [[ -n "$defines" ]]; then
        defines="$defines --define='$OPTARG'"
      else
        defines="--define='$OPTARG'"
      fi
      ;;
  h)
      usage
      ;;
  i)
      installopt="-bi"
      ;;
  P)
      publish=1
      ;;
  Y)
      yum=1
      ;;
  p)
      prepare_only=1
      ;;
  q)
      quiet=1
      ;;
  r)
      rpmbuild_top="$OPTARG"
      ;;
  s)
      specfile="$OPTARG"
      ;;
  *)
      usage
    esac
done
shift $[ OPTIND - 1 ]

if [[ -f "$specfile" ]]; then
  echo "Found specfile at $specfile"
else
  echo "Unable to find specfile $specfile" 1>&2
  exit 1
fi

TMP=`mktemp -d ${TMPDIR:-/tmp}/build_all.XXXXXXXXXX`
if [[ -z "$TMP" ]]; then
  echo "Unable to create temporary file using mktemp" 1>&2
  exit 1
fi

trap "[[ -d \"$TMP\" ]] && rm -rf \"$TMP\" 2>/dev/null" EXIT

tmp_specfile="${TMP}/`basename ${specfile}`"

if [[ -n "${1}" ]]; then
  version=${1}
  release=${2:-1}

  perl -wape '
s&^(\s*Version\s*:\s*).*$&${1}'"${version}"'&i;
s&^(\s*Release\s*:\s*).*$&${1}'"${release}"'&i;
' ${specfile} > "${tmp_specfile}"
else
  version=`perl -wane 'm&^\s*Version\s*:\s*([^#]*)& and print "$1\n";' "${specfile}"`
  release=`perl -wane 'm&^\s*Release\s*:\s*([^#]*)& and print "$1\n";' "${specfile}"`

  echo "Packaging for default $version-$release"
  cp -p "${specfile}" "${tmp_specfile}"
fi


build/package-probe "${version}" common condor pbs-lsf psacct sge glexec \
                                 metric services hadoop-storage condor-events \
                                 dCache-transfer gridftp-transfer \
                                 dCache-storage xrootd-transfer xrootd-storage \
                                 bdii-status onevm slurm
status=$?

if (( $status == 0 )); then
  build/populate_SOURCES -r "${rpmbuild_top}"
  status=$?
else
  exit $status
fi

if [[ -n "$quiet" ]]; then
  redir=">/dev/null 2>&1"
fi

if (( $status == 0 )); then
  if  [[ -n "$prepare_only" ]]; then
    set -x
    eval rpmbuild -bp $defines "${tmp_specfile}" $redir
    exit $?
  else
    set -x
    eval rpmbuild $installopt $defines "${tmp_specfile}" $redir
    status=$?
    set +x
  fi
else
  exit $status
fi

if (( $status == 0 )); then
  set -x
  eval rpmbuild $installopt $defines --target=noarch "${tmp_specfile}" $redir
  status=$?
  set +x
  if [[ "$installopt" != "-ba" ]]; then
    exit $status
  fi
else
  exit $status
fi

if (( $status == 0 )); then
  set -x
  eval rpmbuild $installopt $defines "--define='config_itb 1'" "${tmp_specfile}" $redir
  status=$?
  set +x
else
  exit $status
fi

if (( $status == 0 )); then
  set -x
  eval rpmbuild $installopt $defines "--define='config_itb 1'" --target=noarch "${tmp_specfile}" $redir
  set +x
  status=$?
else
  exit $status
fi

publish_host="flxi07.fnal.gov"
publish_dir=/afs/fnal.gov/files/expwww/gratia/html/Files/probe
yum_cache=/afs/fnal.gov/files/expwww/gratia/yum-repo-cache

if (( $status == 0 )) && (( ${publish:-0} )); then
  echo "Publishing files to AFS for web access ..." 1>&2
  scp -p `find "${rpmbuild_top}"/RPMS -name "gratia-probe*-$version-$release.*.rpm"` \
   "${publish_host}:${publish_dir}/RPMS/" && \
  scp -p "${rpmbuild_top}/SRPMS/gratia-probe-$version-$release.src.rpm" \
   "${publish_host}:${publish_dir}/SRPMS/"
  status=$?
  publish_script=/tmp/public_script.$$
  echo "
  #!/bin/sh -x
  if (( $status == 0 )) && (( ${yum:-0} )); then
    if type -p createrepo; then
      createrepo -c \"${yum_cache}\" -p \"${publish_dir}/RPMS\"
    else
      echo \"Unable to find createrepo command to update yum repository metadata\" 1>&2
      exit 1
    fi
    if type -p yum-arch; then
      yum-arch \"${publish_dir}/RPMS\"
    else
      echo \"Unable to find yum-arch command to update old-style yum repository metadata\" 1>&2
      exit 1
    fi
  fi" > ${publish_script}
  chmod u+x ${publish_script}
  scp ${publish_script} ${publish_host}:/tmp
  ssh ${publish_host} ${publish_script} \; rm ${publish_script}
  status=$?
else
  exit $status
fi

exit $status
