#!/bin/bash
# script by fanthom

# Switch to root
if [ `whoami` != "root" ]; then
    echo "Please enter root's password below"
    su - -c "language-selection-tool `whoami`"
    exit
fi

## Global variables

dir=/tmp/language-selection
storage=/tmp/storage
c='\e[36m'
r='\e[31m'
e=`tput sgr0`

# Check if script was run by normal user
if [ "$1" ]; then pth=home/$1; else pth=root; fi

# Clearing old files from previous run (if any)
rm -r $dir > /dev/null 2>&1
mkdir -p $dir/etc/profile.d
mkdir $storage > /dev/null 2>&1

# Moving files from $storage to /tmp if script is run in offline mode
#mv $storage/* /tmp > /dev/null 2>&1

## Starting the script
# Welcome message
clear
echo -e "${c}Script by fanthom,

Welcome in Language-Selection-Tool, this script will let you setup national language support in Porteus.
Those packages:
glibc-i18n, kde-i18n, koffice-l10n, k3b-i18n, locales-porteus - all around 50MB
needs to be downloaded to achieve the goal so active internet connection is required.
Be aware that answers are case sensitive (type 'a' and 'A' when necessary).

Press enter to proceed."$e
read
clear

# Gathering info
echo -e "${c}Do you want to setup UTF-8 encoding in your system? Answer y/n"$e
read utf
if [ $utf = "y" ]; then
cat /usr/doc/language/locale-utf8.txt
echo
echo -e "${c}This is the list of supported encodings please type in your choice:

Example:
pl_PL.utf8      for polish UTF-8 encoding"$e
echo
read enc
ans=`grep $enc /usr/doc/language/locale-utf8.txt`
	while [ ! "$enc" = "$ans" ]; do
	echo
	echo -e "${r}selected encoding: $enc is not on the list, please try once again"$e
	echo
	read enc
	unset ans
	ans=`grep $enc /usr/doc/language/locale-utf8.txt`
	done
else
cat /usr/doc/language/locale-non-utf8.txt
echo
echo -e "${c}This is the list of supported encodings please type in your choice:

Example:
pl_PL		for polish traditional encoding"$e
echo
read enc
ans=`grep -m1 $enc /usr/doc/language/locale-non-utf8.txt`
	while [ ! "$enc" = "$ans" ]; do
	echo
	echo -e "${r}selected encoding: $enc is not on the list, please try once again"$e
	echo
	read enc
	unset ans
	ans=`grep -m1 $enc /usr/doc/language/locale-non-utf8.txt`
	done
fi

##Preparing package
cd /tmp
# Adding content of glibc-i18n package
echo
echo -e "${r}glibc-i18n is required to complete the task, press enter to download it now"$e
echo
read
wget -c http://ponce.cc/porteus/i486/current/packages/Language-Selection-Tool/glibc-i18n-i486-1ftm.xzm
unsquashfs glibc-i18n-i486-1ftm.xzm
cp -r squashfs-root/var $dir
cd squashfs-root
cp -r --parents usr/lib/locale/$enc $dir
cp -r --parents usr/share/i18n/charmaps $dir
cp --parents usr/share/i18n/locales/POSIX $dir
cp --parents usr/share/locale/locale.alias $dir
last=`echo $enc | cut -d '.' -f 1`
cp usr/share/i18n/locales/$last $dir/usr/share/i18n/locales
cd ..
mv glibc-i18n-i486-1ftm.xzm $storage
rm -r squashfs-root

# System settings
echo '#!/bin/bash' > $dir/etc/profile.d/lang.sh
echo "# File generated by fanthom's Language-Selection tool" >> $dir/etc/profile.d/lang.sh
echo "" >> $dir/etc/profile.d/lang.sh
echo "export LC_ALL=$enc" >> $dir/etc/profile.d/lang.sh
echo "export LANG=$enc" >> $dir/etc/profile.d/lang.sh
echo "export LC_COLLATE=C" >> $dir/etc/profile.d/lang.sh
chmod +x $dir/etc/profile.d/lang.sh

