#!/bin/sh
# -*- sh-basic-offset: 2 -*-

##
# Copyright (c) 2005-2015 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.
##

set -e
set -u

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

. "${wd}/bin/_build.sh";

init_build;

export PATH="/Applications/Server.app/Contents/ServerRoot/usr/bin:${PATH}";

requirements="${wd}/requirements-stable.txt";
extra_features="OpenDirectory,Postgres";


#
# Download virtualenv and friends so that B&I has that core toolchain.
#

mkdir -p "${wd}/.develop/tools";

for pkg in             \
    setuptools-17.0    \
    pip-7.0.3          \
    virtualenv-13.0.3  \
; do
       name="${pkg%-*}";
    version="${pkg#*-}";
     first="$(echo "${name}" | sed 's|^\(.\).*$|\1|')";
       url="https://pypi.python.org/packages/source/${first}/${name}/${pkg}.tar.gz";

    ruler "Downloading ${pkg}";

    curl -L -o "${wd}/.develop/tools/${pkg}.tgz" "${url}";
done;


#
# Build cffi because xattr needs it at setup time.
#

if ! find_header ffi.h; then
  c_glue_include="${dev_roots}/c_glue/include";
  mkdir -p "${c_glue_include}";
  echo "#include <ffi/ffi.h>" > "${c_glue_include}/ffi.h"
  export C_INCLUDE_PATH="${c_glue_include}:${C_INCLUDE_PATH:-}";
fi;


#
# Download dependencies
#

ve_tmp="$(mktemp -d -t CalendarServer_ve_tools)";

# Bootstrap virtualenv and friends so we can use them in this script (not sent to B&I).
py_ve_tools="${ve_tmp}/ve_tools";
export PYTHONPATH="${py_ve_tools}/lib:${wd}:${PYTHONPATH:-}";
bootstrap_virtualenv;

reqs_to_use="${ve_tmp}/requirements.txt";

sed -e '/python-ldap/d' < "${requirements}" > "${reqs_to_use}";
echo "xattr==0.7.5" >> "${reqs_to_use}";

ruler "Downloading Python requirements for .[${extra_features}]";
echo "";
pip_download                       \
  --allow-unverified cx-Oracle     \
  --no-deps                        \
  --requirement="${reqs_to_use}"  \
  ;

rm -rf "${ve_tmp}";


#
# Check out CalDAVTester
#

url="$(grep egg=CalDAVTester "${wd}/requirements-dev.txt" | sed 's|^.*svn+\([^@#]*\).*$|\1|')";
rev="$(grep egg=CalDAVTester "${wd}/requirements-dev.txt" | sed 's|^.*svn+[^@#]*@\([0-9]*\).*$|\1|')";

svn export -r "${rev}" "${url}@${rev}" "${wd}/CalDAVTester";
tar -C "${wd}" -cvzf "${wd}/CalDAVTester.tgz" CalDAVTester;
rm -r CalDAVTester;



#
# Remove .exe files from arvhives
#

for archive in $(find "${wd}/.develop" -type f -name '*.tgz' -or -name '*.tar.gz'); do
  if tar -tvzf "${archive}" "*.exe" > /dev/null 2>&1; then
    ruler "Removing binaries from ${archive}";
    tmp="$(mktemp -t ccsXXXXX)";
    gzcat "${archive}" | gnutar --delete --wildcards -vf - "*.exe" > "${tmp}";
    gzip -c "${tmp}" > "${archive}";
    rm "${tmp}";
  fi;
done;
