#!/bin/msh
# wizzyconf.sh - DSLinux configuration wizard - part of the wizzy-suite
# Licence: GNU GPL v3 or later at your option
# Author:  Florian Bruhin (The Compiler) <florianbruh@gmail.com>
version="0.01r0812010"
# Copyright 2008 Florian Bruhin

# This program is part of the Wizzy-suite
#
# The Wizzy-suite is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# The Wizzy-suite is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with the Wizzy-suite.  If not, see <http://www.gnu.org/licenses/>.

# FIXME TODO: Check $? etc. for errors
# Angerfist - Ubermensch

#=============================================================#
#=============================================================#

NEWLINE="
"
OLDIFS="$IFS"

echo "$*" | grep -q "debug"
if [ "$?" != 0 ]; then
	dialog --infobox "Please wait..." 0 0

	a=`/bin/false`
	if [ $? != 1 ]; then
		dialog --stdout --backtitle "WizzyConf" --title "Warning!" --ok-label "Damn!" --msgbox "Unpatched msh detected, please install a recent DSLinux-version from www.dslinux.org - aborting" 0 0
		exit 1
	fi

	if [ ! -s /etc/rc.conf ]; then
		dialog --stdout --backtitle "WizzyConf" --title "Warning!" --yesno "/etc/rc.conf doesn't exist yet. Should it get created?" 0 0
		if [ $? = 1 ]; then
			dialog --stdout --backtitle "WizzyConf" --title "Warning!" --msgbox "Okay, aborting"
			exit 1
		else
			if [ -s /etc/rc.defaults ]; then 
				cp /etc/rc.defaults /etc/rc.conf
			else
				dialog --stdout --backtitle "WizzyConf" --title "Warning!" --ok-label "Damn!" --msgbox "/etc/rc.defaults doesn't exist - aborting"
				exit 1
			fi
	

		fi
	fi
fi
if echo "$*" | grep "trace"; then
	set -x
fi
if echo "$*" | grep "print"; then
	set -v
fi

if echo "$*" | grep "noexec"; then	
	set -n
fi
func=edituser
while :
do
[ "$1" = "debug" ] && echo $func # FIXME comment out testing only
[ "$1" = "debug" ] && sleep 1 # FIXME comment out testing only
case $func in

#==============================================================#
start)
#==============================================================#
	dlginput=`dialog --stdout --backtitle "WizzyConf" --title "Mainmenu" --cancel-label "Exit" --menu "What do you want to configure?" 0 0 0 \
	"1)" "Users" \
	"2)" "Network" \
	"3)" "Misc." \
	"" "" \
	"" "About Wizzy"`

	case $? in
		1|255) clear; exit ;;
	esac

	case $dlginput in
		1\)) func=users ;;
		2\)) func=network ;;
		3\)) func=misc ;;
		"") func=about ;;
	esac
	;;
#==============================================================#
users)
#==============================================================#

	dlginput=`dialog --stdout --backtitle "WizzyConf" --title "Users" --menu "" 0 0 0 \
	"1)" "Add user" \
	"2)" "Delete user" \
	"3)" "Edit user" \
	"4)" "List users" \
	"5)" "Autologin" \
	--infobox "Please wait..." 0 0`

	case $? in
		1|255) func=start ;;
	esac

	case $dlginput in
		1\)) func=adduser ;;
		2\)) func=deluser ;;
		3\)) func=edituser ;;
		4\)) func=userlist ;;
		5\)) func=autologin ;;
	esac

	dlginput=
	;;

#===========================#
edituser)
#===========================#
	# TODO: Do a menu what to edit (read from $line maybe, not re-reading everything)
	# like that:
	
	### What so you want to edit? ###
	# Username
	# Password
	## User-Id (UID)
	## Group-ID (GID)
	# Realname/Comment
	## Shell
	## Home-Dir
	# set autologin to this user

	##: Only if expert-mode/bit is activated
	# FIXME 081206 rewrite the various promts to be separate functions, so cancel can be solved easly and the "what do you want to edit" feature will get easier, too. Third, the same functions can be used for adduser. Use user_prev and specify the things to ask in edituser) (e.g. [ $username_done ] && user_prev=adduser && func=username) for maximum portability
	if [ "$list_done" != 1 ]; then
		nouser=0
		scanlist=1
		prev_list=edituser
		autologin_title="Edit user"
		func=scanusers
	else
		IFS=":"
		line=`cat /etc/passwd | grep "^$c_user"`
		set -- $line
		IFS="$OLDIFS"
		
		dlginput=`dialog --stdout --backtitle "WizzyConf" --title "Edit user" --msgbox "What do you want to edit?" 0 0 0 "1) username" "$1" "2)" "Password" "3) UID" "$3" "4) GID" "$4" "5) realname/comment" "$5" "6) usershell" "$7" "7) home-directory" "$6"`
			
		# Example: root:xxSVJVBe8UDn2:0:0:Tim Bisley:/home:/bin/sh
		
		if [ "$skip" != "1" ]; then
			userinput_title="Edit User"
			userinput_prev="edituser"
			[ "$userinput_username_done" != "1" ] && func=userinput_username
			[ "$userinput_uid_done" != "1" ] && func=userinput_uid
			[ "$userinput_gid_done" != "1" ] && func=userinput_gid
			[ "$userinput_realname_done" != "1" ] && func=userinput_realname
			[ "$userinput_shell_done" != "1" ] && func=userinput_shell
			[ "$userinput_password_done" != "1" ] && func=userinput_password
			mv /etc/passwd /etc/passwd.bak
			sed "s?$line?$user:$2:$dlginput_uid:$dlginput_gid:$dlginput_realname:$dlginput_home:$dlginput_shell?" /etc/passwd.bak > /etc/passwd
		fi
		
		# TODO: check if new home exists and ask for creating [done as of 302208, testing!!!] [eh, really done? o0]
		# FIXME cancel doesn't work
		# TODO: comment what sed does
	
		userinput_username_done=
		userinput_uid_done=
		userinput_gid_done=
		userinput_realname_done=
		userinput_shell_done=
		userinput_password_done=
		userinput_autologin_done=
		userinput_title=
		dlginput_user=
		dlginput_uid=
		dlginput_gid=
		dlginput_realname=
		dlginput_home=
		dlginput_shell=
		dlginput=
		func=users
		prev_list=
		c_user=
		cmd=
		list_done=0
		autologin_title=
		skip=
	fi
