#!/bin/sh

# This scripts work as following:
# (1) cp current xf86site.def (it may be created by imake-4 ports) 
#       to ${WRKDIR}/xc/config/cf.
#     this means this ports use imake-4's config defaultly.
# (2) Generate temporal config for compiling.
#     Some configs, such as `ForceNormalLib', `FreeBSDBuildXprog', are
#     used for compiling this ports localy. so these configs will be generated
#     this scripts. these configs will be add to `host.def' file.
#     but this host.def will never install. use local only.

ORIGDEF=$PREFIX/lib/X11/config/xf86site.def
DESTDEF=$WRKDIR/xc/config/cf/xf86site.def
ORIGHOSTDEF=$PREFIX/lib/X11/config/host.def
LOCALDEF=$WRKDIR/.config
HOSTDEF=$WRKDIR/xc/config/cf/host.def

configure () {
    # Use original host.def as initial config file
    rm -f $LOCALDEF
    grep -v '#define.*ProjectRoot' $ORIGHOSTDEF >> $LOCALDEF
    echo "#define ProjectRoot $PREFIX" >> $LOCALDEF

    # It's good for FreeBSD ports/packages system.
    echo "#define NothingOutsideProjectRoot YES" >> $LOCALDEF
    echo "#define InstallXserverSetUID NO" >> $LOCALDEF

    # User Config.
    if [ X$HasSecureRPC != XDEFAULT -a X$HasSecureRPC != X ]; then
	echo "#define HasSecureRPC $HasSecureRPC" >> $LOCALDEF
    fi
    if [ X$BuildPexExt != XDEFAULT -a X$BuildPexExt != X ]; then
	echo "#define BuildPexExt $BuildPexExt" >> $LOCALDEF
    fi
    if [ X$BuildXinerama != XDEFAULT -a X$BuildXinerama != X ]; then
	echo "#define BuildXinerama $BuildXinerama" >> $LOCALDEF
    fi
    if [ X$BuildXIE != XDEFAULT -a X$BuildXIE != X ]; then
	echo "#define BuildXIE $BuildXIE" >> $LOCALDEF
    fi
    if [ X$BuildAoutLibraries != XDEFAULT -a X$BuildAoutLibraries != X ]; then
	echo "#define BuildAoutLibraries $BuildAoutLibraries" >> $LOCALDEF
    fi
    if [ X$ForceNormalLib != XDEFAULT -a X$ForceNormalLib != X ]; then
	echo "#define ForceNormalLib $ForceNormalLib" >> $LOCALDEF
    fi
    if [ X$DebuggableLibraries != XDEFAULT -a X$DebuggableLibraries != X ]; then
	echo "#define DebuggableLibraries $DebuggableLibraries" >> $LOCALDEF
    fi

    # disable some configs: there are not used this ports
    for i in \
	InstallXdmConfig \
	InstallXinitConfig \
	InstallFSConfig \
	InstallAppDefFiles \
	BuildServer \
	BuildFontServer \
	BuildFonts \
	Build75DpiFonts \
	Build100DpiFonts \
	BuildSpeedoFonts \
	BuildType1Fonts \
	BuildCIDFonts \
	BuildCyrillicFonts \
	BuildLatin2Fonts
    do \
	echo "#define $i NO" >> $LOCALDEF
    done
    echo "#define HasFreetype2 YES" >> $LOCALDEF
    echo "#define Freetype2Dir ${LOCALBASE}" >> $LOCALDEF
    echo "#define FreeBSDBuildXlib  YES" >> $LOCALDEF
    echo "#define FreeBSDBuildXbin   NO" >> $LOCALDEF

    HasXdmAuth=YES

    cpwh=NO
    if [ $HasXdmAuth = YES ]; then
    	WH=$WRKDIR/xc/lib/Xdmcp/Wraphelp.c
    	if [ -f $WH ] ; then
	    cpwh=SOURCE
    	elif [ -f $DISTDIR/xc/Wraphelp.c ] ; then
	    cpwh=$DISTDIR/xc/Wraphelp.c
    	else
	    echo "==> You must fetch USA-legal Wraphelp.c manually"
	    echo "==> and put it to ${DISTDIR}/xc/."
	    exit 1
	fi
    	if [ X$cpwh != XNO -a X$cpwh != XSOURCE ]; then
	    tr -d '\r' < $cpwh > $WH
    	fi
    fi

    # Copy ORIGDEF to DESTDEF
    rm -f $DESTDEF
    grep -v '#define.*HasXdmAuth' $ORIGDEF >> $DESTDEF
    if [ $cpwh = NO ] ; then
	echo "#define HasXdmAuth NO" >> $DESTDEF
    else
	echo "#define HasXdmAuth YES" >> $DESTDEF
    fi

    # copy generated config to host.def
    cp -f $LOCALDEF $HOSTDEF
}

configure
cp ${X11BASE}/lib/X11/config/version.def ${WRKSRC}/config/cf
exit 0
