#!/bin/sh
#
# $Id: cwmbuild.sh 504 2010-01-29 17:30:09Z tamentis $
#
# This script aims at building OpenBSD's cwm on a Linux platform, it fetches
# pieces of code on various CVS repository.
#
# Last updated and working on 2009-08-30
# Written originally by Bertrand Janin <tamentis@neopulsar.org>
# Updated on 2010-01-29 by Christian Neukirchen <chneukirchen@gmail.com>
#
# Yes I know this is far from clean, feel free to improve.
#


# Setup
BUILD_DIR=/tmp/cwm_build
OBSD_CVSROOT=anoncvs@anoncvs.fr.openbsd.org:/cvs
NBSD_CVSROOT=anoncvs@anoncvs.fr.NetBSD.org:/pub/NetBSD-CVS
OSSH_CVSROOT=anoncvs@anoncvs.mindrot.org:/cvs
PATCH_FILENAME=obsd_cwm_linux.patch

# Prepare the building directory, wipe out everything to avoid conflicts.
rm -rf $BUILD_DIR
mkdir -p $BUILD_DIR

# Extract the patch while we are in the directory, it is located at the end
# of this very file to avoid loading extra files.
sed -n '/^diff/,$p' $0 > $BUILD_DIR/$PATCH_FILENAME

cd $BUILD_DIR

# Load cwm
if ! cvs -d $OBSD_CVSROOT co xenocara/app/cwm/; then
	echo Unable to load cwm from CVS...
	exit
fi

# Load OpenSSH compat lib (strlcat, strlcpy, etc...)
if ! cvs -d $OSSH_CVSROOT co openssh/openbsd-compat/{strlcpy.c,strlcat.c,strtonum.c}; then
	echo Unable to load openbsd-compat from CVS...
	exit
fi

# Load fgetln.c from NetBSD compat
if ! cvs -d $NBSD_CVSROOT co src/tools/compat/fgetln.c; then
	echo Unable to load netbsd\'s fgetln from CVS...
	exit
fi


# Move everything in there
mv -f xenocara/app/cwm/* openssh/openbsd-compat/*.c src/tools/compat/*.c .

patch < $PATCH_FILENAME

echo
echo At this point, if the patch applied properly, you should be able to do
echo the following to compile and install cwm. Make sure you have freetype
echo installed properly with its headers:
echo
echo "    cd $BUILD_DIR"
echo "    make"
echo "    sudo make install"
echo
echo This will install cwm in /usr/local/bin and will put the man pages 
echo in /usr/local/man/man1 and man5.
echo
echo Good luck

exit
# Everything below this line is the patch itself. 
# --------------------------------------------------------------------------

diff -aur cwm_build.old/calmwm.h cwm_build/calmwm.h
--- cwm_build.old/calmwm.h	2010-01-29 09:56:14.069865288 +0100
+++ cwm_build/calmwm.h	2010-01-29 09:57:50.840856134 +0100
@@ -21,6 +21,10 @@
 #ifndef _CALMWM_H_
 #define _CALMWM_H_
 
+/* ugly stuff */
+#define TAILQ_END(head) NULL
+#define __dead
+
 #include <X11/Xatom.h>
 #include <X11/Xft/Xft.h>
 #include <X11/Xlib.h>
diff -aur cwm_build.old/includes.h cwm_build/includes.h
--- cwm_build/includes.h        1969-12-31 19:00:00.000000000 -0500
+++ cwm_build.new/includes.h    2009-08-30 19:20:14.000000000 -0400
@@ -0,0 +1 @@
+
diff -aur cwm_build.old/Makefile cwm_build/Makefile
--- cwm_build.old/Makefile	2010-01-29 09:56:14.071106346 +0100
+++ cwm_build/Makefile	2010-01-29 10:04:04.889862860 +0100
@@ -1,14 +1,23 @@
 # $OpenBSD: Makefile,v 1.15 2009/01/19 20:23:19 todd Exp $
 
-.include <bsd.xconf.mk>
+#.include <bsd.xconf.mk>
 
 PROG=		cwm
 
+BINDIR=		/usr/bin
+
 SRCS=		calmwm.c screen.c xmalloc.c client.c menu.c \
 		search.c util.c xutil.c conf.c input.c xevents.c group.c \
 		kbfunc.c mousefunc.c font.c parse.y
 
-CPPFLAGS+=	-I${X11BASE}/include -I${X11BASE}/include/freetype2 -I${.CURDIR}
+OBJS=		calmwm.o screen.o xmalloc.o client.o menu.o \
+		search.o util.o xutil.o conf.o input.o xevents.o group.o \
+		kbfunc.o mousefunc.o font.o strlcpy.o strlcat.o y.tab.o \
+		strtonum.o fgetln.o
+
+X11BASE=	/usr
+
+CPPFLAGS+=	-I${X11BASE}/include -I${X11BASE}/include/freetype2 -I.
 
 CFLAGS+=	-Wall
 
@@ -20,7 +29,23 @@
 
 CLEANFILES=	cwm.cat1 cwmrc.cat5
 
-obj: _xenocara_obj
 
-.include <bsd.prog.mk>
-.include <bsd.xorg.mk>
+all: $(PROG)
+
+y.tab.c: parse.y
+	byacc parse.y
+
+
+$(PROG): $(OBJS) y.tab.o
+	$(CC) $(OBJS) ${LDADD} -o ${PROG}
+
+$(OBJS): %.o: %.c
+	$(CC) -c $(CFLAGS) $(CPPFLAGS) $<
+
+install: ${PROG}
+	install -m 755 cwm /usr/local/bin/
+	install -m 644 cwm.1 /usr/local/man/man1
+	install -m 644 cwmrc.5 /usr/local/man/man5
+
+#.include <bsd.prog.mk>
+#.include <bsd.xorg.mk>
diff -aur cwm_build.old/xevents.c cwm_build/xevents.c
--- cwm_build.old/xevents.c	2010-01-29 09:56:14.069865288 +0100
+++ cwm_build/xevents.c	2010-01-29 10:01:43.472994946 +0100
@@ -29,6 +29,7 @@
 
 #include <err.h>
 #include <errno.h>
+#include <signal.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
