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

# ----------------------------
# vcsdir_to_builddir.sh
# version 1.5.2.8
# ----------------------------

FORCE_OPT=""
SRCPKG_OPT=""
QUIET_OPT=""
for arg in $@
do
  [ $arg = "--force" ] && FORCE_OPT="yes"
  [ $arg = "--srcpkg" ] && SRCPKG_OPT="yes"
  [ $arg = "--quiet" ] && QUIET_OPT="yes"
done

# check for git and hg binaries
git_binary_found="yes"
hg_binary_found="yes"
which git >/dev/null 2>&1 || type git >/dev/null 2>&1 || git_binary_found="no"
which hg >/dev/null 2>&1 || type hg >/dev/null 2>&1 || hg_binary_found="no"

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

WDIR_BASENAME="$(basename `pwd`)"
VCS_TYPE=""
if [ -d .git ] && [ ${git_binary_found} = "yes" ]; then
  VCS_TYPE="git"
elif [ -d .hg ] && [ ${hg_binary_found} = "yes" ]; then
  VCS_TYPE="mercurial"
else
  [ -z ${QUIET_OPT} ] && echo "`pwd`: unknown/unsupported version control system"
  exit 1
fi

if [ ${VCS_TYPE} = "git" ]
then
  BRANCH=`git name-rev HEAD | cut -d ' ' -f2 | tr '/' '-'`
  [ -z ${QUIET_OPT} ] && echo "  git directory:" "${WDIR_BASENAME}"
  [ -z ${QUIET_OPT} ] && echo "         branch:" "${BRANCH}"

  BDIR="${WDIR_BASENAME}-${BRANCH}-build"
  if [ ! -z ${SRCPKG_OPT} ]; then
    WDIR_BASENAME=`echo ${WDIR_BASENAME} | sed 's/-git//'`
    DESC_VERSION=`git describe 2>/dev/null | sed 's|/|-|'`
    [ -z "${DESC_VERSION}" ] && DESC_VERSION=`git describe --tags 2>/dev/null`
    [ -z "${DESC_VERSION}" ] && DESC_VERSION=`git describe --always 2>/dev/null`
    BDIR="${WDIR_BASENAME}-${DESC_VERSION}.srcpkg"
  fi

  if [ -d ../"${BDIR}" ]
  then
    [ -z ${QUIET_OPT} ] && echo -n "build directory already exists"
    if [ ! -z ${FORCE_OPT} ]; then
      [ -z ${QUIET_OPT} ] && echo ", removing it"
      rm -rf ../"${BDIR}"
    else
      [ -z ${QUIET_OPT} ] && echo "! abort"
      exit 1
    fi
  fi

  mkdir -p ../"${BDIR}"
  [ -z ${SRCPKG_OPT} ] && printf "%s %s" "build directory:" "${BDIR}"
  [ -z ${SRCPKG_OPT} ] || printf "%s %s" "srcpkg directory:" "${BDIR}"
  if [ -d ../"${BDIR}" ]; then
    echo ""
    git archive HEAD | tar xC ../"${BDIR}" 1>/dev/null 2>&1
  else
    echo " (can't create directory!)"
    exit 1
  fi

  if [ ! -z ${SRCPKG_OPT} ]
  then
    cd ..
    echo -n "compressing '${BDIR}'..."
    tar cjf "${BDIR}".tar.bz2 "${BDIR}" 1>/dev/null 2>&1
    if [ $? -eq 0 ]; then
      echo " ok"
      echo "(raw) source package:" "${BDIR}".tar.bz2
    else
      echo " fail!"
    fi
  fi
elif [ ${VCS_TYPE} = "mercurial" ]
then
  BRANCH=`hg branch`
  [ -z ${QUIET_OPT} ] && echo "   hg directory:" "${WDIR_BASENAME}"
  [ -z ${QUIET_OPT} ] && echo "         branch:" "${BRANCH}"

  BDIR="${WDIR_BASENAME}-${BRANCH}-build"
  if [ ! -z ${SRCPKG_OPT} ]
  then
    WDIR_BASENAME=`echo ${WDIR_BASENAME} | sed 's/-hg//' | sed 's/-mercurial//'`
    ##version_from_hg=`hg summary 2>/dev/null | head -1 | awk '{print $2}' | sed 's/:/-/'`
    version_from_hg=`hg parent --template '{latesttag}-{latesttagdistance}-{node|short}\n'`
    [ -z "${version_from_hg}" ] && version_from_hg=`date +"%Y%m%d"`
    BDIR="${WDIR_BASENAME}-${version_from_hg}.srcpkg"
  fi

  if [ -d ../"${BDIR}" ]
  then
    [ -z ${QUIET_OPT} ] && echo -n "build directory already exists"
    if [ ! -z ${FORCE_OPT} ]
    then
      [ -z ${QUIET_OPT} ] && echo ", removing it"
      rm -rf ../"${BDIR}"
    else
      [ -z ${QUIET_OPT} ] && echo "! abort"
      exit 1
    fi
  fi

  mkdir -p ../"${BDIR}"
  echo -n "build directory:" "${BDIR}"
  if [ -d ../"${BDIR}" ]
  then
    echo ""
    hg archive ../"${BDIR}"
  else
    echo " (can't create build directory!)"
    exit 1
  fi

  if [ ! -z ${SRCPKG_OPT} ]
  then
    cd ..
    echo -n "compressing '${BDIR}'..."
    tar cjf "${BDIR}".tar.bz2 "${BDIR}" 1>/dev/null 2>&1
    if [ $? -eq 0 ]; then
      echo " ok"
      echo "(raw) source package:" "${BDIR}".tar.bz2
    else
      echo " fail!"
    fi
  fi  
fi
Unless otherwise stated, the content of this page is licensed under GNU Free Documentation License.