#!/bin/sh

# ArchLinux materiel installer
#
# Authors:
#  Brzulovic Misel <brzulovic@gmail.com>
# License: GPLv2 or later


## APPLICATION
# see http://wiki.archlinux.org/index.php/Useful_Applications
# http://www.linuxpedia.fr/doku.php/archlinux/installation

#PAC="yaourt"
PAC="pacman"

update_all()
{
	# on update de la base 
	
	echo ""
	echo "# database update "
	echo $PAC -Sy --noconfirm
}

install_X(){
	echo ""
	echo "# Installing Xorg"
	echo $PAC -S xorg --noconfirm
	#pacman -S xterm xorg-server  xorg-xkb-utils  xorg-xauth xorg-server-utils  xorg-xinit  xf86-video-vesa xf86-input-mouse  xf86-input-keyboard xorg-res-utils xorg-twm xorg-xinit xorg-fonts-100dpi xorg-fonts-75dpi --noconfirm
	# simplify xorg.conf
	
	
	#echo ""
	#echo "#Installing hwd for xorg.conf"
	#echo $PAC -S hwd --noconfirm
	#echo ""
	#echo "#Generatin Xorg.conf"
	#echo hwd -xa
	
	echo ""
	echo "#copying and Installing hwd for xorg.conf"
	X -configure
	#Xorg --configure
	mv /root/xorg.conf.new /etc/X11/xorg.conf
	
}


install_Fonts(){
	echo ""
	echo "#Installing Fonts"
	#  install FONTS 
	echo $PAC -S ttf-dejavu ttf-ms-fonts ttf-liberation --noconfirm
}


install_Audio(){
	#-------- SON
	echo ""
	echo "#Installing Audio"
	echo $PAC -S alsa-lib alsa-utils --noconfirm
	echo ""
	echo "#Testing Audio"
	echo alsaconf
	echo $PAC -S pciutils --noconfirm
	echo ""
	echo "# Si votre carte son n'est pas détectée, vérifiez que vous avez pciutils (pacman -S pciutils)."
	echo "# taper"
	echo "# alsamixer"
	echo "# alsactl store"
}


install_Perif(){
	echo ""
	echo "# Installing Peripheriques"
	#Gestion des périphériques
	echo $PAC -S hal dbus --noconfirm
	echo ""
	echo "# ajouter le démon hal dans /etc/rc.conf : "
	echo "# DAEMONS=(syslog-ng network netfs crond @hal @alsa @sshd)"
}

install_Xinitrc(){
	echo ""
	echo "# Installing xinitrc"
	echo ""
	echo "# copie du fichier xinitrc"
	echo cp {/etc/skel/,~/}.xinitrc
	echo ""
	echo "# vi ~/.xinitrc"
}

install_ssh(){

	### Configuration a distance (optionnel)
	# Si vous aimez bien faire un copier coller à la souris, je vous propose d'installer openssh:
	echo $PAC -S openssh --noconfirm 

	echo "# Editez le fichier /etc/hosts.allow comme suit: "
	echo ""
	echo "# sshd: ALL"
	echo "# ALL:ALL:DENY"
	echo ""
	echo "# pour lancer le démon ssh"
	echo ""
	echo "# /etc/rc.d/sshd start"
	echo ""
	echo "# Si votre réseau fonctionne vous pouvez maintenant vous connecter à distance à votre nouvelle install !"
	echo "# ssh root@Adresse_IP_de_ma_super_install"
}



init()
{
	echo 
	echo 
	echo "Please Enter which window manager you want to install ?"
	echo "----------------"
	echo "   1- X"
	echo "   2- Fonts"
	echo "   3- Audio"
	echo "   4- Peripheriques"
	echo "   5- Xinitrc"
	echo "   6- openssh"
	echo "   A- INSTALL All"
	echo "   M- return Main Menu"
	echo "   0- exit"
	echo "----------------"
	echo -n "Enter your choice and press [ENTER]: "
	read choice


	# on update la base 
	###########################################
	echo ""
	echo ""
	echo "DataBase update"
	pacman -Sy 

	if [ $choice -eq 1 ]; then
		install_X
		
	elif [ $choice -eq 2 ]; then
		install_Fonts
		
	elif [ $choice -eq 3 ]; then
		install_Audio
		
	elif [ $choice -eq 4 ]; then
		install_Perif
		
	elif [ $choice -eq 5 ]; then
		install_Xinitrc

	elif [ $choice -eq 6 ]; then
		install_ssh

	elif [ "$choice" -eq "A" ]; then
		install_X
		install_Fonts
		install_Audio
		install_Perif
		install_Xinitrc
		install_ssh
		
	elif [ "$choice" -eq "M" ]; then
		echo 
		echo ""
		echo "Return to Main Menu ...\n"
		sh install.sh

	elif [ $choice -eq 0 ]; then
		echo " exiting ...\n"
		exit 1
		
	else
		echo "Nothing done !!"
		echo "Retry again..."
		init
		#exit 1
	fi

	echo ""
	sh install.sh
}


init_dialog()
{


	M1(){ false;}
	M2(){ false;}
	M3(){ false;}
	M4(){ false;}
	M5(){ false;}
	M6(){ false;}
	M7(){ false;}
	M8(){ false;}
	M9(){ false;}
	M0(){ false;}
	MA(){ false;}
	MM(){ false;}
	
	{
	eval "$(
	dialog --separate-output --output-fd 3 \
	--checklist 'Please Enter which window manager you want to install ?' 0 0 0 \
	M1 X				on \
	M2 Fonts			on \
	M3 Audio			on \
	M4 Peripheriques	on \
	M5 Xinitrc			on \
	M6 openssh 			on \
	MM return_Main_Menu	off \
	M0 exit 			off \
	3>&1 >&4 4>&- |
	sed 's/$/(){ true;}/')"
	} 4>&1

	if M0; then
		echo " exiting ...\n"
		exit 1
	fi
	
	if MM; then
		echo ""
		echo ""
		echo "Return to Main Menu ...\n"
		sh install.sh
	fi
	
	$(update_all)
	
	if M1; then
		$(install_X)
	fi
	if M2; then
		$(install_Fonts)
	fi
	if M3; then
		$(install_Audio)
	fi
	if M4; then
		$(install_Perif)
	fi
	if M5; then
		$(install_Xinitrc)
	fi
	if M6; then
		$(install_ssh)
	fi

	sh install.sh

}

init_dialog_text()
{
	update_all
	install_X
	install_Fonts
	install_Audio
	install_Perif
	install_Xinitrc
	install_ssh
}

#init
init_dialog
#init_dialog_text

