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

##
# Copyright (c) 2005-2017 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-osx.txt";
extra_features="opendirectory,postgres";


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

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

for pkg in             \
    setuptools==18.5    \
    pip==8.1.2          \
    virtualenv==15.0.2  \
; do
    ruler "Installing ${pkg}";
    pip download -d "${wd}/.develop/tools" "${pkg}";
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/python/site-packages:${wd}:${PYTHONPATH:-}";
bootstrap_virtualenv;

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

# Copy over each requirements file we need and edit a few to remove stuff we don't need
sed -e '/requirements-dev.txt/d' < "${wd}/requirements-osx.txt" > "${ve_tmp}/requirements-osx.txt";
sed -e '/python-ldap/d' < "${wd}/requirements-cs.txt" > "${ve_tmp}/requirements-cs.txt";
echo "xattr==0.7.5" >> "${ve_tmp}/requirements-cs.txt";
cp "${wd}/requirements-twisted-osx.txt" "${ve_tmp}/";

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

rm -rf "${ve_tmp}";


#
# Check out CalDAVTester
#

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

git clone "${url}" "${wd}/CalDAVTester";
rm -rf "${wd}/CalDAVTester/.git";
# FIXME: checkout rev
tar -C "${wd}" -cvzf "${wd}/CalDAVTester.tgz" CalDAVTester;
rm -r CalDAVTester;



#
# Remove .exe files from archives
#

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;



#
# Remove .eggs files from archives
#

for archive in $(find "${wd}/.develop" -type f -name '*.zip'); do
  if unzip -qql "${archive}" "*/.eggs/" > /dev/null 2>&1; then
    ruler "Removing eggs from ${archive}";
    zip -qd "${archive}" "*/.eggs/*";
  fi;
done;



#
# Remove src
#
ruler "Removing source";
rm -rf "${wd}/src"

ruler "";
