#!/bin/bash
# webuser.sh

# Creates user and there html-directorys, and can remove and change and backup all or specifik
# users files. 

WEBHOME="/www/users/"
OPTIONS="New Remove Change Quit"

# the menu for the add webuserscript! 
select opt in $OPTIONS; do
	if [ "$opt" = "New" ]; then
		echo "New webuser: "
		# fetch username for the new user. 		
		read USERNAME

		adduser $USERNAME
		mkdir $WEBHOME$USERNAME
		ln -s $WEBHOME$USERNAME /home/$USERNAME/.html
		chown $USERNAME:$USERNAME /home/$USERNAME/.html 
		chown -h $USERNAME:$USERNAME /home/$USERNAME/.html
		touch $WEBHOME$USERNAME/index.php
		chown $USERNAME:$USERNAME $WEBHOME$USERNAME/index.php
		echo "<? phpinfo(); ?>" > $WEBHOME$USERNAME/index.php
		echo "User has been added!"
		echo "Quiting program!"
		exit

	elif [ "$opt" = "Remove" ]; then
		echo "User to remove:"
		read USERNAME

		# Remove user from system.
		deluser $USERNAME
	
		# Remove directory from home-directory
		rm -rf /home/$USERNAME
		
		# backup the webdirectory, and save in /www/backup, and remove
		tar -cvf /www/backup/$USERNAME.tar $WEBHOME$USERNAME
		rm -rf $WEBHOME$USERNAME

		echo "It's done!"
		exit

	elif [ "$opt" = "Change" ]; then
		echo "Not implemented!"
		exit

	elif [ "$opt" = "Quit" ]; then
		echo "Welcome back!"
		exit
	else
		clear
		echo "bad option"
	fi
done