## keyboard layout
# console
clear
echo -e "${c}Please type in name of your preferred keymap which will be used in the console mode (not under X).
All keyboard maps are stored in /usr/share/kbd/keymaps/i386 folder.
To get more details about each map press F4 in MC on selected file."$e
echo -e "${r}If you are not going to work in console mode, you can skip this step by leaving it empty and pressing enter."$e
echo
echo -e "${c}Example (for polish users):
pl2.map"$e
echo
read keymap
if [ ! $keymap = "" ]; then
ans=`find /usr/share/kbd/keymaps/i386/ -iname "$keymap" | cut -d '/' -f 8`
	while [ ! "$keymap" = "$ans" ]; do
	echo
	echo -e "${r}Couldnt find selected keymap: $keymap. Perhaps misspelled - please try once again"$e
	echo
	read keymap
	unset ans
	ans=`find /usr/share/kbd/keymaps/i386/ -iname "$keymap" | cut -d '/' -f 8`
	done
mkdir $dir/etc/rc.d
echo "#!/bin/sh
# Load the keyboard map.  More maps are in /usr/share/kbd/keymaps.
if [ -x /usr/bin/loadkeys ]; then
/usr/bin/loadkeys $keymap
fi" >> $dir/etc/rc.d/rc.keymap
chmod +x $dir/etc/rc.d/rc.keymap
echo
echo -e "${c}Please type in name of preferred font which will be used in the console mode.
Remember: selected font must have a support for selected keymap!
All console fonts are stored in /usr/share/kbd/consolefonts folder.

Example (for polish users)
lat2a-16.psfu"$e
echo
read font
ans=`find /usr/share/kbd/consolefonts -iname $font | sed 's@/partialfonts@@g' | cut -d '/' -f 6`
	while [ ! "$font" = "$ans" ]; do
	echo
	echo -e "${r}Couldnt find selected font: $font. Perhaps misspelled - please try once again"$e
	echo
	read font
	unset ans
	ans=`find /usr/share/kbd/consolefonts -iname $font | sed 's@/partialfonts@@g' | cut -d '/' -f 6`
	done
cp /etc/rc.d/rc.font $dir/etc/rc.d/rc.font
sed -i $dir/etc/rc.d/rc.font -e '6s/.*/setfont -v choice/'
sed -i $dir/etc/rc.d/rc.font -e s/choice/$font/g
chmod +x $dir/etc/rc.d/rc.font
fi

# LXDE
kbd=`echo $enc | cut -d "_" -f1`
mkdir -p $dir/$pth/.config/autostart
echo "[Desktop Entry]
Type=Application
Exec=setxkbmap -layout $kbd,us -option grp:alt_shift_toggle" > $dir/$pth/.config/autostart/xkb.desktop

# KDE
echo
if [ -x /usr/bin/kcmshell ]; then
echo
echo -e "${c}Please adjust the KDE (X mode) keyboard layout settings NOW
and don't forget to 'Apply' (save) your setup!
then click 'OK' to continue!...

Press enter when done"$e | fmt -w 80
sleep 2
kcmshell kcm_keyboard
read
mkdir -p $dir/$pth/.kde/share/config
cat /root/.kde/share/config/kxkbrc > $dir/$pth/.kde/share/config/kxkbrc
fi

if [ "$1" ]; then chown -R $1:$1 $dir/$pth; fi

## Windows fs support
clear
echo -e "${c}Do you want to setup automount for NTFS and FAT partitons with nls in your system? Answer y/n"$e
read part
if [ $part = "y" ]; then

# NTFS-3G automount
mkdir $dir/sbin
echo '#!/bin/bash' > $dir/sbin/mount.ntfs-3g
echo "# File generated by fanthom's Language-Selection tool" >> $dir/sbin/mount.ntfs-3g
echo '/bin/ntfs-3g $1 "$2" -o locale=' >> $dir/sbin/mount.ntfs-3g
sed -i $dir/sbin/mount.ntfs-3g -e s/=/=$enc/g
chmod +x $dir/sbin/mount.ntfs-3g
echo
echo -e "${c}NTFS partitions will be automounted with $enc encoding"$e

# FAT automount
	if [ $utf = "y" ]; then
	fat=utf8
	else
	echo
	echo -e "${c}Press enter to check the list of supported iocharsets for FAT filesystem:"$e
	read
	echo
	cat /usr/doc/language/fat.txt
	echo
	echo -e "${c}Type in your choice:"$e
	echo
	read fat
	ans=`grep -m1 $fat /usr/doc/language/fat.txt`
		while [ ! "$fat" = "$ans" ]; do
		echo
		echo -e "${r}selected iocharset: $fat is not on the list, please try once again"$e
		echo
		read fat
		unset ans
		ans=`grep -m1 $fat /usr/doc/language/fat.txt`
		done
	fi
