#!/bin/sh
#
# Build and install XFree86-4.2.0 on Slackware Linux.
#

# *****************************************************
# **************  GLIDE SUPPORT  **********************
# *****************************************************
# If you want to build against Glide, run with $1 = YES
# See more info below.
GLIDE=$1

if [ "$GLIDE" = "YES" ]; then
  echo "*** building X with Glide support ***"
  sleep 10
else
  echo "*** building X without Glide support ***"
  sleep 10
fi

CWD=`pwd`
cd /tmp
tar xjvf $CWD/X420src-1.tar.bz2
tar xjvf $CWD/X420src-2.tar.bz2
tar xjvf $CWD/X420src-3.tar.bz2
cd xc
zcat $CWD/xf86site.def.diff.gz | patch -p1 --backup --verbose --suffix=.orig -E
zcat $CWD/site.def.diff.gz | patch -p1 --backup --verbose --suffix=.orig -E
zcat $CWD/Xlib.h.diff.gz | patch -p1 --backup --verbose --suffix=.orig -E
zcat $CWD/xclock.glibc.diff.gz | patch -p1 --backup --verbose --suffix=.orig -E
zcat $CWD/linux.cf.zlib.diff.gz | patch -p1 --backup --verbose --suffix=.orig -E
zcat $CWD/4.2.0-4.2.1.diff.gz | patch -p1 --backup --verbose --suffix=.orig -E
zcat $CWD/4.2.1-mit-shm-security.patch.gz | patch -p1 --backup --verbose --suffix=.orig -E

# Video hang bug:
zcat $CWD/libXi.diff.gz | patch -p1 --backup --verbose --suffix=.orig -E

# Font server overflows.  I'm sure you thought the X font server code was entirely
# free of these.  Well, this will fix all of them for sure!
zcat $CWD/fs.diff.gz | patch -p1 --backup --verbose --suffix=.orig -E

# Guess not... here's are some more font related overflow fixes:
zcat $CWD/fontfile.diff.gz | patch -p1 --backup --verbose --suffix=.orig -E

if [ "$GLIDE" = "YES" ]; then
# This edits xf86site.def to add HasGlide3.  You'll need to install the a DRI capable
# version of Glide for your video card class (voodoo3/banshee, or voodoo4/5), then the
# tdfx DRI module will be built for that card.
zcat $CWD/glide.diff.gz | patch -p1 --backup --verbose --suffix=.orig -E
fi

# We may be splitting this out at a later date.
( cd extras/freetype2
  CFLAGS="-O2 -march=i386 -mcpu=i686" make setup CFG="--prefix=/usr/X11R6 i386-slackware-linux"
  make
  make install
  echo "#define Freetype2Dir     /usr/X11R6" > /tmp/xc/config/cf/host.def
 )

make World -i CDEBUGFLAGS="-O2 -march=i386 -mcpu=i686"
make install
make install.man

# Fix slight startx bug:
( cd /usr/X11R6/bin
  zcat $CWD/startx.diff.gz | patch -p1 --verbose )

# Wipe/recompile freetype2 (trust me :)
rm -f /usr/X11R6/include/ft2build.h
rm -r /usr/X11R6/include/freetype2
rm -f /usr/X11R6/lib/libfreetype.*
( cd extras/freetype2
  make clean
  CFLAGS="-O2 -march=i386 -mcpu=i686" make setup CFG="--prefix=/usr/X11R6 i386-slackware-linux"
  make
  make install
 )

# Just to be on the safe side, we should provide Compose files where
# they are missing, as it's been known to cause a crash.
for dir in /usr/X11/lib/X11/locale/* ; do
  if [ -d $dir ]; then
    if [ -r $dir/XI18N_OBJS -o -r $dir/XLC_LOCALE ]; then
      if [ ! -r $dir/Compose ]; then
        NAME=`basename $dir`
        CAPN=`echo $NAME | tr [a-z] [A-Z]`
        cat << EOF > $dir/Compose
#
# $CAPN Compose Sequence
#
# Sequence Definition
#
# \$XFree86: xc/nls/Compose/$NAME,v 1.2 `date` volkerdi Exp $
#
# This file currently has no entries.  It appears that a compose file (even
# just an empty one) is required for the appropriate keysyms to work for
# this encoding.
#
# <Multi_key> Means <Compose>
# Special Character

# End of Sequence Definition
EOF
      fi
    fi
  fi
done

#mkdir -p /usr/X11R6/lib/fonts/CID
#if [ ! -r /usr/X11R6/lib/fonts/CID/fonts.dir ]; then
#  echo 0 > /usr/X11R6/lib/fonts/CID/fonts.dir
#fi
#if [ ! -r /usr/X11R6/lib/fonts/CID/fonts.scale ]; then
#  echo 0 > /usr/X11R6/lib/fonts/CID/fonts.scale
#fi

mkdir -p /usr/X11R6/lib/X11/fonts/misc
cat $CWD/linux8x16.pcf.gz > /usr/X11R6/lib/X11/fonts/misc/linux8x16.pcf.gz
cat $CWD/linux8x8.pcf.gz > /usr/X11R6/lib/X11/fonts/misc/linux8x8.pcf.gz

rm -f /usr/X11R6/lib/X11/config/host.def
rm -rf /install
mkdir /install
cat $CWD/xfree86-devel/doinst.sh > /install/doinst.sh

cat $CWD/xinit/README.Xmodmap > /etc/X11/xinit/README.Xmodmap
cat $CWD/xinit/.Xmodmap > /etc/X11/xinit/.Xmodmap

cp -a /etc/X11/xdm/Xsession /etc/X11/xdm/Xsession.orig
cp -a /etc/X11/xdm/Xsetup_0 /etc/X11/xdm/Xsetup_0.orig
cat $CWD/xdm/Xsession > /etc/X11/xdm/Xsession
cat $CWD/xdm/Xsetup_0 > /etc/X11/xdm/Xsetup_0

if [ "$GLIDE" = "YES" ]; then
  cat $CWD/README.tdfx > /usr/X11R6/lib/modules/dri/README.tdfx
fi

rm -f /usr/X11R6/lib/libz.a

cat $CWD/XftConfig/XftConfig > /usr/X11R6/lib/X11/XftConfig
cat $CWD/XftConfig/XftConfig.orig > /usr/X11R6/lib/X11/XftConfig.orig

( cd /usr/X11R6/lib/X11/fonts/misc
  mkfontdir -e /usr/X11R6/lib/X11/fonts/encodings -e /usr/X11R6/lib/X11/fonts/encodings/large . )

( cd /etc/X11/xdm
  rm authdir
  ln -sf ../../../var/lib/xdm authdir )
( cd /etc/X11/xkb
  rm compiled
  ln -sf ../../../var/lib/xkb compiled )

mkdir -p /install
cp $CWD/slack-desc/slack-desc.* /install

mv /etc/X11/xinit/xinitrc /etc/X11/xinit/xinitrc.twm

cat << EOF

Slackware X build complete!

EOF
