#!/bin/sh
#
# Author: Jan Wielemaker (jan@swi.psy.uva.nl)
# Copyright 1994, University of Amsterdam
#
# Copies library files, binaries and other related files to public area
# and then runs ./install to create the xpce-$version executable.
#
# usage:
#	
#   ./install-bins		equivalent to ./install-bins /usr/local
#   ./install-bins area
#	Creates <area>/lib/xpce-$version to hold the libraries
#	and     <area>/bin/xpce-$version for the executable
#	and	<area>/bin/xpce-client for the socket connection program
#

# setup variables

prefix="/usr/local"
TAR_CP_FLAGS="ch"
INSTALL="install -c"
INSTALL_PROGRAM="$INSTALL"
INSTALL_DATA="$INSTALL -m 644"
RTSUFFIX=""

if [ "$1" != "" ]; then prefix="$1"; fi

version=`cat VERSION`
libdir=$prefix/lib/xpce-$version
bin=$prefix/bin/xpce$RTSUFFIX-$version
client=$prefix/bin/xpce-client

# link SWI-Prolog into this area

for d in library boot include; do
    if [ ! -d pl/$d ]; then (cd pl; ln -s ../../pl/$d .); fi
done

if [ ! -r bin/xpce$RTSUFFIX ]; then
    (cd bin; ln -s ../pl/src/xpce$RTSUFFIX .);
fi

# collect necessary files and directories

echo ""
echo "Installing xpce-$version libraries in $libdir"
echo ""

filesa="bin/xpce$RTSUFFIX bin/xpce-client src/XPCE.a pl/src/pl-crt0.o"
filesb="INSTALL install README* INFO include pl/load.pl pl/xpce.pl"
filesc="CXX/demo/Makefile CXX/demo/README CXX/demo/*.C CXX/demo/*.bm"
filesd="STAMP VERSION ChangeLog licence/licence.tex"
filese="src/unx/client.c src/h/interface.h"
filesf="Pce bitmaps postscript man/reference man/faq"
filesg="prolog/boot prolog/lib pl/library pl/boot pl/include"
filesh="prolog/demo prolog/contrib"
allfiles="$filesa $filesb $filesc $filesd $filese $filesf $filesg $filesh"

# Remove possible existing directory

if [ -d $libdir ]; then
    echo -n "Directory $libdir already exists.  Remove (y/n)? "
    read answer
    if [ "$answer" = "y" ]; then
	echo -n "Running rm -rf $libdir ... "
        rm -rf $libdir;
	echo "done."
    fi
fi

# Create new one 

if [ ! -d $libdir ]; then mkdir $libdir; fi

echo -n "Copying files to $libdir ... "
${TAR-tar} ${TAR_CP_FLAGS}f - $allfiles | (cd $libdir; tar xf -)
#cp -pr $allfiles $libdir
echo "done."
echo -n "Cleaning $libdir ... "
(cd $libdir; rm -rf `find . \( -name CVS -o -name '*~' \) -print`)
echo "done."
if [ `whoami` = "root" ]; then
    echo -n "Changing owner to root.bin in $libdir ... "
    (cd $libdir; chown -R root.bin .)
    echo "done."
fi
echo -n "Fixing permissions ... "
echo -n "(go-w) ... "
(cd $libdir; chmod -R go-w .)
echo -n "(a+r) ... "
(cd $libdir; chmod -R a+r .)
echo "done."

echo "Starting $libdir/install $bin"
(cd $libdir; sh install $bin)
if [ ! -x $client ]; then
    echo -n "Installing $client ... "
    $INSTALL_PROGRAM bin/xpce-client $prefix/bin
    echo "done."
fi