;;

#---------------------------#
userinput_username)
#---------------------------#
	while [ "$ok" = 0 -a "$skip" != 1 ]; do
		dlginput_user=`dialog --stdout --backtitle "WizzyConf" --title "Username" --inputbox "The username should (doesn't have to) contain only A-Z, a-z, 0-9 and - and _. It may not begin with the characters -+@~ and may not contain :\"#,=\\/?' space, tab, \"ALL\" or \"default\"." 0 0 "$1"`
		case $? in
			1|255) # cancel
				ok=1
				skip=1
				;;
			*)
				user=`echo "$dlginput_user" | sed "s/[:\\\\\"#,=\/?']//g;s/^[+@~ -]//g;s/ALL//g;s/default//g"`
				if [ "$user" != "$dlginput_user" -o "$user" = "" ]; then
					dialog --stdout --backtitle "WizzyConf" --ok-label "Damn!"  --title "Warning!" --msgbox "The username should only contain a-z, A-Z, 0-9, - and _. It may not begin with the characters -+@~ and may not contain the characters :\"#,=\/?\'\` any whitespace (space, tabulator, newline) nor the words \"ALL\" or \"default\"" 0 0
					ok=0
				else
					if [ "$1" != "$user" ]; then # user changed
						auto=`cat /etc/autologin.conf | grep "AUTOLOGIN.*$"`
						if [ "$auto" = "yes" ]; then
							dialog --stdout --backtitle "WizzyConf" --title "Edit user" --yesno "The user you edited got logged in automatically, but you changed the username. Should the autologin get adjusted accordingly?" 0 0
							if [ $? != 1 ]; then
								if [ $? != 255 ]; then
									mv /etc/autologin.conf /etc/autologin.conf.bak
									sed "s/USER.*$/USER='$user'/" /etc/autologin.conf.bak > /etc/autologin.conf
								fi
							fi
							hadouto=	
						fi
					ok=1
					fi
				fi
				;;
		esac
	done
	userinput_username_done=1
	func=userinput_prev
;;
#---------------------------#
userinput_uid)
#---------------------------#

# FIXME: check the range in which uids may be and check if the uid already exists when called from adduser
	while [ "$ok" = 0 -a "$skip" != 1 ]; do
		dlginput_uid=`dialog --stdout --backtitle "WizzyConf" --title "$userinput_title" --inputbox "UID (user-ID)${NEWLINE}Don't change that unless you know what you're doing!" 0 0 "$3"`
		case $? in
			1|255) # cancel
				ok=1
				skip=1
				;;
			*)
				uid=`echo "$dlginput_uid" | sed "s/![0-9]//g"`
				if [ "$uid" != "$dlginput_uid" -o "$uid" = "" ]; then
					dialog --stdout --backtitle "WizzyConf" --ok-label "Damn!"  --title "Warning!" --msgbox "The UID may only contain numbers and may not be emtpy!" 0 0
					ok=0
				else
					ok=1
				fi
				;;
		esac
	done
	userinput_uid_done=1
	func=userinput_prev
;;
#---------------------------#
userinput_gid)
#---------------------------#
# FIXME: check the range in which gids may be and check if the uid already exists when called from adduser
	while [ "$ok" = 0 -a "$skip" != 1 ]; do
		dlginput_gid=`dialog --stdout --backtitle "WizzyConf" --title "$userinput_title" --inputbox "GID (group-ID)${NEWLINE}Don't change that unless you know what you're doing!" 0 0 "$4"`
		case $? in
			1|255) # cancel
				ok=1
				skip=1
				;;
			*)
				gid=`echo "$dlginput_gid" | sed "s/![0-9]//g"`
				if [ "$gid" != "$dlginput_gid" -o "$gid" = "" ]; then
					dialog --stdout --backtitle "WizzyConf" --ok-label "Damn!"  --title "Warning!" --msgbox "The GID may only contain numbers and may not be emtpy!" 0 0
					ok=0
				else
					ok=1
				fi
				;;
		esac
	done
	userinput_gid_done=1
	func=userinput_prev
