#!/bin/sh
# The ncurses library also build serveral binaries, one of which
# (tic) is then used to compile the terminfo.src file into the
# termcap library. Someone found a bug in tic via the terminfo.src
# file, causing tic to loop forever.  The problem is you need tic
# v5.7 to install terminfo v5.7, so it's a chicken and egg problem.
# So here I'll build a copy of the binaries on the host machine,
# then use them to build for the target.
# These are important for tools built by the Makefile for execution on host
BUILD_CC=`which gcc`
BUILD_AS=`which as`
gcc_dir=${BUILD_CC/%\/gcc/}
as_dir=${BUILD_AS/%\/as/}
BUILD_CFLAGS="-B$gcc_dir -B$as_dir"
BUILD_LDFLAGS="-L/lib -L/usr/lib"

TEMP_DIR=`pwd`
TEMP_DIR=${TEMP_DIR}/tmp

# First, I'm going to reduce the number of entries in the termlib
# to only the 33 most common and useful entries
patch -N misc/run_tic.in <<"EOF"
--- run_tic.in.org      2006-10-28 09:43:30
+++ run_tic.in  2009-04-30 23:10:17
@@ -112,3 +112,3 @@
 EOF
-if ( $SHLIB tic$suffix -x -s -o $TERMINFO $source )
+if ( $SHLIB tic$suffix -x -s -e hurd,ansi,pcansi,dumb,wsvt25,wsvt25m,mach-bold,mach-color,mach,vt102,vt220,vt52,vt100,Eterm,linux,xterm-mono,xterm,xterm-r6,xterm-r5,xterm-vt220,xterm-debian,xterm-xfree86,xterm-color,rxvt-unicode,rxvt-basic,rxvt,screen-s,screen-bce,sun,screen-w,screen,cons25,cygwin -o $TERMINFO $source )
 then
@@ -131,3 +131,3 @@
 EOF
-if ( $SHLIB tic$suffix -s -o $TERMINFO $source )
+if ( $SHLIB tic$suffix -s -e hurd,ansi,pcansi,dumb,wsvt25,wsvt25m,mach-bold,mach-color,mach,vt102,vt220,vt52,vt100,Eterm,linux,xterm-mono,xterm,xterm-r6,xterm-r5,xterm-vt220,xterm-debian,xterm-xfree86,xterm-color,rxvt-unicode,rxvt-basic,rxvt,screen-s,screen-bce,sun,screen-w,screen,cons25,cygwin -o $TERMINFO $source )
 then
EOF

./configure --prefix=$TEMP_DIR

# Make the binary of "file" that will execute on the build machine
make install.includes install.progs

# Now clean out all traces of the build to avoid contamination
make distclean

BASE=~/src/crosstool/gcc-3.4.3-glibc-2.3.2/powerpc-linux-gnu
PACKAGE=`pwd`/../plugin/tools/TOOLS
PACKAGE_DATA=/data/usr/share
PACKAGE_LINK=/data/usr/lib
export CC=powerpc-linux-gnu-gcc
export AR=powerpc-linux-gnu-ar
export PATH=$TEMP_DIR/bin:$BASE/bin:$BASE/powerpc-linux-gnu/bin:$PATH

## Someone found a bug in tic via the terminfo.src file, causing tic
## to loop forever.  The problem is you need tic v5.7 to install
## terminfo v5.7, so it's a chicken and egg problem.
#patch -N misc/terminfo.src <<"EOF"
#--- terminfo.src_5.7    2009-04-28 13:17:21.000000000 -1000
#+++ terminfo.src_5.7_not_suck    2009-04-28 13:16:57.000000000 -1000
#@@ -3708,8 +3532,7 @@
# konsole-xf4x|KDE console window with keyboard for XFree86 4.x xterm,
#-	kend=\EOF, khome=\EOH, use=konsole+pcfkeys,
#-	use=konsole-vt100,
#-# Konsole does not implement shifted cursor-keys.
#-konsole+pcfkeys|konsole subset of xterm+pcfkeys,
#-	kLFT@, kRIT@, kcbt=\E[Z, kind@, kri@, kDN@, kUP@, use=xterm+pcc2,
#-	use=xterm+pcf0,
#+	kend=\EOF, kf1=\EOP, kf13=\EO2P, kf14=\EO2Q, kf15=\EO2R,
#+	kf16=\EO2S, kf17=\E[15;2~, kf18=\E[17;2~, kf19=\E[18;2~,
#+	kf2=\EOQ, kf20=\E[19;2~, kf21=\E[20;2~, kf22=\E[21;2~,
#+	kf23=\E[23;2~, kf24=\E[24;2~, kf3=\EOR, kf4=\EOS,
#+	khome=\EOH, use=konsole-vt100,
# # KDE's "vt100" keyboard has no relationship to any terminal that DEC made, but
#EOF

# Configure, make and install into our build environment
./configure --prefix=$BASE/powerpc-linux-gnu \
            --libdir=$BASE/powerpc-linux-gnu/lib \
            --includedir=$BASE/powerpc-linux-gnu/include \
            --with-build-cc=$BUILD_CC \
            --with-build-cflags="$BUILD_CFLAGS" \
            --with-build-ldflags="$BUILD_LDFLAGS" \
            --host=powerpc-linux-gnu \
            --with-shared --with-normal --without-debug


make
make install.libs install.includes install.ncurses install.progs install.data

make clean

# Configure, make and install into our target environment
./configure --with-terminfo-dirs=$PACKAGE_DATA \
            --datadir=$PACKAGE_DATA --prefix=/ \
            --libdir=/lib \
            --includedir=/include \
            --with-build-cc=$BUILD_CC \
            --with-build-cflags="$BUILD_CFLAGS" \
            --with-build-ldflags="$BUILD_LDFLAGS" \
            --host=powerpc-linux-gnu \
            --with-shared --with-normal --without-debug

make
make DESTDIR=$PACKAGE install.libs install.includes install.ncurses install.progs
mkdir -p $PACKAGE/$PACKAGE_LINK
make DESTDIR=$PACKAGE install.data



