summaryrefslogtreecommitdiff
path: root/examples/bootstrap
blob: e326e4b83b7138db4c621cd56396738774f8896e (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
#!/usr/bin/env zsh
# bootstrap - populate a home with the most necessary scripts
# After running this, other packages can be installed using 'ct'
# Note: Since caretaker is written in zsh, this script also is.
# This way, I don't have to check for zsh somewhere in the script,
# and also have an excuse for using zsh here :P

setopt err_exit
trap "print -P '\n%N:%i: bootstrap failed (${?})'" ZERR
typeset -i rcempty=0
typeset PKG_ROOT PKG_DIR default_path='~/packages'
: ${XDG_CONFIG_HOME=${HOME}/.config}

if ! which git &> /dev/null; then
	echo - 'It appears that git is not available on this system.'
	echo - '-Installation aborted-'
	exit 200
fi

if [[ ${#} -ge 5 ]] {
	PKG_PROTO=${1}
	PKG_USER=${2}
	PKG_HOST=${3}
	PKG_PORT=${4}
	PKG_PATH=${5}
	PKG_DIR=${~${6-${default_path}}}
	PKGLIST_PATH=""
    if [[ $PKG_PROTO == "gitolite" ]]; then
        pkglist_type="$PKG_PROTO"
        PKG_PROTO="ssh"
        PKGLIST_PATH="\${PKG_DIR}/caretaker/examples/pkglist-${pkglist_type}"
    fi
} else {
	cat <<- meow
		Usage: ./bootstrap PKG_PROTO PKG_USER PKG_HOST PKG_PORT PKG_PATH [PKG_DIR]
		PKG_PROTO : the protocol type, e.g. ssh, file or git.
		PKG_USER : user on the package root host. May be left out if it
		         is identical to the local user name.
		PKG_HOST : remote host to connect to
		PKG_PORT : remote port to connect to
		PKG_PATH : package root path on the remote host
		PKG_DIR : the path where caretaker and all further packages will be installed,
		        by default ${default_path}

		Examples: ./bootstrap ssh '' arclight 22 /home/derf/var/packages_root
		          ./bootstrap file '' '' '' /home/derf/var/packages_root
		          ./bootstrap ssh derf flux.derf0.net 22 /home/derf/var/packages_root
		          etc.
	meow
	exit 100
}

if [[ -n ${PKG_USER} ]] {
	PKG_UAH="${PKG_USER}@${PKG_HOST}"
} else {
	PKG_UAH=${PKG_HOST}
}
if [[ ${PKG_PORT} != 22 ]] {
	PKG_UAH="${PKG_UAH}:${PKG_PORT}"
}

# zsh keeps complaining about not having a configuration,
# so let's be kind and give it an empty one.
if ! [[ -e ~/.zshrc ]] {
	echo 'Creating empty zshrc'
	rcempty=1
	touch ~/.zshrc
}

echo 'Creating the basic directory structure'
mkdir -p ~/bin
path=(~/bin ${path})
mkdir -p ${PKG_DIR}/.collected/man/man{1..8}

echo 'Fetching the caretaker package...'
cd ${PKG_DIR}
git clone "${PKG_PROTO}://${PKG_UAH}/${PKG_PATH}/caretaker" caretaker
cd caretaker

echo "Writing ${XDG_CONFIG_HOME}/caretaker/caretaker.conf"
mkdir -p ${XDG_CONFIG_HOME}/caretaker

PKG_PORT_STRING=''
if [[ ${PKG_PORT} != 22 ]] {
	PKG_PORT_STRING="\nPKG_PORT='${PKG_PORT}'"
}
cat > ${XDG_CONFIG_HOME}/caretaker/caretaker.conf <<- flurbl
	PKG_DIR="${PKG_DIR/${HOME}/\${HOME\}}"
	PKG_ROOTS=(default)

	function pkgroot_default {
		PKG_PROTO='${PKG_PROTO}'
		PKG_USER='${PKG_USER}'
		PKG_HOST='${PKG_HOST}'${(g::)PKG_PORT_STRING}
		PKG_PATH='${PKG_PATH}'
flurbl
if [[ $PKGLIST_PATH != "" ]]; then
cat >> ${XDG_CONFIG_HOME}/caretaker/caretaker.conf <<- flurbl
PKGLIST_LOCAL=1
PKGLIST_PATH="${PKGLIST_PATH}"
flurbl
fi
echo '}' >>${XDG_CONFIG_HOME}/caretaker/caretaker.conf

echo 'Installing caretaker package'
rehash

bin/ct eval update_collected caretaker
bin/ct eval exec_hook caretaker post-add
bin/ct update

if (( rcempty )) {
	echo 'Removing empty zshrc'
	rm ~/.zshrc
}

cat <<- tac
	
	bootstrap finished.
	Package directory: ${PKG_DIR}
	config path      : ${XDG_CONFIG_HOME}/caretaker/caretaker.conf
tac