summaryrefslogtreecommitdiff
path: root/gohan.sh
blob: 493fed7bb27c3272b034b529876c69f9d9bfef21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/bin/sh

# My Arch Linux installation script
# by David T. Sadler <david@davidtsadler.com>
# License: GNU GPLv3

### OPTIONS AND VARIABLES ###

### FUNCTIONS ###

read_password() {
  PASSWORD="$(
    # always read from the tty even when redirected:
    exec < /dev/tty || exit # || exit only needed for bash

    # save current tty settings:
    tty_settings=$(stty -g) || exit

    # schedule restore of the settings on exit of that subshell
    # or on receiving SIGINT or SIGTERM:
    trap 'stty "$tty_settings"' EXIT INT TERM

    # disable terminal local echo
    stty -echo || exit

    # prompt on tty
    printf "$1" > /dev/tty

    # read password as one line, record exit status
    IFS= read -r password; ret=$?

    # display a newline to visually acknowledge the entered password
    echo > /dev/tty

    # return the password for $REPLY
    printf '%s\n' "$password"
    exit "$ret"
  )"
}

get_username_and_password() {
  printf "Enter a name for the user account: "
  read name
  while ! echo "$name" | grep "^[a-z_][a-z0-9_-]*$" >/dev/null 2>&1; do
    unset name
    printf "Username not valid. Give a username beginning with a letter, with only lowercase letters, - or _.\n\n"
    printf "Enter a name for the user account: "
    read name
  done
  read_password "Enter a password for that user: "
  pass1=$PASSWORD
  read_password "Retype password: "
  pass2=$PASSWORD
  while ! [ "$pass1" = "$pass2" ]; do
    unset pass2
    echo "Passwords do not match."
    read_password "Enter a password again: "
    pass1=$PASSWORD
    read_password "Retype password: "
    pass2=$PASSWORD
  done
}

add_user() {
  get_username_and_password
  [ -d "/home/$name" ] && echo "Skipping adding of user as $name already exists" && return

  useradd -m -s /bin/zsh "$name" >/dev/null 2>&1 || mkdir -p /home/"$name" && chown "$name":"$name" /home/"$name"
  usermod -aG wheel "$name"
  echo "$name:$pass1" | chpasswd
  unset pass1 pass2
}

### THE ACTUAL SCRIPT ###

add_user