#/bin/bash

if [ -z $1 ]
	then
		echo "Usage: install_lxde.sh <username>"
		exit 1
fi

echo "Installing LXDE, please wait, this will take a few minutes, depending on your computer and Internet connection"

echo -e "" | adduser $1 &> /dev/null

gpasswd -a $1 power > /dev/null

gpasswd -a $1 audio > /dev/null

pacman -Syu --noconfirm > /dev/null

pacman -S --noconfirm alsa-utils libgl xorg xf86-input-evdev xf86-video-vmware mesa hal dbus ttf-ms-fonts ttf-dejavu ttf-bitstream-vera lxde gamin openbox obconf obmenu gtk-chtheme gtk-engines wicd openssh firefox emesene ntp flashplugin

pacman -Scc --noconfirm > /dev/null

echo "exec startlxde" > /home/$1/.xinitrc

echo "x:5:once:/bin/su $1 -l -c \"/bin/bash --login -c startx >/dev/null 2>&1\"" >> /etc/inittab

sed -i 's/id:3:initdefault:/#id:3:initdefault:/g' /etc/inittab

sed -i 's/#id:5:initdefault:/id:5:initdefault:/g' /etc/inittab

sed -i 's/x:5:respawn:\/usr\/bin\/xdm -nodaemon/#x:5:respawn:\/usr\/bin\/xdm -nodaemon/g' /etc/inittab

echo "@setxkbmap pt" >> /etc/xdg/lxsession/LXDE/autostart 

echo "alias free='echo Disk Usage: && df -m && echo && echo Memory usage: && free -m && echo '" >> /home/$1/.bashrc

ntpdate ntp.ist.utl.pt > /dev/null

sed '$d' < /etc/PolicyKit/PolicyKit.conf > file

mv file /etc/PolicyKit/PolicyKit.conf

echo -e "<match action="org.freedesktop.hal.power-management.shutdown">\n <return result="yes"/>\n</match>\n<match action="org.freedesktop.hal.power-management.reboot">\n <return result="yes"/>\n</match>\n</config>" >> /etc/PolicyKit/PolicyKit.conf

if [ -z $2 ]

	then

		sed '$d' < /etc/rc.conf > file

		mv file /etc/rc.conf

		echo "DAEMONS=(@hal @alsa @wicd @syslog-ng @network @netfs @crond)" >> /etc/rc.conf

fi

echo Installation done! Rebooting in 5 seconds...

sleep 5

reboot

