#!/bin/sh
#
# SliTaz Control Box is a tool to configure and manage a SliTaz system.
# The script uses GTKdialog for the UI interface. Some shell functions
# are called by argument. Individual window dialogs are put into 
# functions.
#
# (C) GNU gpl v3 - SliTaz GNU/Linux 2010.
# Author: Christophe Lincoln <pankso@slitaz.org>
#
VERSION=1.1

# Include gettext helper script.
. /usr/bin/gettext.sh

# Export package name for gettext.
TEXTDOMAIN='tazctrlbox'
export TEXTDOMAIN

# Get init configuration.
. /etc/rcS.conf

# Tazctrlbox is only for root.
if test $(id -u) != 0 ; then
	exec subox $0
	exit 0
fi

# By default autologin is not configured on an installed system.
if ! grep -q 'auto_login' /etc/slim.conf; then
	echo '# Auto login default user' >> /etc/slim.conf
	echo 'auto_login          no' >> /etc/slim.conf
fi

# Change Grub menu.lst timeout.
sed_grub_timeout()
{
	CURRENT=`cat /boot/grub/menu.lst | grep ^timeout | cut -d " " -f2`
	sed -i s/"timeout $CURRENT"/"timeout $GRUB_TIMEOUT"/ /boot/grub/menu.lst
}

# Change Grub menu.lst timeout.
sed_grub_color()
{
	CURRENT=`cat /boot/grub/menu.lst | grep ^color | cut -d " " -f2-3`
	sed -i s#"color $CURRENT"#"color $GRUB_COLOR"# /boot/grub/menu.lst
}

# Set check fs on boot.
sed_check_fs()
{
	sed -i s#"CHECK_FS=\"$CHECK_FS\""#"CHECK_FS=\"$NEW_CHECK_FS\""# \
		/etc/rcS.conf
}

# Set loaded modules on boot.
sed_load_modules()
{
	sed -i s/"LOAD_MODULES=\"$LOAD_MODULES\""/"LOAD_MODULES=\"$NEW_MODULES\""/ \
		/etc/rcS.conf
}

# Set daemons to run on boot.
sed_run_daemons()
{
	sed -i s/"RUN_DAEMONS=\"$RUN_DAEMONS\""/"RUN_DAEMONS=\"$NEW_DAEMONS\""/ \
		/etc/rcS.conf
}

# Get user list.
get_users()
{
	for i in `cat /etc/passwd | cut -d ":" -f 1`
	do
		if [ -d /home/$i ]; then
			login=$i
			uid=`cat /etc/passwd | grep $i | cut -d ":" -f 3`
			gid=`cat /etc/passwd | grep $i | cut -d ":" -f 4`
			name=`cat /etc/passwd | grep $i | cut -d ":" -f 5 | sed s/,,,//`
			home=`cat /etc/passwd | grep $i | cut -d ":" -f 6`
			shell=`cat /etc/passwd | grep $i | cut -d ":" -f 7`
			echo "system-users | $login | $uid:$gid | $name | $home | $shell"
		fi
	done
}

# Remove a user or change passwd.
manage_user()
{
	export MANAGE_DIALOG="
<window title=\"`gettext "Manage user: "`$USER\" icon-name=\"computer\">
	<vbox>
		<vbox>
			<text wrap=\"false\" width-chars=\"34\">
				<label>\"
Login name: $USER
				\"</label>
			</text>
		</vbox>
		<hbox>
			<text wrap=\"false\">
				<label>\"`gettext "New password:"`\"</label>
			</text>
			<entry invisible_char=\"*\" visibility=\"false\" max_length=\"8\">
				<variable>PASSWD</variable>
			</entry>
			<button>
				<label>Change</label>
				<input file icon=\"forward\"></input>
				<action>echo \"$USER:$PASSWD\" | chpasswd -m</action>
				<action type=\"closewindow\">MANAGE_USER</action>
			</button>
		</hbox>
		<hbox>
			<button>
				<label>`gettext "Delete user"`</label>
				<input file icon=\"gtk-delete\"></input>
				<action>deluser $USER</action>
				<action type=\"closewindow\">MANAGE_USER</action>
			</button>
			<button cancel>
				<action type=\"closewindow\">MANAGE_USER</action>
			</button>
		</hbox>
	</vbox>
</window>
"
	gtkdialog --center --program=MANAGE_DIALOG >/dev/null
}