echo '#!/bin/bash' > $dir/sbin/mount.vfat
echo "# File generated by fanthom's Language-Selection tool" >> $dir/sbin/mount.vfat
echo 'mv /sbin/mount.vfat /sbin/mount.vfat-tmp' >> $dir/sbin/mount.vfat
echo '/bin/mount $1 "$2" -o iocharset=' >> $dir/sbin/mount.vfat
sed -i $dir/sbin/mount.vfat -e s/=/=$fat/g
echo 'mv /sbin/mount.vfat-tmp /sbin/mount.vfat' >> $dir/sbin/mount.vfat
chmod +x $dir/sbin/mount.vfat
echo
echo -e "${c}FAT partitions will be automounted with $fat iocharset
press enter to proceed"$e
read
fi

## Adding extra language files
# kde-i18n
clear
echo -e "${c}Press enter to check the list of supported languages in KDE (kde-i18n):"$e
read
cat /usr/doc/language/kde-i18n.txt
echo
echo -e "${c}Type in your choice or press enter to skip those lang files:"$e
echo
read i18n
if [ ! $i18n = "" ]; then
ans=`cat /usr/doc/language/kde-i18n.txt | awk '{print $1}' | grep -m1 $i18n`
	while [ ! "$i18n" = "$ans" ]; do
	echo
	echo -e "${r}selected language: $i18n is not on the list, please try once again"$e
	echo
	read i18n
	unset ans
	ans=`cat /usr/doc/language/kde-i18n.txt | awk '{print $1}' | grep -m1 $i18n`
	done
echo
wget -c ftp://ftp.heanet.ie/mirrors/ftp.slackware.com/pub/slackware/unsupported/kde-3.5.10-for-slack13.0/packages/kdei/kde3-i18n-$i18n-3.5.10-noarch-opt2.txz
txz2xzm kde3-i18n-$i18n-3.5.10-noarch-opt2.txz kde3-i18n-$i18n-3.5.10-noarch-opt2.xzm
unsquashfs kde3-i18n-$i18n-3.5.10-noarch-opt2.xzm
cp -r squashfs-root/var $dir
cp -r squashfs-root/opt/kde3/share/apps $dir/usr/share
cp -r squashfs-root/opt/kde3/share/locale $dir/usr/share
mv kde3-i18n-$i18n-3.5.10-noarch-opt2.xzm $storage
rm kde3-i18n-$i18n-3.5.10-noarch-opt2.txz 
rm -r squashfs-root
fi

# KOffice
clear
echo
echo -e "${c}Press enter to check the list of supported languages in KOffice:"$e
read
cat /usr/doc/language/KOffice.txt
echo
echo -e "${c}Type in your choice or press enter to skip those lang files:"$e
echo
read kof
if [ ! $kof = "" ]; then
ans=`cat /usr/doc/language/KOffice.txt | awk '{print $1}' | grep -m1 $kof`
	while [ ! "$kof" = "$ans" ]; do
	echo
	echo -e "${r}selected language: $kof is not on the list, please try once again"$e
	echo
	read kof
	unset ans
	ans=`cat /usr/doc/language/KOffice.txt | awk '{print $1}' | grep -m1 $kof`
	done