;;
#---------------------------#
userinput_realname)
#---------------------------#
	while [ "$ok" = 0 -a "$skip" != 1 ]; do
		dlginput_user=`dialog --stdout --backtitle "WizzyConf" --title "Username" --inputbox "The username should (doesn't have to) contain only A-Z, a-z, 0-9 and - and _. It may not begin with the characters -+@~ and may not contain :\"#,=\\/?' space, tab, \"ALL\" or \"default\"." 0 0 "$1"`
		case $? in
			1|255) # cancel
				ok=1
				skip=1
				;;
			*)
				realname=`echo "$dlginput_realname" | sed "s/://g"` # FIXME what else isn't allowed in the realname?
				if [ "$realname" != "$dlginput_realname" ]; then
					dialog --stdout --backtitle "WizzyConf" --ok-label "Damn!"  --title "Warning!" --msgbox "The realname may not contain any colons (:)" 0 0
					ok=0
				else
					ok=1
				fi
				;;
		esac
	done
	userinput_realname_done=1
	func=userinput_prev
;;
#---------------------------#
userinput_shell)
#---------------------------#
	while [ "$ok" = 0 -a "$skip" != 1 ]; do
		shell=`dialog --stdout --backtitle "WizzyConf" --title "$userinput_title" --inputbox "Usershell${NEWLINE}Don't change that unless you know what you're doing!" 0 0 "$7"`
		case $? in
			1|255) # cancel
				ok=1
				skip=1
				;;
			*)
				if [ -f "$shell" -a -x "$shell" ]; then
					dialog --stdout --backtitle "WizzyConf" --ok-label "Damn!"  --title "Warning!" --msgbox "The shell must be an executable file." 0 0
					ok=0
				else
					ok=1
				fi
				;;
		esac
	done
	userinput_shell_done=1
	func=userinput_prev
	
;;
#---------------------------#
userinput_password)
#---------------------------#
	dialog --stdout --backtitle "WizzyConf" --title "Edit user" --yesno "Do you want to set a new password?" 0 0 && passwd -q "$user"
	case $? in
		0)
			dialog --stdout --backtitle "WizzyConf" --title "Edit user" --msgbox "Password successfully changed." 0 0
			;;
		1)
			dialog --stdout --backtitle "WizzyConf" --title "Edit user" --msgbox "Failed changing password: no rights to open /etc/passwd${NEWLINE}${NEWLINE}Please send a bugreport to florianbruh@gmail.com including this information:${NEWLINE}func: ${func}${NEWLINE}user: $dlginput_user${NEWLINE}version: $version${NEWLINE}error: passwd died with exit status 1${NEWLINE}ls: `ls -l /etc/passwd`${NEWLINE}version of dslinux: `cat /etc/version`" 0 0
			;;
		2)
			dialog --stdout --backtitle "WizzyConf" --title "Edit user" --msgbox "Failed changing password: invalid combination of options${NEWLINE}${NEWLINE}Please send a bugreport to florianbruh@gmail.com including this information:${NEWLINE}func: ${func}${NEWLINE}user: $dlginput_user${NEWLINE}version: $version${NEWLINE}error: passwd died with exit status 2${NEWLINE}version of dslinux: `cat /etc/version`" 0 0
			;;
		3)
			dialog --stdout --backtitle "WizzyConf" --title "Edit user" --msgbox "Failed changing password: unexpected error${NEWLINE}${NEWLINE}Please send a bugreport to florianbruh@gmail.com including this information:${NEWLINE}func: ${func}${NEWLINE}user: $dlginput_user${NEWLINE}version: $version${NEWLINE}error: passwd died with exit status 3${NEWLINE}version of dslinux: `cat /etc/version`" 0 0
			;;
		4)
			dialog --stdout --backtitle "WizzyConf" --title "Edit user" --msgbox "Failed changing password: unexpected error: /etc/password doesn't exist${NEWLINE}${NEWLINE}Please send a bugreport to florianbruh@gmail.com including this information:${NEWLINE}func: ${func}${NEWLINE}user: $dlginput_user${NEWLINE}version: $version${NEWLINE}error: passwd died with exit status 4${NEWLINE}version of dslinux: `cat /etc/version`" 0 0
			;;
		5)
			dialog --stdout --backtitle "WizzyConf" --title "Edit user" --msgbox "Failed changing password: /etc/passwd is already in use${NEWLINE}${NEWLINE}Please send a bugreport to florianbruh@gmail.com including this information:${NEWLINE}func: ${func}${NEWLINE}user: $dlginput_user${NEWLINE}version: $version${NEWLINE}error: passwd died with exit status 5${NEWLINE}version of dslinux: `cat /etc/version`" 0 0
			;;
		6)
			dialog --stdout --backtitle "WizzyConf" --title "Edit user" --msgbox "Failed changing password: Invalid argument for option${NEWLINE}${NEWLINE}Please send a bugreport to florianbruh@gmail.com including this information:${NEWLINE}func: ${func}${NEWLINE}user: $dlginput_user${NEWLINE}version: $version${NEWLINE}error: passwd died with exit status 6${NEWLINE}version of dslinux: `cat /etc/version`" 0 0
			;;
	esac
	userinput_shell_done=1
	func=userinput_prev
