#!/bin/sh
# Heavily based on the Slackware 13.0 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/txz
fi

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

if [ ! -f nx-X11-3.4.0-1.tar.gz ];
then
        wget -c http://64.34.161.181/download/3.4.0/sources/nx-X11-3.4.0-1.tar.gz
fi

if [ ! -f nxagent-3.4.0-3.tar.gz ];
then
        wget -c http://64.34.161.181/download/3.4.0/sources/nxagent-3.4.0-3.tar.gz
fi

if [ ! -f nxauth-3.4.0-1.tar.gz ];
then
        wget -c http://64.34.161.181/download/3.4.0/sources/nxauth-3.4.0-1.tar.gz
fi

if [ ! -f nxcomp-3.4.0-1.tar.gz ];
then
        wget -c http://64.34.161.181/download/3.4.0/sources/nxcomp-3.4.0-1.tar.gz
fi

if [ ! -f nxcompext-3.4.0-1.tar.gz ];
then
        wget -c http://64.34.161.181/download/3.4.0/sources/nxcompext-3.4.0-1.tar.gz
fi

if [ ! -f nxcompsh-3.4.0-1.tar.gz ];
then
        wget -c http://64.34.161.181/download/3.4.0/sources/nxcompsh-3.4.0-1.tar.gz
fi

if [ ! -f nxcompshad-3.4.0-1.tar.gz ];
then
        wget -c http://64.34.161.181/download/3.4.0/sources/nxcompshad-3.4.0-1.tar.gz
fi

if [ ! -f nxkill-3.4.0-1.tar.gz ];
then
        wget -c http://64.34.161.181/download/3.4.0/sources/nxkill-3.4.0-1.tar.gz
fi

if [ ! -f nxproxy-3.4.0-2.tar.gz ];
then
        wget -c http://64.34.161.181/download/3.4.0/sources/nxproxy-3.4.0-2.tar.gz
fi

if [ ! -f freenx-server-0.7.3.tar.gz ];
then
        wget -c http://download.berlios.de/freenx/freenx-server-0.7.3.tar.gz
fi

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.4.0-1.tar.gz
tar xvzf $CWD/nxagent-3.4.0-3.tar.gz
tar xvzf $CWD/nxauth-3.4.0-1.tar.gz
tar xvzf $CWD/nxcomp-3.4.0-1.tar.gz
tar xvzf $CWD/nxcompext-3.4.0-1.tar.gz
tar xvzf $CWD/nxcompsh-3.4.0-1.tar.gz
tar xvzf $CWD/nxcompshad-3.4.0-1.tar.gz
tar xvzf $CWD/nxkill-3.4.0-1.tar.gz
tar xvzf $CWD/nxproxy-3.4.0-2.tar.gz

(
cd nxcomp
echo nxcomp
./configure
make
)

(   
cd nxcompext
echo nxcompext
./configure 
make
)   

(   
zcat $CWD/nxcompsh_patch.gz | patch -p0 --backup
cd nxcompsh
echo nxcompsh
./configure
make
)   

(   
cd nxcompshad
echo nxcompshad
./configure  
make
)   

(   
cd nxkill
echo nxkill
./configure
make
)   

(   
cd nxproxy
echo nxproxy
./configure
make
)   

(   
cd nx-X11
echo 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-2dav" \
requiredbuilder -v -y -c -s $CWD $PKG
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.txz

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
  if [ "$TMP" != "/" ]; then
    rm -rf $TMP
  fi
fi