echo
wget -c ftp://ftp.heanet.ie/mirrors/ftp.slackware.com/pub/slackware/unsupported/kde-3.5.10-for-slack13.0/packages/kdei/koffice-kde3-l10n-$kof-1.6.3-noarch-opt2.txz
txz2xzm koffice-kde3-l10n-$kof-1.6.3-noarch-opt2.txz koffice-kde3-l10n-$kof-1.6.3-noarch-opt2.xzm
unsquashfs koffice-kde3-l10n-$kof-1.6.3-noarch-opt2.xzm
cp -r squashfs-root/var $dir
cp -r squashfs-root/opt/kde3/* $dir/usr
mv koffice-kde3-l10n-$kof-1.6.3-noarch-opt2.xzm $storage
rm koffice-kde3-l10n-$kof-1.6.3-noarch-opt2.txz 
rm -r squashfs-root
fi

# K3B
clear
echo
echo -e "${c}Press enter to check the list of supported languages in K3B:"$e
read
cat /usr/doc/language/k3b.txt
echo
echo -e "${c}Type in your choice or press enter to skip those lang files:"$e
echo
read k3b
if [ ! $k3b = "" ]; then
ans=`grep -m1 $k3b /usr/doc/language/k3b.txt`
	while [ ! "$k3b" = "$ans" ]; do
	echo
	echo -e "${r}selected language: $k3b is not on the list, please try once again"$e
	echo
	read k3b
	unset ans
	ans=`grep -m1 $k3b /usr/doc/language/k3b.txt`
	done
echo
wget -c ftp://ftp.heanet.ie/mirrors/ftp.slackware.com/pub/slackware/unsupported/kde-3.5.10-for-slack13.0/packages/kdei/k3b3-i18n-1.0.5-noarch-1.txz
txz2xzm k3b3-i18n-1.0.5-noarch-1.txz k3b3-i18n-1.0.5-noarch-1.xzm
unsquashfs k3b3-i18n-1.0.5-noarch-1.xzm
cp -r squashfs-root/var $dir
cp -r squashfs-root/opt/kde3/share/locale/$k3b $dir/usr/share/locale
mv k3b3-i18n-1.0.5-noarch-1.xzm $storage
rm k3b3-i18n-1.0.5-noarch-1.txz 
rm -r squashfs-root
fi

# system
clear
echo
echo -e "${c}Press enter to check the list of supported languages in whole system (Midnight Commander, gpicview, BMP etc.):"$e
read
cat /usr/doc/language/system.txt
echo
echo -e "${c}Type in your choice or press enter to skip those lang files:"$e
echo
read sys
if [ ! $sys = "" ]; then
ans=`grep -m1 $sys /usr/doc/language/system.txt`
	while [ ! "$sys" = "$ans" ]; do
	echo
	echo -e "${r}selected language: $sys is not on the list, please try once again"$e
	echo
	read sys
	unset ans
	ans=`grep -m1 $sys /usr/doc/language/system.txt`
	done
echo
wget -c http://ponce.cc/porteus/i486/current/packages/Language-Selection-Tool/locales-porteus-i486-1ftm.xzm
unsquashfs locales-porteus-i486-1ftm.xzm
cp -r squashfs-root/locale/$sys $dir/usr/share/locale
fi

# man
clear
echo
echo -e "${c}Press enter to check the list of man pages with national language support:"$e
read
cat /usr/doc/language/man.txt
echo
echo -e "${c}Type in your choice or press enter to skip those lang files:"$e
echo
read man
if [ ! $man = "" ]; then
ans=`grep -m1 $man /usr/doc/language/man.txt`
	while [ ! "$man" = "$ans" ]; do
	echo
	echo -e "${r}selected language: $man is not on the list, please try once again"$e
	echo
	read man
	unset ans
	ans=`grep -m1 $man /usr/doc/language/man.txt`
	done
echo
mkdir $dir/usr/man > /dev/null 2>&1
cp -r squashfs-root/man/$man $dir/usr/man
rm -r squashfs-root
fi
mv locales-porteus-i486-1ftm.xzm $storage

# Making xzm module
clear
echo -e "${c}press enter to create xzm module from all files which you have selected"$e
read
dir2xzm $dir /root/locales-$enc.xzm

## Final message
clear
echo -e "${c}All required files are packed up in /root/locales-$enc.xzm module, move it to your /porteus/modules directory and reboot Porteus."$e
offline=`cat /usr/bin/language-selection-tool | head -n 50 | grep "offline-mode"`
if [ "$offline" = "" ]; then
echo -e "${r}All downloaded packages are placed in $storage folder for later usage.
If something went wrong you can run this script again without re-downloading of files.
You will save the time and bandtwidth on downloading and converting those packages once again."$e
echo
echo -e "${c}Do you want me to convert LST to the offline version now? Answer y/n"$e
echo
read version
if [ $version = "y" ]; then
sed -i /usr/bin/language-selection-tool -e 's/^wget/#wget/g'
sed -i /usr/bin/language-selection-tool -e 's/^#mv/mv/g'
head -n 21 /usr/bin/language-selection-tool > /tmp/language-selection-tool
echo -e 'echo -e "${c}Script by fanthom,

LST is working in the 'offline-mode' now. You wont be able to download any new package through it anymore.
You can always revert this change by issuing following command in the console:
cp /mnt/live/memory/images/001-core.xzm/usr/bin/language-selection-tool /usr/bin
Be aware that all modules will be moved from $storage to /tmp folder, which is default for making operations.

press enter to proceed"$e' >> /tmp/language-selection-tool
sed -n -e '31,$p' /usr/bin/language-selection-tool >> /tmp/language-selection-tool
mv /tmp/language-selection-tool /usr/bin
chmod +x /usr/bin/language-selection-tool
echo 
echo -e "${r}from now on LST will be working in the 'offline mode'"$e
echo
fi
fi
echo
echo -e "${c}Press enter to go directly to the /root directory.
Thanks for using my tool.

Regards,
fanthom"$e
read
konqueror /root &
