#!/bin/bash
# Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
# Copyright [2016-2022] EMBL-European Bioinformatics Institute
# 
# 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.

API_VERSION=$1
home=`dirname $0`

if [ "$(uname)" == "Darwin" ]; then
  tmpdir=$(mktemp -d -t ensembl-api)
else
  tmpdir=$(mktemp -d)
fi

if [ -n "$API_VERSION" ]; then
  branch='--branch release/$API_VERSION'
else
  branch=''
fi

if [ -f ensembl-api.tar.gz ]; then
  echo 'Removing the tarball already here'
  rm ensembl-api.tar.gz
fi

# Run the clone
echo 'Starting clone'
${home}/../bin/git-ensembl --clone --depth 1 $branch --dir $tmpdir api tools

pushd $tmpdir >& /dev/null

# Cleanup
echo 'Cleaning up clones'
for d in ensembl ensembl-compara ensembl-variation ensembl-funcgen ensembl-io ensembl-tools; do
  rm -rf $d/modules/t
  rm -rf $d/.git
done

# Tarball
echo 'Generating tarball'
tar zcf ensembl-api.tar.gz ensembl*

# Cleanup
popd >& /dev/null
mv $tmpdir/ensembl-api.tar.gz $PWD/.
rm -rf $tmpdir

# Done
echo 'Finished generating Ensembl API tarball'
