summaryrefslogtreecommitdiff
path: root/playbooks.d/user-tyil/playbook.bash
blob: 0ca042575267e864a9471d3fe7bce55c5d3c1979 (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
#!/usr/bin/env bash

playbook_add() {
	notice "$BASHTARD_PLAYBOOK" "Creating user 'tyil'"
	useradd \
		--no-create-home \
		tyil

	notice "$BASHTARD_PLAYBOOK" "Cloning dotfiles"
	git clone "$(config "users.tyil.git.url")" "$(config "users.tyil.home")"
	chown -R tyil:tyil "$(config "users.tyil.home")"

	playbook_sync
}

playbook_sync() {
	local packages=(
		bash
		git
		sudo
		zsh
	)
	local sharedir="$BASHTARD_ETCDIR/playbooks.d/$BASHTARD_PLAYBOOK/share"

	notice "$BASHTARD_PLAYBOOK" "Installing all desired utilities"
	for package in "${packages[@]}"
	do
		pkg install "$package"
	done

	if [[ $BASHTARD_COMMAND != "add" ]]
	then
		notice "$BASHTARD_PLAYBOOK" "Updating dotfiles"
		sudo -u tyil \
			git -C "$(config "users.tyil.home")" stash
		sudo -u tyil \
			git -C "$(config "users.tyil.home")" pull origin "$(config "users.tyil.git.branch")"
		sudo -u tyil \
			git -C "$(config "users.tyil.home")" stash pop
	fi

	notice "$BASHTARD_PLAYBOOK" "Updating all 3rd party git repositories"
	grep -v '^#' "$sharedir/gittab" | while read -r tab dir
	do
		dir="$(config "users.tyil.home")/$dir"

		grep -v '^#' "$sharedir/gittab.d/$tab" | while read -r name repo branch
		do
			notice "$BASHTARD_PLAYBOOK" "Updating $dir/$name"

			if [[ ! -d "$dir/$name" ]]
			then
				sudo -u tyil git clone \
					--single-branch \
					--branch "$branch" \
					--depth 1 \
					"$repo" \
					"$dir/$name"
			fi

			sudo -u tyil git -C "$dir/$name" checkout "$branch"
			sudo -u tyil git -C "$dir/$name" pull "$repo" "$branch"
		done
	done
}

playbook_del() {
	userdel tyil
	rm -fr --one-file-system -- "$(config "users.tyil.home")"
}