summaryrefslogtreecommitdiff
path: root/gohan.sh
blob: 1219c106928c32df97a2feb7139323ef184b0e29 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/bin/sh

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

### OPTIONS AND VARIABLES ###

### FUNCTIONS ###

install_from_pacman() {
  pacman --noconfirm --needed -S "$1" >/dev/null 2>&1
}

# Install packages required by GOHAN.
install_dependancies() {
  install_from_pacman base-devel
  install_from_pacman git
}

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
}

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
}

# Sets up the home directory for the user account.
setup_user_home_directory() {
  clean_user_home_directory
}

# Remove un-needed files and directories from the home directory.
clean_user_home_directory() {
  [ -f "/home/$name/.bash_history" ] && rm /home/$name/.bash_history
  [ -f "/home/$name/.bash_logout" ] && rm /home/$name/.bash_logout
  [ -f "/home/$name/.bash_profile" ] && rm /home/$name/.bash_profile
  [ -f "/home/$name/.bashrc" ] && rm /home/$name/.bashrc
}


install_yay() {
  [ -f "/usr/bin/yay" ] || (
    echo "Installing yay."
    # Use all cores for compilation.
    sed -i "s/-j2/-j$(nproc)/;s/^#MAKEFLAGS/MAKEFLAGS/" /etc/makepkg.conf
    cd /tmp
    rm -rf /tmp/yay
    git clone https://aur.archlinux.org/yay.git
	chown -R "$name":"$name" yay 
    cd yay
    sudo -u "$name" makepkg --noconfirm -si >/dev/null 2>&1
    cd /tmp
    rm -rf /tmp/yay
  );
}

### THE ACTUAL SCRIPT ###

install_dependancies

add_user

setup_user_home_directory

install_yay