#!/bin/sh -x
#
#  Authors:
#    Christian Schulte <schulte@ps.uni-sb.de>
#
#  Contributors:
#    Konstantin Popov <kost@sics.se>
#
#  Copyright:
#    Christian Schulte, 1998
#    Konstantin Popov, 2001
#
#  Last change:
#    $Date: 2001/11/30 23:49:33 $ by $Author: duchier $
#    $Revision: 1.33.2.4 $
# 
#  This file is part of Mozart, an implementation 
#  of Oz 3:
#     http://www.mozart-oz.org
# 
#  See the file "LICENSE" or
#     http://www.mozart-oz.org/LICENSE.html
#  for information on usage and redistribution 
#  of this file, and for a DISCLAIMER OF ALL 
#  WARRANTIES.
#

RPMROOT=/usr/src/redhat
RPMSRC=$RPMROOT/SOURCES
RPMSPEC=$RPMROOT/SPECS
TMPDIR=/tmp
: ${PACKAGER:="Christian Schulte <schulte@ps.uni-sb.de>"}

#
# Read dir
#
if test $# -lt 2
then
   echo "usage: create-rpm <build-dir> <dst-dir>" 1>&2;
   echo <build-dir> is the place where Mozart has been built;
   exit 1;
fi
BUILDDIR=$1
BUSRCTAR=`ls ${BUILDDIR}/mozart-*.*-src.tar.gz`
BUDOCTAR=`ls ${BUILDDIR}/mozart-*.*-doc.tar.gz`
BUSTDTAR=`ls ${BUILDDIR}/mozart-*.*-std.tar.gz`
DSTDIR=$2
OZHOME=/usr/lib/mozart

#
# Find version
#

VERSION=`(echo ${BUSRCTAR} | sed 's/.*mozart-\(.*\..*\)-src.tar.gz/\1/')`
BUILD=`(echo $Revision: 1.33.2.4 $ | sed 's|.*\.\([1-9][0-9]*\).*|\1|')`

#
# Build sh files
#

cd $TMPDIR

cat > oz.sh <<EOF
export PATH=${OZHOME}/bin:\${PATH}
EOF

cat > oz.csh <<EOF
setenv PATH ${OZHOME}/bin:\${PATH}
EOF

chmod a+x oz.sh oz.csh
tar cf - oz.csh oz.sh | gzip -9 > ${RPMSRC}/mozart-profiles.tar.gz
rm -f oz.sh oz.csh


#
# Build xpm
#