# Add a new user.
add_user()
{
	export ADD_USER_DIALOG='
<window title="New user" icon-name="gtk-add">
	<vbox>
		<vbox>
			<text wrap="false" width-chars="34">
				<label>"'`gettext "
New account information"`'
				"</label>
			</text>
		</vbox>
		<hbox>
			<text wrap="false">
				<label>"'`gettext "Login:"`'      "</label>
			</text>
			<entry>
				<variable>NEW_USER</variable>
			</entry>
		</hbox>
		<hbox>
			<text wrap="false">
				<label>"'`gettext "Password:"`'"</label>
			</text>
			<entry invisible_char="*" visibility="false" max_length="8">
				<variable>PASSWD</variable>
			</entry>
		</hbox>
		<hbox>
			<button ok>
				<action>adduser -D $NEW_USER</action>
				<action>echo "$NEW_USER:$PASSWD" | chpasswd -m</action>
				<action>addgroup $NEW_USER audio</action>
				<action>addgroup $NEW_USER cdrom</action>
				<action>addgroup $NEW_USER floppy</action>
				<action>addgroup $NEW_USER video</action>
				<action>rmdir /home/$NEW_USER</action>
				<action>cp -a /etc/skel /home/$NEW_USER</action>
				<action>cp /root/.xinitrc /home/$NEW_USER</action>
				<action>mkdir -p /home/$NEW_USER/.config/slitaz</action>
				<action>cp -a /etc/slitaz/applications.conf /home/$NEW_USER/.config/slitaz</action>
				<action>chown -R $NEW_USER:$NEW_USER /home/$NEW_USER</action>
				<action type="closewindow">MANAGE_USER</action>
			</button>
			<button cancel>
				<action type="closewindow">MANAGE_USER</action>
			</button>
		</hbox>
	</vbox>
</window>
'
	gtkdialog --center --program=ADD_USER_DIALOG >/dev/null	
}

# i18n functions.
list_locales()
{
	cd /usr/share/i18n/locales
	for locale in `ls -1 [a-z][a-z]_[A-Z][A-Z]`
	do
		echo "preferences-desktop-locale | $locale | UTF-8"
	done
}
gen_utf8_locale()
{
	rm -rf /usr/lib/locale/$LANGUAGE
	localedef -i $LANGUAGE -c -f UTF-8 /usr/lib/locale/$LANGUAGE
	# System configuration
	echo "LANG=$LANGUAGE" > /etc/locale.conf
	echo "LC_ALL=$LANGUAGE" >> /etc/locale.conf
}

# Main dialog with notebook.
#
export MAIN_DIALOG='
<window title="'`gettext "SliTaz Control Box"`'" icon-name="computer">
<vbox>

	<hbox>
		<text use-markup="true">
			<label>"'`gettext "<b>SliTaz Control Box</b>"`'"</label>
		</text>
		<pixmap>
			<input file>/usr/share/pixmaps/tazctrlbox.png</input>
		</pixmap>
	</hbox>

	<notebook labels="'`gettext "Boot loader|Initialization|Login manager|Time|Language|Users"`'">'
	
# GRUB.
MAIN_DIALOG=${MAIN_DIALOG}"
	<vbox>
		<frame `gettext "Grub boot loader"`>
			<hbox>
				<text wrap=\"false\">
					<label>\"`gettext "Timeout:"`\"</label>
				</text>
				<entry>
					<input>cat /boot/grub/menu.lst | grep ^timeout | cut -d \" \" -f2</input>
					<variable>GRUB_TIMEOUT</variable>
				</entry>
				<button>
					<label>`gettext "Change"`</label>
					<input file icon=\"forward\"></input>
					<action>$0 sed_grub_timeout</action>
				</button>
			</hbox>
			<hbox>
				<text wrap=\"false\">
					<label>\"`gettext "Color:"`    \"</label>
				</text>
				<entry>
					<input>cat /boot/grub/menu.lst | grep ^color | cut -d \" \" -f2-3</input>
					<variable>GRUB_COLOR</variable>
				</entry>
				<button>
					<label>`gettext "Change"`</label>
					<input file icon=\"forward\"></input>
					<action>$0 sed_grub_color</action>
				</button>
			</hbox>
			<hbox>
				<text wrap=\"false\">
					<label>\"`gettext "Configuration file:"`\"</label>
				</text>
				<button>
					<label>/boot/grub/menu.lst</label>
					<input file icon=\"accessories-text-editor\"></input>
					<action>leafpad /boot/grub/menu.lst</action>
					<action>refresh:GRUB_COLOR</action>
					<action>refresh:GRUB_TIMEOUT</action>
				</button>
			</hbox>
		</frame>
	</vbox>"
