#!/bin/sh

##
# Copyright (c) 2005-2007 Apple Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# DRI: Wilfredo Sanchez, wsanchez@apple.com
##

##
# Submit project to B&I (Apple internal build)
##

set -e
set -u

version="11";

 wd="$(cd "$(dirname "$0")" && pwd)";
src="$(cd "${wd}/.." && pwd)";

##
# Command line
##

build=false;

submission_enabled=true;

usage ()
{
  program="$(basename "$0")";

  if [ "${1-}" != "-" ]; then echo "$@"; echo; fi;

  echo "Usage: ${program} release";
  echo "       ${program} -b";
  echo "";
  echo "Options:";
  echo "	-b Run buildit";

  if [ "${1-}" == "-" ]; then return 0; fi;
  exit 64;
}

while getopts 'hb' option; do
  case "$option" in
    '?') usage; ;;
    'h') usage -; exit 0; ;;
    'b') build=true; ;;
  esac;
done;
shift $((${OPTIND} - 1));

if ! "${build}"; then
    if [ $# == 0 ]; then usage "No release specified"; fi;
    release="$1"; shift;

    if ! "${submission_enabled}"; then
        echo "Submissions from this branch are not enabled.";
        exit 1;
    fi;
fi;

if [ $# != 0 ]; then usage "Unrecognized arguments:" "$@"; fi;

 project="CalendarServer";
     uri="$(svn info "${src}" --xml | sed -n 's|^.*<url>\(.*\)</url>.*$|\1|p')";
revision="$(svnversion "${src}")";

##
# Do the Right Thing
##

if "${build}"; then
  project_version="${project}-$(echo ${revision} | sed -e 's/:/_/g')";
else 
#
# We need a single revision number
#
  if [ -n "$(echo "${revision}" | sed 's|[0-9M]||g')" ]; then
    echo "Working copy has multiple versions of files: ${revision}.  Aborting.";
    exit 1;
  else
    revision="$(echo "${revision}" | sed 's|M$||g')";
  fi;

  project_version="${project}-$((${version} + ${revision} / 10000 % 100)).$((${revision} / 100 % 100)).$((${revision} % 100))";

#
# Make sure changes are checked in.
#
  if [ "$(svn st "${src}" | grep -v support/submit)" != "" ]; then
    echo "Working copy has uncommitted changes.  Aborting.";
    exit 1;
  fi;
fi;
#
# Do submission
#

tmp="$(mktemp -d -t CalendarServer_build)";
wc="${tmp}/${project_version}";

if "${build}"; then
  echo "";
  echo "Copying ${src}...";
  ignores="$(mktemp -t CalendarServer_ignores)";
  svn st --no-ignore | sed -n -e 's|^I......||p' > "${ignores}";
  rsync -av --exclude=".svn" --exclude-from="${ignores}" "${src}/" "${wc}";
  rm "${ignores}";
else
  echo "";
  echo "Exporting ${uri}@${revision}..."
  svn export -r "${revision}" "${uri}@${revision}" "${wc}";
fi;

echo ""
echo "Tweaking for B&I...";
ln -s support/Makefile.Apple "${wc}/Makefile";

version_file="${wc}/SubmissionInfo.xml";
cat - >> "${version_file}" <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<submission>
 <project>${project}</project>
 <version>${version}</version>
 <source>
  <svn>
   <uri>${uri}</uri>
   <revision>${revision}</revision>
   <date>$(date -u)</date>
  </svn>
 </source>
</submission>
EOF

echo "";
echo "Preparing sources for ${project_version}...";
make -C "${wc}" prep;

if "${build}"; then
    echo "";
    echo "Building ${project_version}...";
    sudo ~rc/bin/buildit "${wc}" -arch ppc -arch i386;
else
    echo "";
    echo "Submitting sources for ${project_version}...";
    ~rc/bin/submitproject "${wc}" "${release}";
fi;


rm -rf "${tmp}";
