#! /bin/sh 

# cf  http://www.linuxpedia.fr/doku.php/archlinux/installation


# Configuration de votre système

#Première chose à faire: créez un utilisateur et assignez le aux groupes dont il fera parti.
#Exemple :

PAC="pacman"

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

add_user(){
	echo ""
	echo ""
	echo -n "# type a new user and press [ENTER]:"
	read USER
  
	echo "# Adding user $USER ..."
	useradd -m -s /bin/bash $USER
	passwd $USER
	#usermod -G users,wheel,slocate,audio,optical,floppy,storage,video $USER
	#usermod -G users,wheel,audio,optical,floppy,storage,video $USER
	#adduser $USER admin
	
	# other
	#usermod -G users,wheel,slocate,audio,video,lp,floppy,optical,storage,scanner,power,log < nom_utilisateur >
	usermod -G users,wheel,audio,video,lp,floppy,optical,storage,scanner,power,log $USER

	pacman -S sudo --noconfirm
	echo "Added user $USER"
	echo ""
	echo "Adding $USER to sudoers"
	echo "">> /etc/sudoers
    echo "$USER ALL=(ALL) ALL" >> /etc/sudoers
	echo "$USER ALL=NOPASSWD: /usr/bin/pacman"  >> /etc/sudoers
	echo "$USER ALL=NOPASSWD: /usr/bin/pacdiffviewer"  >> /etc/sudoers
	echo "">> /etc/sudoers
}


install_Yaourt(){
	echo ""
	echo ""
	echo "# Installing Yaourt"
	echo ""
	echo "# appending to /etc/pacman.conf"
	 
	echo "" >> /etc/pacman.conf
	echo "[archlinuxfr]" >> /etc/pacman.conf
	echo "#Server = http://repo.archlinux.fr/x86_64" >> /etc/pacman.conf
	echo "Server = http://repo.archlinux.fr/i686" >> /etc/pacman.conf
	echo "" >> /etc/pacman.conf

	pacman -Sy --noconfirm
	pacman -S yaourt pacman-color --noconfirm
	pacman -S shaman --noconfirm

	
	#cf: http://wiki.archlinux.fr/man:archlinux:yaourtrc
	echo "#opening /etc/yaourtrc..."
	echo ""
	echo "#please uncheck :"
	echo ""
	echo "#PacmanBin /usr/bin/pacman-color"
	echo ""
	echo -n "# press [ENTER] to continue"
	read choice

	vi /etc/yaourtrc
}

init()
{
	echo ""
	echo ""
	echo "Please Enter which window manager you want to install ?"
	echo "----------------"
	echo "   1- yaourt"
	echo "   2- add a new user"
	echo "   A- INSTALL All"
	echo "   M- return Main Menu"
	echo "   0- exit"
	echo "----------------"
	echo -n "Enter your choice and press [ENTER]: "
	read choice
	
	
	if [ $choice -eq 1 ]; then
		install_Yaourt
		
	elif [ $choice -eq 2 ]; then
		add_user
	
	elif [ "$choice" == "A" ]; then
		install_Yaourt
		add_user
		
	elif [ "$choice" == "M" ]; then
		echo ""
		echo ""
		echo "Return to Main Menu ...\n"
		sh install.sh
	
	install_Yaourt
	
	elif [ $choice -eq "0" ]; then
		echo " exiting ...\n"
		exit 1
		
	else
		echo " Nothing done"
		echo ""
		echo "Retry again..."
		init
		#exit 1
	fi

	echo ""
	sh install.sh
}



init_dialog()
{
	C1(){ false;}
	C2(){ false;}
	C3(){ false;}
	C4(){ false;}
	C5(){ false;}
	C6(){ false;}
	C7(){ false;}
	C8(){ false;}
	C9(){ false;}
	C0(){ false;}
	CA(){ false;}
	CM(){ false;}

	{
	eval "$(
	dialog --separate-output --output-fd 3 \
	--checklist 'Please Enter which window manager you want to install ?' 0 0 0 \
	C1 yaourt				on \
	C2 add_a_new_user		on \
	CM return_Main_Menu		off \
	C0 exit 				off \
	3>&1 >&4 4>&- |
	sed 's/$/(){ true;}/')"
	} 4>&1

	if C0; then
		echo " exiting ...\n"
		exit 1
	fi
	
	if CM; then
		echo ""
		echo ""
		echo "Return to Main Menu ...\n"
		sh install.sh
	fi
	
	update_all
	
	if C1; then
		install_Yaourt
	fi
	if C2; then
		add_user
	fi

	sh install.sh

}


#init
init_dialog

