summaryrefslogtreecommitdiff
path: root/gohan.sh
blob: 97ee869ff77c8c922f3951f03a6bff2fed761571 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/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_from_repo() {
  progname="$(basename "$1" .git)"
  dir="$3/$progname"

  [ -d "$dir" ] || git clone "$1" "$dir" >/dev/null 2>&1
}

# Install packages required by GOHAN.
install_dependancies() {
  echo "Installing dependancies ..."

  install_from_pacman base-devel
  install_from_pacman git
  install_from_pacman go
}

add_user() {
  get_username_and_password

  if [ -d "/home/$name" ]; then
    echo "Skipping adding of user as $name already exists."
  else
    echo "Adding user $name ..."
    useradd -m -s /bin/zsh "$name" >/dev/null 2>&1
    usermod -aG wheel "$name"
    echo "$name:$pass1" | chpasswd
  fi

  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

  unset PASSWORD
}

configure_sudo() {
  install_from_pacman sudo
  [ -f "/etc/sudoers.d/$name" ] || echo "$name ALL=(ALL) NOPASSWD: ALL" > "/etc/sudoers.d/$name"
}

# Sets up the home directory for the user account.
create_user_home_directory() {
  echo "Creating and setting up user home directory ..."

  [ -d "/home/$name" ] || mkdir -p "/home/$name"

  install_dotfiles

  clean_user_home_directory

  chown -R "$name":"$name" "/home/$name"
}

install_dotfiles() {
  srcdir="/home/$name/.local/src"

  install_from_repo "https://git.davidtsadler.com/dotfiles/" "custom dotfiles" "$srcdir"

  [ -d "$srcdir/dotfiles" ] && (
    cd "$srcdir/dotfiles"
    sudo -u "$name" make clean install >/dev/null 2>&1
  )
}

# 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
  );
}

install_packages() {
  curl -Ls https://git.davidtsadler.com/gohan/plain/packages.csv?h=version-1.0.0 > /tmp/packages.csv
  total=$(wc -l < /tmp/packages.csv)
  while IFS=, read -r tag program comment; do
    n=$((n+1))
    echo "$comment" | grep "^\".*\"$" >/dev/null 2>&1 && comment="$(echo "$comment" | sed "s/\(^\"\|\"$\)//g")"
    case "$tag" in
      "A") install_from_aur "$program" "$comment" ;;
      "G") install_repo "$program" "$comment" "/usr/local/src" ;;
      "#Tag") ;;
      *) install_package "$program" "$comment" ;;
    esac
  done < /tmp/packages.csv
  rm /tmp/packages.csv
}

install_package() {
  printf  "Installing \`$1\` ($n of $total). $1 $2\n"
  install_from_pacman "$1"
}

install_from_aur() {
  printf "Installing \`$1\` ($n of $total) from the AUR. $1 $2\n"
  sudo -u "$name" yay -S --noconfirm "$1" >/dev/null 2>&1
}

install_repo() {
  progname="$(basename "$1" .git)"
  dir="$3/$progname"

  printf "Installing \`$progname\` ($n of $total) via \`git\` and \`make\`. $(basename "$1") $2\n"

  install_from_repo "$1" "$2" "$3"

  [ -d "$dir" ] && (
    cd "$dir"
    make clean install >/dev/null 2>&1
  )
}

### THE ACTUAL SCRIPT ###

install_dependancies

add_user

configure_sudo

create_user_home_directory

install_yay

install_packages