;;

#===========================#
adduser)
#===========================#
	dialog --yesno "This feature is still in development. Enter anyway?" 0 0 # FIXME comment out development only
	case $? in # FIXME comment out development only
	1|255) func=users ;; # FIXME comment out development only
	*) # FIXME comment out development only
		# Example: root:xxSVJVBe8UDn2:0:0:Tim Bisley:/home:/bin/sh
		lastuser=`echo /etc/passwd | sed -n -e '1 p'`	# that looks
		IFS=":"						# rather strange
		set -- $lastuser				# does that
		IFS="$OLDIFS"					# work? FIXME
		
		if [ "$3" -ge 1000 ]; then
			uid=`expr "$3" + 1`
		else
			uid=1000
		fi

		dlginput_user=`dialog --stdout --backtitle "WizzyConf" --title "$userinput_title" --inputbox "Username" 0 0` && \
		dlginput_realname=`dialog --stdout --backtitle "WizzyConf" --title "$userinput_title" --inputbox "Realname" 0 0` && \
		dlginput_uid=`dialog --stdout --backtitle "WizzyConf" --title "$userinput_title" --inputbox "UID${NEWLINE}Don't change that unless you know what you're doing!" 0 0 "$uid"` && \
		dlginput_home=`dialog --stdout --backtitle "WizzyConf" --title "$userinput_title" --inputbox "Home${NEWLINE}Don't change that unless you know what you're doing!" 0 0 "/home"` && \
		dlginput_shell=`dialog --stdout --backtitle "WizzyConf" --title "$userinput_title" --inputbox "Usershell${NEWLINE}Don't change that unless you know what you're doing!" 0 0 "/bin/sh"`
	
		case $? in	# does it what it should
		1|255)		# because of the &&s (FIXME)
			dlginput_user=
			dlginput_realname=
			dlginput_home=
			dlginput_shell=
			dlginput_uid=
			uid=
			lastuser=
			func=users
			;;
		*)


			# TODO: check if new home exists and ask for creating [done as of 302208, testing!!!]
			# FIXME: TEST Test if updating of /etc/group acts correctly [is it even necessary?]

			# FIXME: Ask if it should adjust autologin [should be done as of 081120, needs testing]
			# FIXME cancel doesn't work
			# TODO: comment what sed does
			
			dialog --stdout --backtitle "WizzyConf" --infobox "Please wait..." 0 0
	
			user=`echo "$dlginput_user" | sed "s/[:\\\\\"#,=\/?']//g;s/^[+@~ -]//g;s/ALL//g;s/default//g"`
			echo "$user:xxSVJVBe8UDn2:$dlginput_uid:0:$dlginput_realname:$dlginput_home:$dlginput_shell" >> /etc/passwd

			dialog --stdout --backtitle "WizzyConf" --title "Set a new password" --msgbox "You will now have to enter your password twice (no *s or any other characters will get displayed). Your password should have 5-8 characters and contain normal letters, capital letters and numbers." 0 0 && passwd -q "$dlginput_user"
			case $? in
				0)
					dialog --stdout --backtitle "WizzyConf" --title "Edit user" --msgbox "Password successfully changed.${NEWLINE}${NEWLINE}Test-debug:${NEWLINE}func: ${func}${NEWLINE}user: $dlginput_user${NEWLINE}version: $version${NEWLINE}error: passwd died with exit status 1${NEWLINE}ls: `ls -l /etc/passwd`${NEWLINE}version of dslinux: `cat /etc/version`" 0 0 # FIXME
					;;
				1)
					dialog --stdout --backtitle "WizzyConf" --title "Edit user" --msgbox "Failed changing password: no rights to open /etc/passwd${NEWLINE}${NEWLINE}Please send a bugreport to florianbruh@gmail.com including this information:${NEWLINE}func: ${func}${NEWLINE}user: $dlginput_user${NEWLINE}version: $version${NEWLINE}error: passwd died with exit status 1${NEWLINE}ls: `ls -l /etc/passwd`${NEWLINE}version of dslinux: `cat /etc/version`" 0 0
					;;
				2)
					dialog --stdout --backtitle "WizzyConf" --title "Edit user" --msgbox "Failed changing password: invalid combination of options${NEWLINE}${NEWLINE}Please send a bugreport to florianbruh@gmail.com including this information:${NEWLINE}func: ${func}${NEWLINE}user: $dlginput_user${NEWLINE}version: $version${NEWLINE}error: passwd died with exit status 2${NEWLINE}version of dslinux: `cat /etc/version`" 0 0
					;;
				3)
					dialog --stdout --backtitle "WizzyConf" --title "Edit user" --msgbox "Failed changing password: unexpected error${NEWLINE}${NEWLINE}Please send a bugreport to florianbruh@gmail.com including this information:${NEWLINE}func: ${func}${NEWLINE}user: $dlginput_user${NEWLINE}version: $version${NEWLINE}error: passwd died with exit status 3${NEWLINE}version of dslinux: `cat /etc/version`" 0 0
					;;
				4)
					dialog --stdout --backtitle "WizzyConf" --title "Edit user" --msgbox "Failed changing password: unexpected error: /etc/password doesn't exist${NEWLINE}${NEWLINE}Please send a bugreport to florianbruh@gmail.com including this information:${NEWLINE}func: ${func}${NEWLINE}user: $dlginput_user${NEWLINE}version: $version${NEWLINE}error: passwd died with exit status 4${NEWLINE}version of dslinux: `cat /etc/version`" 0 0
					;;
				5)
					dialog --stdout --backtitle "WizzyConf" --title "Edit user" --msgbox "Failed changing password: /etc/passwd is already in use${NEWLINE}${NEWLINE}Please send a bugreport to florianbruh@gmail.com including this information:${NEWLINE}func: ${func}${NEWLINE}user: $dlginput_user${NEWLINE}version: $version${NEWLINE}error: passwd died with exit status 5${NEWLINE}version of dslinux: `cat /etc/version`" 0 0
					;;
				6)
					dialog --stdout --backtitle "WizzyConf" --title "Edit user" --msgbox "Failed changing password: Invalid argument for option${NEWLINE}${NEWLINE}Please send a bugreport to florianbruh@gmail.com including this information:${NEWLINE}func: ${func}${NEWLINE}user: $dlginput_user${NEWLINE}version: $version${NEWLINE}error: passwd died with exit status 6${NEWLINE}version of dslinux: `cat /etc/version`" 0 0
					;;
			esac

			cmd=
			uid=
			user=
			dlginput_user=
			dlginput_realname=
			dlginput_home=
			dlginput_shell=
			dlginput=
			lastuser=
			func=users
		esac
	esac
	;;
