#!/bin/bash
#
# make-release-tarballs
# create release tarballs for CafeOBJ
#
# Copyright (c) 2014-2015, Norbert Preining. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#   * Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#
#   * 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 AUTHOR 'AS IS' AND ANY EXPRESSED
# 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 AUTHOR 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.
#

version=$1
vminor=$2
shift
shift
# remaining components are the lisp interpreters for which we build

default_components="bin share"

if [ x$vminor = x ] ; then
  echo "Usage: make-dist-tarball VERSION MINOR <lisp-interpreters>" >&2
  exit
fi


if [ ! -d dist/lib/cafeobj-$version ] ; then
  echo "Did you configure --enable-distribution && make && make install?" >&2
  exit 1
fi

if ! [ -d dist/bin -o -d dist/share ] ; then
  echo "Did you configure --enable-distribution && make && make install?" >&2
  exit 1
fi

for i in $* ; do
  case $i in
  acl-standalone|sbcl) true ;;
  *) echo "Unsupported standalone lisp interpreter: $i" >&2 ; exit ;;
  esac
done

cd dist

for i in $* ; do
  distdir=lib/cafeobj-$version/$i
  case $i in
  acl-standalone) sname=acl ; dumpfile=$distdir/CafeOBJ ;;
  sbcl)           sname=sbcl; dumpfile=$distdir/cafeobj.sbcl ;;
  esac
  if ! [ -d $distdir ] ; then
    echo "Cannot find dump for $i, exiting." >&2
    exit 1
  fi
  case `file $dumpfile` in
  *32-bit*) arch=x32 ;;
  *64-bit*) arch=x64 ;;
  *) echo "Cannot determine arch of CafeOBJ dump!" >&2 ; exit 1 ;;
  esac
  #
  # we have to make sure that the current engine is the default one
  # selected in the wrapper script!
  sed --in-place -e "s/^engine=.*/engine=$i/" bin/cafeobj
  echo "Building $sname standalone for $arch`uname` ..."
  tar --owner=0 --group=0 -cvzf ../cafeobj-$version$vminor-${sname}-${arch}`uname`.tar.gz	\
  	$default_components $distdir
done

cd ..