cat > ${RPMSRC}/mozart.xpm <<EOF
/* XPM */
static char * mozart_xpm[] = {
"69 30 32 1",
" 	c None",
".	c #FFFFFF",
"+	c #FFE69E",
"@	c #FBF6E9",
"#	c #F7E7B8",
"\$	c #FAD466",
"%	c #FBCE4A",
"&	c #FFDE7C",
"*	c #FFF3D2",
"=	c #F0D688",
"-	c #E3BA42",
";	c #61501F",
">	c #100F0A",
",	c #000000",
"'	c #392E0F",
")	c #B29030",
"!	c #777777",
"~	c #484848",
"{	c #A7A7A7",
"]	c #282828",
"^	c #D7D7D7",
"/	c #201B08",
"(	c #A18F59",
"_	c #8F8F8F",
":	c #896F25",
"<	c #725C1E",
"[	c #D3AC3E",
"}	c #C0B185",
"|	c #514215",
"1	c #606060",
"2	c #BFBFBF",
"3	c #C2A34C",
"                                #@                                   ",
"                              *\$%&                                   ",
"                            *&&*%%*                                  ",
"                           +\$*  #%\$                                  ",
"                         #\$#    #%%#                                 ",
"                       *\$&      #%%%                                 ",
"                     @=%%\$  #=  +%%%+                                ",
"                    #%%%%%=\$%#  &%%%%*                               ",
"                  #-;>,')%%%%#  \$%%+%&                         !~~   ",
"                *&)>,,,,,;%%%*  %=@ &%*                        ~,,   ",
"              @+%%>,')',,,)%%*  *   #%\$                        ~,,   ",
"~~~{],]_^~,,~@+%%),,)%%/,,/%%     @(>[%#      _],,>~^  !~~!>~_~>,,~~ ",
",,,,,,,,>,,,,~@%%:,,<%%[,,,[%    }',,|%%@    1,,,,,,>^ ~,,,,~!,,,,,, ",
",,,]!>,,,!~,,, #%:,,>%%%',,[%# #;,,,,'%%+   ^,,>{^],,_ ~,,,,~_~,,,~~ ",
",,,2 !,,1 @,,,  \$[,,,;%%:,,[%%3>,,>,,<%%%*  @2!2 ^],,! ~,,>{   ,,,   ",
",,,2 !,,!  ,,,  #%|,,,:[',/%%%%/>)[,,:%%%&   @!],,,,,! ~,,!    ,,,   ",
",,,  !,,!  ,,,   &%/,,,,,,)\$%%%[%%),,)%%%%# @>,,]1],,! ~,,_    ,,,   ",
",,,  !,,!  ,,,   *%%',,,~2   #%%%%:,,[%%:%\$ _,,]@ ~,,! ~,,2    ,,,   ",
",,,  !,,!  ,,,    +%%[[=      *%%%;,,[:>,:%#!,,~ ^>,,! ~,,2    ,,,_2@",
",,,  !,,!  ,,,     %%%%  *&#   #%%',,/,,,:\$*_,,,],,,,! ~,,2    ],,,,2",
",,,  !,,!  ,,,     #%%=  #%%*   %%/,,,,'3#   ~,,,~!,,! ~,,2    {>,,,2",
"                    \$%#  #%%\$   =%/,,/)=@     ^2^                22^ ",
"                    *%&   \$%%#  #%)>:\$*                              ",
"                     &%   *%%+  &%%-#                                ",
"                     *%#   *=*  %%+                                  ",
"                      +%*      =&@                                   ",
"                       %%#   *+*                                     ",
"                       #%%%%%#                                       ",
"                        \$%%+                                         ",
"                        *&*                                          "};
EOF

cp ${BUSRCTAR} ${BUDOCTAR} ${BUSTDTAR} ${RPMSRC}

SRCTAR=mozart-${VERSION}-src.tar.gz
DOCTAR=mozart-${VERSION}-doc.tar.gz
STDTAR=mozart-${VERSION}-std.tar.gz
SPEC=mozart-$VERSION-$BUILD.spec

# Generate redhat spec file
cd $TMPDIR
cat > $RPMSPEC/$SPEC <<EOF
# Spec file for Mozart, an efficient and distributed implementation of Oz
# Author: Christian Schulte, 1998
# Contributor: Konstantin Popov, 2001
# Contributor: Denys Duchier, 2001
# Copyright: Christian Schulte, 1998, 1999
# Copyright: Konstantin Popov, 2001
# Copyright: Denys Duchier, 2001
Name: mozart
Version: $VERSION
Release: $BUILD
Summary: Mozart, an efficient and distributed implementation of Oz
Copyright: Free
Vendor: Mozart Consortium
Url: http://www.mozart-oz.org/
Group: Development/Languages
Packager: $PACKAGER
Icon: mozart.xpm

Source0: ftp://ftp.mozart-oz.org/pub/${VERSION}/tar/${SRCTAR}
Source1: ftp://ftp.mozart-oz.org/pub/${VERSION}/tar/${DOCTAR}
Source2: mozart-profiles.tar.gz
Source3: ftp://ftp.mozart-oz.org/pub/${VERSION}/tar/${STDTAR}

BuildRoot: /var/tmp/mozart-${VERSION}

%description
The Mozart system provides state-of-the-art support in two areas: open
distributed computing and constraint-based inference. Mozart
implements Oz, a concurrent object-oriented language with dataflow
synchronization. Oz combines concurrent and distributed programming
with logical constraint-based inference, making it a unique choice for
developing multi-agent systems. Mozart is an ideal platform for both
general-purpose distributed applications as well as for hard problems
requiring sophisticated optimization and inferencing abilities. We
have developed applications in scheduling and time-tabling, in
placement and configuration, in natural language and knowledge
representation, multi-agent systems and sophisticated collaborative
tools.

Mozart has been jointly developed by the Programming Systems Lab at
DFKI and Universitt des Saarlandes, the Swedish Institute of Computer
Science, and Universit de Louvain.