# Init script.
MAIN_DIALOG=${MAIN_DIALOG}"
	<vbox>
		<frame `gettext "rcS init scripts"`>
			<hbox>
				<text wrap=\"false\">
					<label>\"`gettext "Check filesystems:"`\"</label>
				</text>
				<entry>
					<input>echo $CHECK_FS</input>
					<variable>NEW_CHECK_FS</variable>
				</entry>
				<button>
					<label>Change</label>
					<input file icon=\"forward\"></input>
					<action>$0 sed_check_fs</action>
				</button>
			</hbox>
			<hbox>
				<text wrap=\"false\">
					<label>\"`gettext "Load modules:"`      \"</label>
				</text>
				<entry>
					<input>echo $LOAD_MODULES</input>
					<variable>NEW_MODULES</variable>
				</entry>
				<button>
					<label>`gettext "Change"`</label>
					<input file icon=\"forward\"></input>
					<action>$0 sed_load_modules</action>
				</button>
			</hbox>
			<hbox>
				<text wrap=\"false\">
					<label>\"`gettext "Run daemons:"`       \"</label>
				</text>
				<entry>
					<input>echo $RUN_DAEMONS</input>
					<variable>NEW_DAEMONS</variable>
				</entry>
				<button>
					<label>`gettext "Change"`</label>
					<input file icon=\"forward\"></input>
					<action>$0 sed_run_daemons</action>
				</button>
			</hbox>
			<hbox>
				<text wrap=\"false\">
					<label>\"`gettext "Add local commands:"`\"</label>
				</text>
				<button>
					<label>/etc/init.d/local.sh</label>
					<input file icon=\"accessories-text-editor\"></input>
					<action>leafpad /etc/init.d/local.sh</action>
				</button>
			</hbox>
		</frame>
	</vbox>"
# Slim login.
MAIN_DIALOG=${MAIN_DIALOG}'
	<vbox>
		<frame '`gettext "Slim settings"`'>
			<hbox>
				<text wrap="false">
					<label>"'`gettext "Sessions:"`'                "</label>
				</text>
				<entry>
					<input>cat /etc/slim.conf | grep ^session | sed s/"sessions. *"//</input>
					<variable>SLIM_SESSIONS</variable>
				</entry>
				<button>
					<label>'`gettext "Change"`'</label>
					<input file icon="forward"></input>
					<action>sed -i "s/^sessions.*/sessions            $SLIM_SESSIONS/" /etc/slim.conf</action>
				</button>
			</hbox>
			<hbox>
				<text wrap="false">
					<label>"'`gettext "Default user:"`'           "</label>
				</text>
				<entry>
					<input>cat /etc/slim.conf | grep ^default_user | sed s/"default_user. *"//</input>
					<variable>SLIM_DEF_USER</variable>
				</entry>
				<button>
					<label>'`gettext "Change"`'</label>
					<input file icon="forward"></input>
					<action>sed -i "s/^default_user.*/default_user        $SLIM_DEF_USER/" /etc/slim.conf</action>
				</button>
			</hbox>
			<hbox>
				<text wrap="false">
					<label>"'`gettext "Auto login (yes|no):"`' "</label>
				</text>
				<entry max_length="3">
					<input>cat /etc/slim.conf | grep ^auto_login | sed s/"auto_login. *"//</input>
					<variable>SLIM_AUTO_LOGIN</variable>
				</entry>
				<button>
					<label>'`gettext "Change"`'</label>
					<input file icon="forward"></input>
					<action>sed -i "s/^auto_login.*/auto_login          $SLIM_AUTO_LOGIN/" /etc/slim.conf</action>
				</button>
			</hbox>
			<hbox>
				<text wrap="false">
					<label>"'`gettext "Theme:"`'"</label>
				</text>
				<combobox>
					<variable>NEW_SLIM_THEME</variable>'
# List all installed Slim themes.
for dir in $(ls /usr/share/slim/themes)
do
	THEME_ITEMS="<item>$dir</item>"
	MAIN_DIALOG=${MAIN_DIALOG}${THEME_ITEMS}
done		
MAIN_DIALOG=${MAIN_DIALOG}'
				</combobox>	
				<button>
					<label>'`gettext "Preview"`'</label>
					<input file icon="video-display"></input>
					<action>slim -p /usr/share/slim/themes/$NEW_SLIM_THEME &</action>
				</button>
				<button>
					<label>'`gettext "Change"`'</label>
					<input file icon="forward"></input>
					<action>sed -i "s/^current_theme.*/current_theme       $NEW_SLIM_THEME/" /etc/slim.conf</action>
					<action>refresh:SLIM_THEME</action>
				</button>
			</hbox>
			<hbox>
				<text wrap="false">
					<label>"'`gettext "Configuration file:"`'"</label>
				</text>
				<button>
					<label>/etc/slim.conf</label>
					<input file icon="accessories-text-editor"></input>
					<action>leafpad /etc/slim.conf</action>
					<action>refresh:SLIM_SESSIONS</action>
					<action>refresh:SLIM_DEF_USER</action>
				</button>
			</hbox>
		</frame>
	</vbox>'
