# Copyright 1999-2004 Gentoo Technologies, Inc.
# Distributed under the terms of the GNU General Public License v2
# $Header: $
DESCRIPTION="Cyrilic support for X and console"
HOMEPAGE="http://lml.bas.bg/~anton/linux/bglinux.html"

SRC_URI="ftp://lml.bas.bg/home/anton/linux/${P}.tar.gz"
LICENSE="GPL2"
SLOT="0"
KEYWORDS="~x86"
#IUSE="X gnome"

# >libc2.2, if X then > X4.0.2, localedef installed, if Qt then > 2.3
DEPEND=""
#RDEPEND=""

# Source directory; the dir where the sources can be found (automatically
# unpacked) inside ${WORKDIR}.  S will get a default setting of ${WORKDIR}/${P}
# if you omit this line.
S=${WORKDIR}/${PN}


src_install() {

set -e
#destination dir
DD=/usr/local
NEWMYROOT=${D}${DD}
#NEWMYROOT2='\/usr\/local'

DIR=${S}

einfo "The new version of bglinux is being installed in :\n${NEWMYROOT}... "
dodir ${DD}
cp -r ${DIR}/hierarchy/* ${NEWMYROOT}

einfo "Installing improved keyboard support for X... "
dodir /usr/X11R6/lib/X11/xkb
cp -r ${NEWMYROOT}/X11R6/lib/X11/xkb/* ${D}usr/X11R6/lib/X11/xkb

einfo "Installing support for CP1251 fontencoding for X... "
dodir /usr/X11R6/lib/X11/cyrencodings
cp -r ${NEWMYROOT}/X11R6/lib/X11/cyrencodings/* ${D}usr/X11R6/lib/X11/cyrencodings
dodir /usr/lib/X11/locale/microsoft-cp1251
touch ${D}/usr/lib/X11/locale/microsoft-cp1251/Compose
dodir /usr/X11R6/lib/X11/locale/microsoft-cp1251
touch ${D}/usr/X11R6/lib/X11/locale/microsoft-cp1251/Compose

einfo "Please wait.  Compilling Cyrillic locales for"
dodir /usr/share/i18n
cp -r ${NEWMYROOT}/share/i18n/* ${D}/usr/share/i18n

dodir /usr/lib/locale
cp /usr/lib/locale/locale-archive ${D}/usr/lib/locale
if [ -x /usr/bin/localedef ]; then
    (
	cd ${D}/usr/share/i18n/locales
	einfo " CP1251"
	/usr/bin/localedef -f ../charmaps/CP1251 -i bg_BG bg_BG --prefix=${D} 
	einfo " UTF-8"
	/usr/bin/localedef -f ../charmaps/UTF-8 -i bg_BG bg_BG.UTF-8 --prefix=${D}
	einfo " ISO-8859-5"
	/usr/bin/localedef -f ../charmaps/ISO-8859-5 -i bg_BG bg_BG.ISO8859-5 --prefix=${D}
	einfo " KOI8-R"
	/usr/bin/localedef -f ../charmaps/KOI8-R -i bg_BG bg_BG.KOI8-R --prefix=${D}
	einfo "done..."
    )
else
    einfo " No, you don't have /usr/bin/localedef installed."
fi

einfo "Patching some files:"
dodir /etc/X11/Sessions/
cp /etc/X11/Sessions/* ${D}etc/X11/Sessions/
#!!more files need to be copied to sandbox

for i in {${D}etc/X11,${D}etc/X11/xdm,${D}etc/X11/kdm,${D}etc/X11/wdm}/Xsession {${D}etc/X11,${D}etc/X11/gdm,${D}etc/gdm}/Sessions/* {${D}etc/X11/xinit,${D}usr/X11R6/lib/X11/xinit}/xinitrc* ; do
#	echo "    $i"
    if [ -e "$i" ]; then
	echo "    $i"
	ed $i 2>/dev/null <<'EOF'
g/#@bglinux.@/d
1a
if test -f ${HOME}/.xcyrillic ;then . ${HOME}/.xcyrillic;fi #@bglinux3@ -> keep
.
wq
EOF
    fi
done


dodir /tmp
dodir /usr/share/locale/
cp /usr/share/locale/locale.alias  ${D}/usr/share/locale/
for i in ${D}etc/locale.alias ${D}usr/share/locale/locale.alias ${D}etc/X11/gdm/locale.alias ${D}etc/gdm/locale.alias; do
    if [ -e "$i" ]; then
	echo "    $i"
	awk '
/^[^#]*bg_BG/ {
    next;
}

{
    print;
}

END {
    print "Bulgarian       bg_BG"
}
' $i >${D}/tmp/locale.alias.$$
	cp ${D}/tmp/locale.alias.$$ $i
    fi
done


cp /etc/X11/XF86Config-4 ${D}etc/X11
for i in ${D}/etc/X11/XF86Config-4 ${D}/etc/X11/XF86Config; do
    if [ -e "$i" ]; then
	echo "    $i"
	awk '
/^ *[Ss]ection *"Module"/ {
    modsect=1;
}

/^[ 	]*[Ll]oad[ 	]*"type1"/ {
    if (modsect==1) {next}
}

/^[ 	]*[Ll]oad[ 	]*"bitmap"/ {
    if (modsect==1) {next}
}

/^ *[Ee]nd[Ss]ection/ {
    if (modsect == 1) {
        print "        Load \"bitmap\"";
        print "        Load \"type1\"";
        modsect = 0;
    }
}

{
    print;
}
' $i >${D}/tmp/XF86Config.$$
	cp ${D}/tmp/XF86Config.$$ $i
    fi
done

if [ -e ${D}/usr/X11R6/lib/X11/locale/locale.alias ]; then
    einfo "    ${D}/usr/X11R6/lib/X11/locale/locale.alias"
    awk '
/^bg/ {
    next;
}

/^sr/ {
    next;
}

{
    print;
}

END {
    print "";
    print "bg:                            bg_BG.CP1251";
    print "bg_BG:                         bg_BG.CP1251";
    print "bg_BG.cp1251:                  bg_BG.CP1251";
    print "bg_BG.iso88595:                bg_BG.ISO8859-5";
    print "bg_BG.KOI8-R:                  ru_RU.KOI8-R";
    print "bg_BG.koi8r:                   ru_RU.KOI8-R";
    print "bg_BG.KOI8-U:                  ru_UA.KOI8-U";
    print "bg_BG.koi8u:                   ru_UA.KOI8-U";
    print "sr:                            sr_YU.ISO8859-2";
    print "sr_YU:                         sr_YU.ISO8859-2";
    print "sr_YU.iso88592:                sr_YU.ISO8859-2";
    print "sr@cyrillic:                   bg_BG.ISO8859-5";
    print "sr_YU@cyrillic:                bg_BG.ISO8859-5";
    print "sr_YU.ISO8859-5@cyrillic:      bg_BG.ISO8859-5";
    print "sr_YU.iso88595@cyrillic:       bg_BG.ISO8859-5";
    print "sr_YU.CP1251@cyrillic:         bg_BG.CP1251";
    print "sr_YU.cp1251@cyrillic:         bg_BG.CP1251";
}
' ${D}/usr/X11R6/lib/X11/locale/locale.alias > ${D}/tmp/locale.alias.$$
    cp -r ${D}/tmp/locale.alias.$$ ${D}/usr/X11R6/lib/X11/locale/locale.alias
fi
einfo "Installation completed successfully."

}

pkg_postinst() {
    einfo "Use : ${DD}/bin/set-bulgarian-env to configure"
}

pkg_config() {
    einfo "Use : ${DD}/bin/set-bulgarian-env to configure"
}