#!/bin/sh

#------------------------------------------------------------------------------
# package details

PACKAGE="rxvt-unicode"

ARCHIVE="rxvt-unicode-9.31.tar.gz"
WORKDIR="rxvt-unicode-9.31"


#------------------------------------------------------------------------------
# set tool options

# compiler options
export CXXFLAGS="$CXXFLAGS -std=c++11"


#------------------------------------------------------------------------------
# clean up previous work

rm -rf $WORKDIR


#------------------------------------------------------------------------------
# extract archive

tar -xf $ARCHIVE 


#------------------------------------------------------------------------------
# enter build directory

cd $WORKDIR 


#------------------------------------------------------------------------------
# apply source code modifications

patch -b -Np1 -i ../rxvt-unicode-9.31-configure-features.patch 
patch -b -Np1 -i ../rxvt-unicode-9.31-disable-changing-mouse-cursor.patch 
patch -b -Np1 -i ../rxvt-unicode-9.31-load-systemwide-configuration-on-startup.patch 
patch -b -Np1 -i ../rxvt-unicode-9.31-support-24-bit-true-color.patch 


#------------------------------------------------------------------------------
# build and install package

# configure build
./configure --prefix=/usr                     \
            --disable-fallback                \
            --disable-perl                    \
            --disable-xim                     \
            --enable-24-bit-color             \
            --enable-256-color                \
            --enable-smart-resize             \
            --enable-unicode3                 \
            --with-codesets=jp,kr,zh          \
            --with-name=rxvt                  \
            --with-res-class=Rxvt             \
            --with-res-name=rxvt              \
            --with-term=rxvt-unicode-256color

# build it
make

# install it
make install


#------------------------------------------------------------------------------
# create startup script

cat > /usr/bin/rxvt-start << EOF
#!/bin/sh

# start rxvt client
rxvtc "\$@"

# daemon isn't running?
if [ \$? -eq 2 ]
then
    # start daemon
    rxvtd -q -o -f

    # start client
    rxvtc "\$@"
fi
EOF

# make it executable
chmod a+x /usr/bin/rxvt-start 


#------------------------------------------------------------------------------
# compatibility symlink

if [ ! -f /usr/bin/gnome-terminal ]
then
    ln -sf rxvt-start /usr/bin/gnome-terminal
fi

if [ ! -f /usr/bin/terminal ]
then
    ln -sf rxvt-start /usr/bin/terminal 
fi


#------------------------------------------------------------------------------
# install configuration file

if [ -d /usr/lib/X11/app-defaults ]
then
    cp ../Rxvt /usr/lib/X11/app-defaults

elif [ -d /etc/X11/app-defaults ]
then
    cp ../Rxvt /etc/X11/app-defaults

else
    echo "Not sure where to install configuration file. You'll have to figure it out."
fi


#------------------------------------------------------------------------------
# clean up

rm -rf $WORKDIR


#------------------------------------------------------------------------------

