Script "patch_raw_srcpkg.sh"
#!/bin/sh

# --------------------------
# patch_raw_srcpkg.sh
# version 2.5.4.5
# --------------------------

# check for parameters
if [ ! $# -eq 1 ]; then
  echo "ERROR: exactly one parameter (package title), please. Abort."
  exit 1
fi

[ -z "${BUILD_FARM}" ] && BUILD_FARM="${HOME}/build-farm"
if [ ! -d "${BUILD_FARM}" ]
then
  echo "ERROR: build-farm directory '${BUILD_FARM}' doesn't exist. Abort."
  exit 1
fi

PKG_TITLE=`basename "${1}"`
PKG_MODE=`dirname "${1}"`
[ -z "${PKG_MODE}" ] && PKG_MODE="."

[ -z "${PKGBS_DIR}" ] && PKGBS_DIR="${BUILD_FARM}"/BUILD-SCRIPTS
[ -d "${PKGBS_DIR}" ] || PKGBS_DIR=`pwd`
CONFIG_PATH="${PKGBS_DIR}/${PKG_MODE}"/CONFIG
PPFILES_PATH="${PKGBS_DIR}/${PKG_MODE}"/PPFILES

# check for required binaries
REQD_BINS=""
which sed >/dev/null 2>&1 || type sed >/dev/null 2>&1 || REQD_BINS="sed,${REQD_BINS}"
which tar >/dev/null 2>&1 || type tar >/dev/null 2>&1 || REQD_BINS="tar,${REQD_BINS}"
which grep >/dev/null 2>&1 || type grep >/dev/null 2>&1 || REQD_BINS="grep,${REQD_BINS}"
if [ ! -z "${REQD_BINS}" ]; then
  echo -e `basename ${0}`: "Required binaries (${REQD_BINS}\010) are not present."
  exit 1
fi

[ -z "${SRCPKG_STORE}" ] && SRCPKG_STORE="${BUILD_FARM}"/PACKAGES/srcpkg/"${PKG_MODE}"
if [ ! -d "${SRCPKG_STORE}" ]; then
  mkdir -p "${SRCPKG_STORE}" 2>/dev/null || echo "WARNING: cannot create storage directory for source packages"
fi
[ -d "${BUILD_FARM}/${PKG_MODE}" ] || mkdir -p "${BUILD_FARM}/${PKG_MODE}"

SRCPKG_DIR=`ls -t "${BUILD_FARM}/${PKG_MODE}" | grep "${PKG_TITLE}" | grep srcpkg | grep -v ".tar." | head -1`
SRCPKG_PATH="${BUILD_FARM}/${PKG_MODE}/${SRCPKG_DIR}"

if [ ! -z "${SRCPKG_DIR}" ]; then
  echo "found srcpkg directory:" "${SRCPKG_DIR}"
else
  # try to look at top level of build farm
  SRCPKG_DIR=`ls -t "${BUILD_FARM}" | grep "${PKG_TITLE}" | grep srcpkg | grep -v ".tar." | head -1`
  if [ ! -z "${SRCPKG_DIR}" ]; then
    # move srcpkg directory to the correct level
    echo "found srcpkg directory at top level of build farm:" "${SRCPKG_DIR}"
    SRCPKG_PATH="${BUILD_FARM}/${PKG_MODE}/${SRCPKG_DIR}"
    mv -f "${BUILD_FARM}/${SRCPKG_DIR}" "${SRCPKG_PATH}"
  else
    echo "ERROR: srcpkg (a.k.a. build) directory cannot be found. Abort."
    exit 1
  fi
fi

# deal with raw source package, if it is found; if not, ignore it silently
if [ -f "${SRCPKG_PATH}".tar.bz2 ]; then
  printf "removing raw source package ('%s.tar.bz2')..." "${SRCPKG_DIR}"
  rm -f "${SRCPKG_PATH}".tar.bz2 2>/dev/null && echo " ok"
elif [ -f "${BUILD_FARM}/${SRCPKG_DIR}".tar.bz2 ]; then
  printf "removing raw source package from top level of build farm ('%s.tar.bz2')..." "${SRCPKG_DIR}"
  rm -f "${BUILD_FARM}/${SRCPKG_DIR}".tar.bz2 2>/dev/null && echo " ok"
fi

# patch it
printf "patching..."

# The name of source package must follow this style:
#     ${package_title}-v${package_version}[-${additional_version_info}].srcpkg.${archive_suffix}
NEW_SRCPKG_DIR=`echo "${SRCPKG_DIR}" | sed -e "s/${PKG_TITLE}-${PKG_TITLE}-/${PKG_TITLE}-v/" -e 's/-vv/-v/'`
NEW_SRCPKG_DIR=`echo "${NEW_SRCPKG_DIR}" | sed -e "s/${PKG_TITLE}-/${PKG_TITLE}-v/" -e 's/-vv/-v/'`

cd "${SRCPKG_PATH}"
if [ -f "${PKGBS_DIR}/${PKG_MODE}"/PATCHCMDS/patchcmds-"${PKG_TITLE}" ]; then
  source "${PKGBS_DIR}/${PKG_MODE}"/PATCHCMDS/patchcmds-"${PKG_TITLE}"
  echo " ok"
else
  if [ ! -z "${NEW_SRCPKG_DIR}" ] && [ "${SRCPKG_DIR}" != "${NEW_SRCPKG_DIR}" ]; then
    echo " ok (default patch)"
  else
    # but we will re-compress it anyway
    echo " nothing to patch"
    NEW_SRCPKG_DIR=""
  fi
fi

# check if patch requested change of srcpkg directory name
if [ ! -z "${NEW_SRCPKG_DIR}" ] && [ "${SRCPKG_DIR}" != "${NEW_SRCPKG_DIR}" ]; then
  if [ -z `echo "${NEW_SRCPKG_DIR}" | grep ".srcpkg"` ]; then
    NEW_SRCPKG_DIR="${NEW_SRCPKG_DIR}".srcpkg
  fi
  if [ -d "${BUILD_FARM}/${PKG_MODE}/${NEW_SRCPKG_DIR}" ]; then
    echo -n "removing previous target srcpkg directory ('${BUILD_FARM}/${PKG_MODE}/${NEW_SRCPKG_DIR}')..."
    rm -rf "${BUILD_FARM}/${PKG_MODE}/${NEW_SRCPKG_DIR}"
    echo " ok"
  fi
  mv -f "${BUILD_FARM}/${PKG_MODE}/${SRCPKG_DIR}" "${BUILD_FARM}/${PKG_MODE}/${NEW_SRCPKG_DIR}" && echo \
    "srcpkg directory renamed:" \'"${SRCPKG_DIR}"\' "-->" \'"${NEW_SRCPKG_DIR}"\'
  SRCPKG_DIR="${NEW_SRCPKG_DIR}"
  SRCPKG_PATH="${BUILD_FARM}/${PKG_MODE}/${SRCPKG_DIR}"
fi
NEW_SRCPKG_DIR=""

cd "${BUILD_FARM}/${PKG_MODE}"

# create patched srcpkg
echo -n "compressing '${SRCPKG_DIR}'..."
tar cjf "${SRCPKG_DIR}".tar.bz2 "${SRCPKG_DIR}" 1>/dev/null 2>&1
if [ $? -eq 0 ]; then
  echo " ok"
  printf "created source package:"
  mv -f "${SRCPKG_PATH}".tar.bz2 "${SRCPKG_STORE}/${SRCPKG_DIR}".tar.bz2
  echo " ${SRCPKG_STORE}/${SRCPKG_DIR}.tar.bz2"
else
  echo " fail!"
  echo "ERROR: srcpkg cannot be created; try to remove existing '${SRCPKG_DIR}.tar.bz2'. Abort."
  exit 1
fi

exit 0
Unless otherwise stated, the content of this page is licensed under GNU Free Documentation License.