#!/bin/sh
# Heavily based on the Slackware 12.1 SlackBuild
# Home: http://freenx.berlios.de http://www.nomachine.com
# Packager: Davide Locatelli ( l.davide AT tiscali.it )

CWD=`pwd`
if [ "$TMP" = "" ]; then
 TMP=/tmp/tgz
fi

PKG=$TMP/package-freenx-server
NAME=freenx-server
VERSION=0.7.3
TARGET=i486
ARCH=${ARCH:-i486}
BUILD=1dav

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
elif [ "$ARCH" = "athlonxp" ]; then
  SLKCFLAGS="-O2 -march=athlon-xp -mtune=athlon-xp"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

if [ ! -d $TMP ]; then
 mkdir -p $TMP
fi
if [ ! -d $PKG ]; then
 mkdir -p $PKG
fi

cd $TMP
tar xvzf $CWD/nx-X11-3.2.0-2.tar.gz
tar xvzf $CWD/nxagent-3.2.0-10.tar.gz
tar xvzf $CWD/nxauth-3.2.0-1.tar.gz
tar xvzf $CWD/nxcomp-3.2.0-7.tar.gz
tar xvzf $CWD/nxcompext-3.2.0-1.tar.gz
tar xvzf $CWD/nxcompsh-3.2.0-1.tar.gz
tar xvzf $CWD/nxcompshad-3.2.0-3.tar.gz
tar xvzf $CWD/nxkill-3.2.0-1.tar.gz
tar xvzf $CWD/nxproxy-3.2.0-1.tar.gz

(
cd nxcomp
./configure
make
)

(   
cd nxcompext
./configure 
make
)   

(   
cd nxcompsh
./configure
make
)   

(   
cd nxcompshad
./configure  
make
)   

(   
cd nxkill
./configure
make
)   

(   
cd nxproxy
./configure
make
)   

(   
cd nx-X11
make World
)

mkdir -p $PKG/usr/bin
mkdir -p $PKG/usr/NX/{bin,etc,lib}

cp -a nx-X11/lib/X11/libX11.so* $PKG/usr/NX/lib
cp -a nx-X11/lib/Xext/libXext.so* $PKG/usr/NX/lib
cp -a nx-X11/lib/Xrender/libXrender.so* $PKG/usr/NX/lib
cp -a nxcomp/libXcomp.so* $PKG/usr/NX/lib
cp -a nxcompext/libXcompext.so* $PKG/usr/NX/lib
cp -a nxcompshad/libXcompshad.so* $PKG/usr/NX/lib
# this one is for a problem with symbolic link on libXrender
(
cd $PKG/usr/NX/lib
ln -sf libXrender.so.1.2.2 libXrender.so.1.2
)

cp -a nxkill/nxkill $PKG/usr/NX/bin

# binaries
cp -a nx-X11/programs/Xserver/nxagent $PKG/usr/NX/bin
cp -a nxproxy/nxproxy $PKG/usr/NX/bin

tar xzvf $CWD/$NAME-$VERSION.tar.gz
cd $NAME-$VERSION
chown -R root.root .

patch -p0 < gentoo-nomachine.diff

(
cd nxserver-helper
make
)   

install -m755 nxcups-gethost nxdesktop_helper nxdialog nxkeygen \
              nxloadconfig nxnode nxnode-login nxprint nxserver \
              nxserver-helper/nxserver-helper nxsetup nxviewer_helper \
              $PKG/usr/NX/bin

install -m644 node.conf.sample $PKG/usr/NX/etc/node.conf.new

mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a AUTHORS CONTRIB COPYING ChangeLog INSTALL \
	$PKG/usr/doc/$NAME-$VERSION

( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)

( cd $PKG/usr/man
  find . -name "*.?" | xargs gzip -9
)

chown -R root.bin $PKG/usr/bin
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/slack-desc > $PKG/usr/doc/$NAME-$VERSION/slack-desc
cat $CWD/$NAME.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/$NAME.SlackBuild
cd $PKG
ADD="xdialog >= 2.3.1-i486-1dav" \
requiredbuilder -v -y -c -s $CWD $PKG
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz

if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP
fi
