#!/bin/sh
#
# Extracts the given architecture directory from its patch file
#
# Usage: extract_arch <arch>
#
if [ $# != 2 ]; then
  echo "usage: $0 <reference arch> <arch to patch>"
  exit 1
fi

PATCH_ARCH=$1
ARCH=$2
PATCH_DIR=`dirname $0`
cd $PATCH_DIR
# if the directory is already present (whether or not there is a patch),
#   we assume it is up-to-date.
if [ ! -d "${ARCH}" ]; then
  if [ ! -f "${ARCH}.patch" ]; then
    echo "patch file ${ARCH}.patch does not exist"
    exit 1
  fi

  echo EXTRACTING ${ARCH}
  cp -r ${PATCH_ARCH} ${ARCH}
  cd ${ARCH}
  patch -p1 < ../${ARCH}.patch
  if [ $? != 0 ]; then
    echo "Patch failed."
    \rm -rf ${ARCH}
    exit 1
  fi
fi