# Time settings.
MAIN_DIALOG=${MAIN_DIALOG}'	
	<vbox>
		<frame '`gettext "Date and time"`'>
			<hbox>
				<text wrap="false">
					<label>"'`gettext "System time:"`'   "</label>
				</text>
				<entry editable="false">
					<input>LC_ALL=C date</input>
					<variable>DATE</variable>
				</entry>
				<button>
					<label>'`gettext "Sync online"`'</label>
					<input file icon="reload"></input>
					<action>rdate -s tick.greyware.com</action>
					<action>refresh:DATE</action>
					<action>refresh:HWTIME</action>
				</button>
			</hbox>
			<hbox>
				<text wrap="false">
					<label>"'`gettext "Hardware time:"`'"</label>
				</text>
				<entry editable="false">
					<input>LC_ALL=C hwclock</input>
					<variable>HWTIME</variable>
				</entry>
				<button>
					<label>'`gettext "Set from system"`'</label>
					<input file icon="reload"></input>
					<action>hwclock -w -u</action>
					<action>refresh:HWTIME</action>
					<action>refresh:DATE</action>
				</button>
			</hbox>
			<hbox>
				<text wrap="true">
					<label>"'`gettext "Timezone:"`'       "</label>
				</text>
				<entry>
					<input>cat /etc/TZ</input>
					<variable>NEW_TZ</variable>
				</entry>
				<button>
					<label>'`gettext "Change"`'</label>
					<input file icon="forward"></input>
					<action>echo "$NEW_TZ" > /etc/TZ</action>
				</button>
			</hbox>
		</frame>
	</vbox>'
# Language settings.
MAIN_DIALOG=${MAIN_DIALOG}"
	<vbox>
		<tree>
			<width>600</width><height>210</height>
			<variable>LANGUAGE</variable>
			<label>`gettext "Language|Charmap"`</label>
			<input icon_column=\"0\">$0 list_locales</input>
			<action>$0 gen_utf8_locale</action>
		</tree>
		<hbox>
		<text width-chars=\"60\">
			<label>
\"`gettext "To change the system language you can double-click on the locale name."`\"
			</label>
		</text>
		<button>
			<label>`gettext "Keymap"`</label>
			<input file icon=\"input-keyboard\"></input>
			<action>tazkeymap &</action>
		</button>
	</hbox>
	</vbox>"
# Display users list through get_users.
MAIN_DIALOG=${MAIN_DIALOG}"
	<vbox>
		<tree>
			<width>600</width><height>210</height>
			<variable>USER</variable>
			<label>`gettext "Login|uid:gid|Name|Home|SHell"`</label>
			<input icon_column=\"0\">$0 get_users</input>
			<action>$0 manage_user</action>
			<action>refresh:USER</action>
		</tree>
		<hbox>
		<text width-chars=\"60\">
			<label>
\"`gettext "To change passwords or delete users you can double-click on the user name."`\"
			</label>
		</text>
		<button>
			<label>`gettext "Add newuser"`</label>
			<input file icon=\"gtk-add\"></input>
			<action>$0 add_user</action>
			<action>refresh:USER</action>
		</button>
	</hbox>
	</vbox>"
export MAIN_DIALOG=${MAIN_DIALOG}'	
	</notebook>

	<hbox>
		<button>
			<label>'`gettext "Network"`'</label>
			<input file icon="netbox"></input>
			<action>netbox &</action>
		</button>
		<button>
			<label>'`gettext "Wireless"`'</label>
			<input file icon="network-wireless"></input>
			<action>wifibox &</action>
		</button>
		<button>
			<label>'`gettext "Packages"`'</label>
			<input file icon="tazpkg"></input>
			<action>tazpkgbox &</action>
		</button>
		<button>
			<label>'`gettext "Hardware"`'</label>
			<input file icon="computer"></input>
			<action>tazhw box &</action>
		</button>
		<button>
			<label>'`gettext "Server"`'</label>
			<input file icon="utilities-system-monitor"></input>
			<action>serverbox &</action>
		</button>
		<button>
			<label>'`gettext "Storage"`'</label>
			<input file icon="media-flash"></input>
			<action>mountbox &</action>
		</button>
		<button>
			<label>'`gettext "Exit"`'</label>
			<input file icon="exit"></input>
			<action type="exit">Exit</action>
		</button>
	</hbox>

</vbox>

</window>'

# Script can be called with an arg to exec a function.
if [ -n "$1" ]; then
	$1
else
	gtkdialog --center --program=MAIN_DIALOG >/dev/null
fi

exit 0