#===========================#
deluser)
#===========================#
	if [ "$list_done" != 1 ]; then
		nouser=0
		scanlist=1
		prev_list=deluser
		autologin_title="Delete user"
		func=scanusers
	else
		# FIXME what about the errors deluser gives? Maybe simply ignore?
		dialog --stdout --backtitle "WizzyConf" --title "Delete user" --yesno "Really delete the user \"$c_user\"?" 0 0 && deluser "$c_user"
		case $? in
			0)
				cmd=
				;;
			1)
				dialog --stdout --backtitle "WizzyConf" --title "Delete user" --msgbox "Deleting user \"$c_user\" failed: The user to delete was not a system account. No action was performed.${NEWLINE}${NEWLINE}Please send a bugreport to florianbruh@gmail.com including this information:${NEWLINE}func: ${func}${NEWLINE}user: $c_user${NEWLINE}version: $version${NEWLINE}error: deluser died with exit status 1${NEWLINE}passwd: `cat /etc/passwd | grep $c_user`${NEWLINE}version of deluser: `deluser --version | grep version`${NEWLINE}version of dslinux: `cat /etc/version`" 0 0
				;;
			2)
				dialog --stdout --backtitle "WizzyConf" --title "Delete user" --msgbox "Deleting user \"$c_user\" failed: There is no such user. No action was performed.${NEWLINE}${NEWLINE}Please send a bugreport to florianbruh@gmail.com including this information:${NEWLINE}func: ${func}${NEWLINE}user: $c_user${NEWLINE}version: $version${NEWLINE}error: deluser died with exit status 2${NEWLINE}passwd: `cat /etc/passwd | grep $c_user`${NEWLINE}version of deluser: `deluser --version | grep version`${NEWLINE}version of dslinux: `cat /etc/version`" 0 0
				;;
			3)
				dialog --stdout --backtitle "WizzyConf" --title "Delete user" --msgbox "Deleting user \"$c_user\" failed: There is no such group. No action was performed.${NEWLINE}${NEWLINE}Please send a bugreport to florianbruh@gmail.com including this information:${NEWLINE}func: ${func}${NEWLINE}user: $c_user${NEWLINE}version: $version${NEWLINE}error: deluser died with exit status 3${NEWLINE}passwd: `cat /etc/passwd | grep $c_user`${NEWLINE}version of deluser: `deluser --version | grep version`${NEWLINE}version of dslinux: `cat /etc/version`" 0 0
				;;
			4)
				dialog --stdout --backtitle "WizzyConf" --title "Delete user" --msgbox "Deleting user \"$c_user\" failed: Internal error. No action was performed.${NEWLINE}${NEWLINE}Please send a bugreport to florianbruh@gmail.com including this information:${NEWLINE}func: ${func}${NEWLINE}user: $c_user${NEWLINE}version: $version${NEWLINE}error: deluser died with exit status 1${NEWLINE}passwd: `cat /etc/passwd | grep $c_user`${NEWLINE}version of deluser: `deluser --version | grep version`${NEWLINE}version of dslinux: `cat /etc/version`" 0 0
				;;
			5)
				dialog --stdout --backtitle "WizzyConf" --title "Delete user" --msgbox "Deleting user \"$c_user\" failed: The group to delete is not empty. No action was performed.${NEWLINE}${NEWLINE}Please send a bugreport to florianbruh@gmail.com including this information:${NEWLINE}func: ${func}${NEWLINE}user: $c_user${NEWLINE}version: $version${NEWLINE}error: deluser died with exit status 1${NEWLINE}passwd: `cat /etc/passwd | grep $c_user`${NEWLINE}version of deluser: `deluser --version | grep version`${NEWLINE}version of dslinux: `cat /etc/version`" 0 0
				;;
			6)
				dialog --stdout --backtitle "WizzyConf" --title "Delete user" --msgbox "Deleting user \"$c_user\" failed: The user does not belong to the specified group. No action was performed.${NEWLINE}${NEWLINE}Please send a bugreport to florianbruh@gmail.com including this information:${NEWLINE}func: ${func}${NEWLINE}user: $c_user${NEWLINE}version: $version${NEWLINE}error: deluser died with exit status 1${NEWLINE}passwd: `cat /etc/passwd | grep $c_user`${NEWLINE}version of deluser: `deluser --version | grep version`${NEWLINE}version of dslinux: `cat /etc/version`" 0 0
				;;
			7)
				dialog --stdout --backtitle "WizzyConf" --title "Delete user" --msgbox "Deleting user \"$c_user\" failed: You cannot remove a user from its primary group. No action was performed.${NEWLINE}${NEWLINE}Please send a bugreport to florianbruh@gmail.com including this information:${NEWLINE}func: ${func}${NEWLINE}user: $c_user${NEWLINE}version: $version${NEWLINE}error: deluser died with exit status 1${NEWLINE}passwd: `cat /etc/passwd | grep $c_user`${NEWLINE}version of deluser: `deluser --version | grep version`${NEWLINE}version of dslinux: `cat /etc/version`" 0 0
				;;
			8)
				dialog --stdout --backtitle "WizzyConf" --title "Delete user" --msgbox "Deleting user \"$c_user\" failed: The required perl-package perl-modules is not installed. This package is required to perform the requested actions. No action was performed.${NEWLINE}${NEWLINE}Please send a bugreport to florianbruh@gmail.com including this information:${NEWLINE}func: ${func}${NEWLINE}user: $c_user${NEWLINE}version: $version${NEWLINE}error: deluser died with exit status 1${NEWLINE}passwd: `cat /etc/passwd | grep $c_user`${NEWLINE}version of deluser: `deluser --version | grep version`${NEWLINE}version of dslinux: `cat /etc/version`" 0 0
				;;
			9)
				dialog --stdout --backtitle "WizzyConf" --title "Delete user" --msgbox "Deleting user \"$c_user\" failed: For removing the root account the parameter "--force" is required. No action was performed.${NEWLINE}${NEWLINE}Please send a bugreport to florianbruh@gmail.com including this information:${NEWLINE}func: ${func}${NEWLINE}user: $c_user${NEWLINE}version: $version${NEWLINE}error: deluser died with exit status 1${NEWLINE}passwd: `cat /etc/passwd | grep $c_user`${NEWLINE}version of deluser: `deluser --version | grep version`${NEWLINE}version of deluser: `deluser --version | grep version`${NEWLINE}version of dslinux: `cat /etc/version`" 0 0
				;;
		esac
		func=users
		prev_list=
		c_user=
		list_done=0
		autologin_title=
	fi
	;;
