op_usermanager: A new script, a very basic usermanager to add and remove users
[openpandora.oe.git] / recipes / pandora-system / pandora-scripts / op_usermanager.sh
1 #!/bin/bash
2
3 if zenity --question --title="Usermanager" --text="Do you wish to add or remove a user?" --ok-label="Add user" --cancel-label="Remove User"; then       
4
5         echo "Add user"
6         while ! name=$(zenity --title="Enter full name" --entry --text "Please enter a full name for the new user.") || [ "x$name" = "x" ] ; do
7             zenity --title="Error" --error --text="Please try again." --timeout 6
8         done
9
10         username_guess=$(echo "$name" | cut -d" " -f1 | tr A-Z a-z)
11
12         while ! username=$(zenity --title="Enter the new username" --entry --text "Please choose a short username.\n\nIt should be all lowercase and contain only letters and numbers." --entry-text "$username_guess") || [ "x$username" = "x" ] ; do
13             zenity --title="Error" --error --text="Please try again." --timeout 6
14         done
15
16         while ! useradd -c "$name,,," -G adm,audio,video,wheel,netdev,plugdev,users "$username" ; do
17           username=$(zenity --title="Please check username" --entry --text "Please ensure that your username consists of only\nletters and numbers and is not already in use on the system." --entry-text "$username")
18         done
19
20         password=""
21         while [ x$password = x ] ; do
22           password1=$(zenity --title=Password --entry --text="Please choose a new password." --hide-text)
23           password2=$(zenity --title=Confirm --entry --text="Confirm your new password." --hide-text)
24           if [ $password1 != $password2 ] ; then 
25                 zenity --title="Error" --error --text="The passwords do not match.\n\nPlease try again." --timeout 6
26           else 
27                 if [ x$password1 = x ] ; then
28                         zenity --title="Error" --error --text="Password cannot be blank!\n\nPlease try again." --timeout 6
29                 else
30                         password=$password1
31                 fi
32           fi
33         done
34
35         passwd "$username" <<EOF
36         $password
37         $password
38 EOF
39         if zenity --question --title="User created" --text="The user $username has been successfully created.\n\nDo you want to set this user as default user for the login?" --ok-label="Yes, please!" --cancel-label="No, keep the old user as default"; then
40             sed -i "s/.*default_user.*/default_user $username/g" /etc/slim.conf
41         fi
42         zenity --info --title="User created" --text "Thanks. The new user can now be used." --timeout 6
43 else
44         xfceuser=$(ps u -C xfce4-session | tail -n1 | awk '{print $1}')
45         echo "Remove User"
46         amount=$(cat /etc/passwd | grep /home/ | grep -v root | awk -F\: '{print $1 }' | wc -l)
47         if [ ${amount} = "1" ]; then
48               zenity --title="Error" --error --text="Sorry! You can't remove the last normal user!" --timeout 6
49         else
50               selection=$(cat /etc/passwd | grep /home/ | grep -v root | grep -v $xfceuser | awk -F\: '{print $1 }' | zenity --width=100 --height=200 --title="Select the user to delete" --list  --column "Username"  --text "Select the user to delete\n\nPlease note: You can't remove the user that is currently logged in.")
51               if zenity --question --title="Confirm User Removal" --text="Are you REALLY sure you want to remove the user $selection?\n\nThere will be NO other confirmation and this can NOT be undone!" --ok-label="Yes, remove user!" --cancel-label="Don't remove the user"; then
52                   echo "Really remove $selection"
53                   userdel -fr $selection
54                   sed -i "s/.*default_user $selection/default_user/g" /etc/slim.conf
55                   zenity --info --title="User removed" --text "The user $selection has been removed." --timeout 6
56               else
57                   echo "Don't remove $selection"
58                   zenity --info --title="User not removed" --text "Cancelled removal of user $selection at user's request." --timeout 6
59               fi
60         fi
61 fi
62
63
64