#!/bin/sh
# $Id: vnc.SlackBuild,v 1.3 2007/01/08 11:25:04 root Exp root $
# -----------------------------------------------------------------------------
#
# Slackware SlackBuild script 
# ===========================
# By:        Eric Hameleers <alien@slackware.com>
# For:       vnc
# URL:       http://www.realvnc.com/
# Needs:     Fairly recent version of X
# Changelog:
# 4.0-1:     16/oct/2004 by Eric Hameleers
# 4_1_1-1:   30/apr/2005 by Eric Hameleers
#            * Unfortunately, there's no updated documentation tarball yet.
# 4_1_1-2:   02/aug/2005 by Eric Hameleers
#            * Forgot to add the vnc extension for X to the package.
# 4_1_1-3:   12/aug/2005 by Eric Hameleers
#            * Somehow the permissions of the package were messed up, so that
#              after installation,. / was group-writable and Sendmail
#              refused to run. Fixed this in _vnc.tar.gz.
# 4.1.2-1:   21/nov/2006 by Eric Hameleers
#            * The 4.1.2 is a security fix.
# 4.1.2-2:   08/jan/2007 by Eric Hameleers
#            * Fixed group-writable directories in the package (sendmail would
#              stop working after install of the vnc package).
#
# -----------------------------------------------------------------------------
# 
# Issue 'sh SlackBuild --cleanup' to build a Slackware package.
# The package (.tgz) plus descriptive .txt file are created in /tmp .
# Install using 'installpkg'. 
#
# -----------------------------------------------------------------------------


# --- INIT ---
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi

PRGNAM=vnc
SRCVER=4_1_2
VERSION=4.1.2
SUB=x86_linux
ARCH=i486
BUILD=2

PKG=$TMP/package-$PRGNAM

if [ ! -d $TMP/tmp-$PRGNAM ]; then
  mkdir -p $TMP/tmp-$PRGNAM # location to build the source
else
  rm -rf $TMP/tmp-$PRGNAM/*
fi
if [ ! -d $PKG ]; then
  mkdir -p $PKG # place for the package to be built
else
  rm -rf $PKG/*
fi


# --- PACKAGE BUILDING ---


echo "++"
echo "|| $PRGNAM-$VERSION"
echo "++"

# Explode the package framework:
cd $PKG
explodepkg $CWD/_$PRGNAM.tar.gz

cd $TMP/tmp-$PRGNAM

#
# Extract the tarball, patch and modify
#
echo "Extracting the program source archive(s)..."
tar -xzvf $CWD/${PRGNAM}-${SRCVER}-${SUB}.tar.gz

mv ${PRGNAM}-${SRCVER}-${SUB} ${PRGNAM}-${VERSION}
cd ${PRGNAM}-${VERSION}

chown -R root:root *
find . -perm 777 -exec chmod 755 {} \;
find . -perm 666 -exec chmod 644 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 664 -exec chmod 644 {} \;


#cp -a Xvnc vncconnect vncpasswd vncserver vncviewer $PKG/usr/bin
#chmod 555 $PKG/usr/bin/*
#chgrp bin $PKG/usr/bin/*
./vncinstall $PKG/usr/bin $PKG/usr/man $PKG/usr/X11R6/lib/modules/extensions

cp -a java/* $PKG/usr/share/vnc/classes/
chmod 644  $PKG/usr/share/vnc/classes/*

cp -a $CWD/Vncviewer.ad-$VERSION $PKG/usr/lib/X11/app-defaults/Vncviewer
chmod 644 $PKG/usr/lib/X11/app-defaults/Vncviewer


# --- DOCUMENTATION ---

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION

cp -a LICENCE.txt README $PKG/usr/doc/$PRGNAM-$VERSION
mv $PKG/install/README.1st $PKG/usr/doc/$PRGNAM-$VERSION
chown -R root:root $PKG/usr/doc/$PRGNAM-$VERSION/*
chmod -R a-ws $PKG/usr/doc/$PRGNAM-$VERSION/*

# Compress the man pages
find $PKG/usr/man -type f -name "*.?" -exec gzip -9f {} \;

#
# Patch vncserver :
# alternate default xstartup & enable rgb and font paths
#
sed -i \
  -e 's?^\( *"\)\(xterm -geometry 80x24+10+10 -ls -title \\"\\\$VNCDESKTOP Desktop\\" &\\n"\.\)$?\1#\2?' \
  -e "s#^\(.*\)\(\"twm &\\\n\");\)\$#\1\"xterm -geometry 124x24\+30\+30 -title \\\\\"VNC Startup Tip\\\\\" -e less /usr/doc/vnc-$VERSION/README\.1st \&\\\n\"\.\\
\1\2#" \
  -e 's?# *\(\$cmd \.= " -fp /usr/lib/X11/fonts/misc/,/usr/lib/X11/fonts/75dpi/";\)?\1?' \
  -e 's?# *\(\$cmd \.= " -co /usr/lib/X11/rgb";\)?\1?' \
  $PKG/usr/bin/vncserver


# --- OWNERSHIP, RIGHTS ---

chmod -R o-w $PKG


# --- PACKAGE DESCRIPTION ---

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc


# --- BUILDING ---

# Build the package:
cd $PKG
makepkg --linkadd y --chown n $TMP/$PRGNAM-$VERSION-$ARCH-$BUILD.tgz 2>&1 \
  | tee $CWD/makepkg.log
(cd $TMP && \
  md5sum $PRGNAM-$VERSION-$ARCH-$BUILD.tgz > \
  $PRGNAM-$VERSION-$ARCH-$BUILD.tgz.md5
)
cat $PKG/install/slack-desc | grep "^${PRGNAM}" > \
  $TMP/$PRGNAM-$VERSION-$ARCH-$BUILD.txt


# --- CLEANUP ---

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