#===========================#
userlist)
#===========================#
	# FIXME TODO edit that to use scanusers) 
	IFS=":"
	cat /etc/passwd | while read line
	do
		set -- $line
		allusers="$allusers|$1"
		allusers="$allusers/$5"
	done

	line=
	IFS="|"
	i=1

	for j in $allusers; do
		IFS="/"
		set -- $j
		cmd="$cmd \"$i\) $1\" \"$2\""
		i=`expr $i + 1`
	done
	allusers=
	dlginput=
	j=
	i=
	eval dlginput=\`dialog --stdout  --backtitle \""WizzyConf"\" --title \""List users"\" --ok-label \""Show more info"\" --menu \"\" 0 0 0 "$cmd"\`
	case $? in
	1|255)
		cmd=
		func=start
		;;
	*)
		cmd=
		IFS=" "
		set -- $dlginput
		user="$2"

		infos=`cat /etc/passwd | grep "^$user"`
		IFS=":"
		set -- $infos
		[ "$1" = "nobody" -o "$1" = "bin" -o "$1" = "daemon" ] && sysuser="[systemuser]"
		[ "$1" = "root" ] && sysuser="[superuser (root)]"
		dlginput2=`dialog --stdout --backtitle "WizzyConf" --title "Userinfo to user \"$user\"" --msgbox "Username: $1${NEWLINE}passwordhash: $2${NEWLINE}UID: $3${NEWLINE}GID: $4${NEWLINE}Realname: $5${NEWLINE}Home: $6${NEWLINE}Usershell: $7${NEWLINE}$sysuser" 0 0`

		infos=
		sysuser=
		user=
		dlginput2=
		IFS="$OLDIFS"
		func=userlist
		;;
	esac
	;;

#===========================#
scanusers)
#===========================#
	# TODO add a showsysusers-bit  [code done, but add a button to switch it now :D]
	
	if [ -z "$cmd" ]; then # if cmd is empty -> no scan was done before or something changed
		IFS=":"
		cat /etc/passwd | while read scanline; do
			set -- $scanline
			allusers="$allusers|$1"
			allusers="$allusers/$5"
		done
	
		IFS="|"
		i=1
		for j in $allusers; do
			IFS="/"
			set -- $j
			[ "$1" = nobody ] && no=1
			[ "$1" = bin ] && no=1
			[ "$1" = daemon ] && no=1
			if [ "$no" != "1" ]; then # user is no sysuser
				cmd="$cmd \"$i\) $1\" \"$2\""
				i=`expr $i + 1`
			elif [ "$no" = "1" ] && [ "$listsysusers" = "1" ]; then # user is a sysuser and sysusers should get displayed
				cmd="$cmd \"$i\) $1\" \"[systemuser]\""
				i=`expr $i + 1`
			else # user is a sysuser but they shouldn't get displayed
				no=
			fi
		done
		i="$i\)"
	fi
	
	echo "scanlist = $scanlist" # FIXME
	if [ "$scanlist" = "1" ]; then
		[ "$nouser" = "1" ] && nouserdisp="\"$i\" \"No autologin\""
		
		eval dlginput=\`dialog --stdout  --backtitle \""WizzyConf"\" --title \""$userlist_title"\" --extra-button --extra-label \""show sysusers"\" --menu \"\" 0 0 0 "$cmd" "$nouserdisp" --infobox \""Please wait..."\" 0 0\`
		dlgout="$?"
		
		sleep 5 #FIXME		
		c_user=
		# if 3 show sysusers
		case $dlgout in
		0)
			if [ "$i)" != "$dlginput" ]; then # if a user is choosen.
				IFS=" "
				set -- $dlginput
				c_user="$2"
			fi
			scanlist=
			;;
		3)
			# FIXME 081130 add a please wait-screen
			showsysusers=1
			cmd=
			func=scanusers
			scanlist=1
			;;
		*)
			scanlist=
			;;
		esac
	fi
	

	nouserdisp=
	
	i=
	j=
	IFS="$OLDIFS"
	allusers=
	dlginput=
	nouser=0
	no=0
	

	# FIXME 081130 Add this to the "case" above and be sure func is scanusers when $? is 3.
	# oh, cancel takes quite a while
	if [ "$dlgout" = "0" ]; then
		echo "prev_scan: $prev_scan"
		echo ">> prev_list: $prev_list"
 		func="$prev_list"
		list_done=1
	elif [ -z "$dlgout" ]; then # if dlgout is empty so only the scan was done
		echo ">> prev_scan: $prev_scan"
		echo "prev_list: $prev_list"
		func="$prev_scan"
	else
		func=users
		list_done=0
	fi
	dlgout=
	;;
		
		
#===========================#
autologin)
#===========================#
	if [ "$list_done" != 1 ]; then
		nouser=1
		prev_list=autologin
		userlist_title="Autologin"
		func=scanusers
	else
		if [ -z "$c_user" ]; then
			mv /etc/autologin.conf /etc/autologin.conf.bak
			sed 's/AUTOLOGIN.*$/AUTOLOGIN=no/' /etc/autologin.conf.bak > /etc/autologin.conf
		else
			mv /etc/autologin.conf /etc/autologin.conf.bak
			sed 's/AUTOLOGIN.*$/AUTOLOGIN=yes/;s/USER.*$/USER='$c_user'/' /etc/autologin.conf.bak > /etc/autologin.conf
		fi
		func=users
		prev_list=
		c_user=
		list_done=0
	fi
	;;


#==============================================================#
network)
#==============================================================#
	dlginput=`dialog --stdout --backtitle "WizzyConf" --title "Network" --menu "" 0 0 0 \
	"1)" "WiFi" \
	"2)" "esd" \
	"3)" "DynDNS" \
	"4)" "Boa" \
	"5)" "Hostname" \
	"6)" "Hosts" \
	"7)" "SSH" \
	"8)" "telnet" \
	"9)" "FTP" \
	"10)" "Samba"`
	case $? in
		1|255) func=start ;;
	esac

	case $dlginput in
		1\)) func=wifi ;;
		2\)) func=esd ;;
		3\)) func=dns ;;
		4\)) func=boa ;;
		5\)) func=hostname ;;
		6\)) func=hosts ;;
		7\)) func=ssh ;;
		8\)) func==telnet ;;
		9\)) func=ftp ;;
		10\)) func=samba ;;
	esac
	;;

#===========================#
wifi)
#===========================#
	dialog --stdout --backtitle "WizzyConf" --title "WiFi" --yesno "Start wnc now?" 0 0 && wnc
	func=network
	;;
#===========================#
esd)
#===========================#
	dialog --stdout --backtitle "WizzyConf" --title "Warning!" --ok-label "Damn!" --msgbox "Sorry, this function doesn't exist yet" 0 0
	func=network
	;;
#===========================#
dns)
#===========================#
	dialog --stdout --backtitle "WizzyConf" --title "Warning!" --ok-label "Damn!" --msgbox "Sorry, this function doesn't exist yet" 0 0
	func=network
	;;
#===========================#
boa)
#===========================#
	dialog --stdout --backtitle "WizzyConf" --title "Warning!" --ok-label "Damn!" --msgbox "Sorry, this function doesn't exist yet" 0 0
	func=network
	;;
#===========================# Needs testing! FIXME
hostname)
#===========================#
	dialog --stdout --backtitle "WizzyConf" --infobox "Please wait..." 0 0
	hostname=`cat /etc/rc.conf | grep "hostname=" | sed "s/hostname=//" | sed "s/\"//g"`
	dlginput=`dialog --stdout --backtitle "WizzyConf" --title "Hostname of the DS" --inputbox "May only contain a-z, A-Z, 0-9 or - but may not begin or end with -." 0 0 $hostname` 
	case $? in
		1|255) func=network ;;
	esac

	# FIXME BUG sed gives an error (fixed?)

	mv /etc/rc.conf /etc/rc.conf.bak
	hostname=`echo "$dlginput" | sed "s/[^a-zA-Z0-9-]//g"`
	if [ -z "$hostname" ]; then
		dialog --stdout --backtitle "WizzyConf" --title "Warning!" --ok-label "Damn!" "The hostname you entered was empty or contained unallowed characters." 0 0
	else
		sed 's/hostname=.*$/hostname=\"$hostname\"/' /etc/rc.conf.bak > /etc/rc.conf # TODO: delete non-allowed chars
		# doesn't work FIXME
	fi

	hostname=
	func=network
	;;
#===========================#
hosts)
#===========================#

	dialog --stdout --backtitle "WizzyConf" --title "Warning!" --ok-label "Damn!" --msgbox "Sorry, this function doesn't exist yet" 0 0
	func=network
	;;
#===========================#
ssh)
#===========================#
	dialog --stdout --backtitle "WizzyConf" --title "Warning!" --ok-label "Damn!" --msgbox "Sorry, this function doesn't exist yet" 0 0
	func=network
	;;
#===========================#
telnet)
#===========================#
	dialog --stdout --backtitle "WizzyConf" --title "Warning!" --ok-label "Damn!" --msgbox "Sorry, this function doesn't exist yet" 0 0
	func=network
	;;
#===========================#
ftp)
#===========================#
	dialog --stdout --backtitle "WizzyConf" --title "Warning!" --ok-label "Damn!" --msgbox "Sorry, this function doesn't exist yet" 0 0
	func=network
	;;
#===========================#
samba)
#===========================#
	dialog --stdout --backtitle "WizzyConf" --title "Warning!" --ok-label "Damn!" --msgbox "Sorry, this function doesn't exist yet" 0 0
	func=network
	;;

#==============================================================#
misc)
#==============================================================#
	dialog --stdout --backtitle "WizzyConf" --title "Warning!" --ok-label "Damn!" --msgbox "Sorry, this function doesn't exist yet" 0 0
	func=start
	;;

#==============================================================#
about)
#==============================================================#
	# FIXME Intro => Credits => Lizenz per Buttons switchbar
	dialog --ok-label "Show credits" --extra-button --extra-label "Show license" --stdout --backtitle "WizzyConf" --title "About Wizzy" --textbox /usr/share/wizzy/about 0 0
	case $? in
		1|255) func=start ;;
		0) func=credits ;;
		3) func=licence ;;
	esac
	;;
#===========================#
licence)
#===========================#
	dialog --ok-label "Show about" --extra-button --extra-label "Show credits" --stdout --backtitle "WizzyConf" --title "About Wizzy" --textbox /usr/share/wizzy/copying 0 0
	case $? in
		1|255) func=start ;;
		0) func=about ;;
		3) func=credits ;;
	esac
	;;

#===========================#
credits)
#===========================#
	dialog --ok-label "Show about" --extra-button --extra-label "Show license" --stdout --backtitle "WizzyConf" --title "About Wizzy" --textbox /usr/share/wizzy/credits 0 0
	case $? in
		1|255) func=start ;;
		0) func=about ;;
		3) func=licence ;;
	esac
	;;

#=============================================================#
#=============================================================#
esac
done

