# Depends on static-pixman
# Depends on static-libdrm
# Depends on static-bigreqspr
# Depends on static-composite
# Depends on static-damagepro
# Depends on static-fixesprot
# Depends on static-fontsprot
# Depends on static-randrprot
# Depends on static-recordpro
# Depends on static-renderpro
# Depends on static-resourcep
# Depends on static-videoprot
# Depends on static-xcmiscpro
# Depends on static-xineramap
# Depends on static-libxfont
# Depends on static-libxkbfil
# Depends on static-libxxf86d
# Depends on static-libpciacc

# Exit on error:
set -e

wget http://www.x.org/releases/individual/xserver/xorg-server-1.14.3.tar.bz2
tar xf xorg-server-1.14.3.tar.bz2
cd xorg-server-1.14.3

LD=/static/bin/ld \
    CPPFLAGS=-D_GNU_SOURCE \
    PKG_CONFIG=/static/bin/pkg-config \
    CC=/static/bin/musl-gcc \
    LDFLAGS='-s -L/static/lib -static -static-libgcc' \
    ./configure --prefix=/static \
    --with-xkb-output=/static/var/lib/xkb \
    --enable-install-setuid \
    --disable-glx \
    --disable-screensaver \
    --disable-dri \
    --disable-dri2

# Fix hw/xfree86/os-support/xf86_OSlib.h:182:20: fatal error: termio.h: No such file or directory
sed -e '182s/termio.h/termios.h/' \
    -i hw/xfree86/os-support/xf86_OSlib.h

# Fix the following:
#  CCLD   Xnest
#/static/lib/libX11.a(XKBGAlloc.o): In function `XkbFreeGeomOverlayKeys':
#/usr/src/static-libx11/libX11-1.6.2/src/xkb/XKBGAlloc.c:256: multiple definition of `XkbFreeGeomOverlayKeys'
#../../xkb/.libs/libxkb.a(XKBGAlloc.o):/usr/src/static-xorg-serv/xorg-server-1.14.3/xkb/XKBGAlloc.c:243: first defined here
sed -e '240s,^$,/*,' \
    -e '249s,^$,*/,' \
    -i.orig xkb/XKBGAlloc.c

make install

cd ..
rm -rf tar xf xorg-server-1.14.3
rm xorg-server-1.14.3.tar.bz2

cat << EOF >> /static/etc/sysconfig/createfiles
/tmp/.ICE-unix dir 1777 root root
/tmp/.X11-unix dir 1777 root root
EOF

echo
echo Remember to run the following commands as root before starting
echo the X server:
echo chown root /static/bin/Xorg
echo chmod u+s /static/bin/Xorg

exit 0