This package contains all you need to run and develop Oz programs with
Mozart.
%package doc
Summary: Mozart documentation
Group: Development/Languages
requires: mozart
%description doc
This package contains the extensive Mozart online documentation as a
set of HTML pages, postscript and pdf documents. It also contains demo
applications that can directly be started from the pages.
%package contrib
Summary: Mozart contributions
Group: Development/Languages
requires: mozart
%description contrib
This package contains a collection of contributed software packages
for Mozart that might come in handy for applications like a regular
expression package, data base interfaces, and so on.
%package stdlib
Summary: Mozart standard library
Group: Development/Languages
requires: mozart
%description stdlib
This package contains a continually growing collection of libraries
that complement the core Mozart distribution.
%prep
%setup -q
%setup -q -D -T -b 3

%build
: \${CFLAGS="\$RPM_OPT_FLAGS"}
: \${CXXFLAGS="\$RPM_OPT_FLAGS"}
export CFLAGS CXXFLAGS
./configure --prefix=\$RPM_BUILD_ROOT$OZHOME --enable-opt=yes --disable-doc
make

%install
rm -rf \$RPM_BUILD_ROOT
PREFIX=\$RPM_BUILD_ROOT$OZHOME
# config files
mkdir -p \$RPM_BUILD_ROOT/etc/profile.d
(cd \$RPM_BUILD_ROOT/etc/profile.d; \
 gzip -dc \$RPM_SOURCE_DIR/mozart-profiles.tar.gz | tar xf -)
# documentation files
mkdir -p \$PREFIX
(cd \$PREFIX; \
 gzip -dc \$RPM_SOURCE_DIR/$DOCTAR | tar xf -; \
 mv mozart/doc .; \
 mv mozart/print .; \
 rm -rf mozart; \
 rm -rf doc/demo/applets; \
 chown -R root .; \
 chgrp -R root .; \
 chmod -R a+rX,g-w,o-w .)
# Generic install
make install
# Strip binaries and shared objects
find \$PREFIX -type f -name '*.so*' -exec strip '{}' ';'
find \$PREFIX -type f -name '*.exe' -exec strip '{}' ';'
# standard library
(cd \$RPM_BUILD_DIR/\$RPM_PACKAGE_NAME-\$RPM_PACKAGE_VERSION-std; \$PREFIX/bin/ozengine ./ozmake --prefix=\$PREFIX --install --package=mozart-stdlib.pkg --nosavedb)

# Create file lists
PLTFRM=\`\$PREFIX/bin/ozplatform\`
make files.base files.base-\${PLTFRM}
make files.contrib files.contrib-\${PLTFRM}
make files.doc
cat files.base files.base-\${PLTFRM} | \
    sed 's|^./|$OZHOME/|g' > mfiles.base 
cat files.contrib files.contrib-\${PLTFRM} | \
    sed 's|^./|$OZHOME/|g' > mfiles.contrib 
cat files.doc | \
    sed 's|^./|$OZHOME/|g' > mfiles.doc 

%clean
rm -rf \$RPM_BUILD_ROOT
rm -rf \$RPM_BUILD_DIR/\$RPM_PACKAGE_NAME-\$RPM_PACKAGE_VERSION-std
%files -f mfiles.base
%doc README LICENSE LICENSE.html LICENSE.rtf
%config /etc/profile.d/oz.sh
%config /etc/profile.d/oz.csh
%files doc -f mfiles.doc
%files contrib -f mfiles.contrib
EOF

#
# No we have all what we need, go and build rpms
#

rpm --clean -ba ${RPMSPEC}/mozart-$VERSION-$BUILD.spec
rm -f ${RPMSPEC}/${SPEC}
rm -f ${RPMSRC}/${SRCTAR}
rm -f ${RPMSRC}/${DOCTAR}
rm -f ${RPMSRC}/${STDTAR}
rm -f ${RPMSRC}/mozart-profiles.tar.*
rm -f ${RPMSRC}/mozart.xpm

#
# Move the created rpms to destination
#

mv ${RPMROOT}/RPMS/i386/mozart-{,doc-,contrib-}$VERSION-$BUILD.i386.rpm ${DSTDIR}
mv ${RPMROOT}/SRPMS/mozart-$VERSION-$BUILD.src.rpm ${DSTDIR}
