#! /bin/sh
#
# RTEMS Tools Project (http://www.rtems.org/)
# Copyright 2015,2016,2019 Chris Johns (chrisj@rtems.org)
# All rights reserved.
#
# This file is part of the RTEMS Tools package in 'rtems-tools'.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

set -e

#
# This script packages a package in a tar file.
#

#
# Defaults.
#
. ${release_top}/rtems-release-defaults

#
# Common package start.
#
title="RTEMS Release Package"
. ./rtems-release-package-start

#
# Work in a package specific directory in the release directory.
#
# Clone the repo and then check if there are any submodules.
#
echo "git clone ${git_remote} ${git_local}"
git clone ${git_remote} ${git_local}

stamped_tar=${prefix}-unstamped

#
# If there are submodules, exclude the ones we do not wish to package, eg the
# whole of the FreeBSD source tree. For the ones to package get the commit
# (treeish) for the branch we releasing and then create an archive. The
# submodule archives are merged into the main archive once we have collected
# them all.
#
# A package may have one or more branches that need to be package.
# If not defined there is only one the version based branch
#
# Get the package's branches and package names
#
cd ${git_local}
 var_pkg_main=${package_prefix}_pkg_main
 eval "pkg_main=\${$var_pkg_main}"
 # If there is no package main use main
 if [ -z "${pkg_main}" ]; then
   pkg_main="main"
 fi
 var_pkg_branches=${package_prefix}_pkg_branches
 eval "pkg_branches=\${$var_pkg_branches}"
 var_pkg_names=${package_prefix}_pkg_names
 eval "pkg_names=\${$var_pkg_names}"
 # If there are no package branches use the default mapping
 if [ -z "${pkg_branches}" ]; then
   # See rtems-release-package-start for the map branch macro
   pkg_branches=$(rtems_map_branch ${package} ${version})
   pkg_names=""
 fi
 echo "git fetch origin"
 git fetch origin
 cd ..   # ${git_local}

echo "] Package ${package} ${version}: remote-branches='${pkg_branches}' pkg-names='${pkg_names}'"
for remote_branch in ${pkg_branches}
do
cd ${git_local}
  if [ -n "${pkg_names}" ]; then
   pkg_name=$(echo ${pkg_names} | cut -f1 -d ' ')
   if [ "${pkg_name}" = "none" ]; then
    pkg_name=""
   else
    pkg_name="-${pkg_name}"
   fi
   pkg_names=$(echo ${pkg_names} | cut -f2- -d ' ')
   pkg_prefix=${package}${pkg_name}-${release}
  else
   pkg_name=""
   pkg_prefix=${prefix}
  fi
  echo "] Package ${package} ${version}: remote-branch=${remote_branch} pkg-name=${pkg_name}"
  git_submodules=$(git submodule | awk '{print $2}')
  if [ -n "${git_submodules}" ]; then
   echo "] git submodules found ...."
   echo "git submodules init"
   git submodule init
   echo "git checkout ${remote_branch}"
   git checkout ${remote_branch}
   for s in ${git_submodules}
    do
    ok=$(echo ${git_submodules_excludes} | sed -e "s/.*${s}.*/no/g")
    if [ "${ok}" != "no" ]; then
     echo "git submodule update ${s}"
     git submodule update ${s}
     treeish=$(git ls-tree HEAD ${s} | awk '{print $3}')
     cd ${s}
      echo "git archive --format=tar --prefix=${pkg_prefix}/${s}/ ${treeish}"
      git archive --format=tar --prefix=${pkg_prefix}/${s}/ ${treeish} > \
                                          ../../${stamped_tar}-${s}.tar
      cd ..   # ${s}
    else
     echo "git submodule ${s} excluded"
    fi
   done
   echo "git checkout ${pkg_main}"
   git checkout ${pkg_main}
  fi
  echo "git archive --format=tar --prefix=${pkg_prefix}/ ${remote_branch}"
  git archive --format=tar --prefix=${pkg_prefix}/ ${remote_branch} > \
                                          ../${stamped_tar}.tar
  cd ..   # ${git_local}

 if [ ! -f ${stamped_tar}.tar ]; then
  echo "error: git archive failed, no tar file"
  exit 1
 fi
 echo "tar xf ${stamped_tar}.tar"
 tar xf ${stamped_tar}.tar
 if [ -n "${git_submodules}" ]; then
  for s in ${git_submodules}
  do
    ok=$(echo ${git_submodules_excludes} | sed -e "s/.*${s}.*/no/g")
    if [ "${ok}" != "no" ]; then
     echo "tar xf ${stamped_tar}-${s}.tar"
     tar xf ${stamped_tar}-${s}.tar
    fi
  done
 fi

 cd ${pkg_prefix}
  echo "] Creating VERSION: ${release}"
  echo "[version]" > VERSION
  echo "revision = ${release}" >> VERSION
  cd .. # ${pkg_prefix}

 #
 # Run the worker script if provided. It can perform any package
 # specific set up functions. This is done before we finally
 # package the release.
 #
 if [ -n "${worker}" ]; then
  wk_name=$(basename ${worker})
  echo "] Worker: ${wk_name}"
  ${worker} ${package} ${version} ${revision} ${release_url} ${top}
 fi

 #
 # The '..' is the release directory.
 #
 echo "tar cf - ${pkg_prefix}"
 tar cf - ${pkg_prefix} | ${comp} > ../${pkg_prefix}.tar.${comp_ext}

 echo "] Created: ${release}/${pkg_prefix}.tar.${comp_ext}"

 #
 # Update the global exported variables
 #
 echo "# Package ${package} ${version}: remote-branches=\"${pkg_branches}\" pkg-names=\"${pkg_names}\"" >> ${pkg_list}
 echo "rtems_packages=\"\${rtems_packages} ${pkg_prefix}\"" >> ${pkg_list}
 if [ ${add_rsb_hash} = yes ]; then
  echo "rtems_rsb_hash_pkgs=\"\${rtems_rsb_hash_pkgs} ${pkg_prefix}\"" >> ${pkg_list}
 fi
done

#
# Comman package end.
#
. ${top}/rtems-release-package-end

exit 0
