summaryrefslogtreecommitdiff
path: root/examples/bootstrap
blob: 6d4013ed1fe4365bf4392e2b62656e9b0cbe6066 (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
#!/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 pkglist_cgi=0
typeset PKG_ROOT PKG_DIR default_path='~/packages'
: ${XDG_CONFIG_HOME=$HOME/.config}

while [[ $1 == --* ]] {
	case $1 in
		--pkglist-cgi)
			pkglist_cgi=1
			pkglist_cgi_url=$2
			shift
		;;
	esac
	shift
}

if [[ -n $1 ]] {
	PKG_ROOT=$1
	PKG_DIR=${~${2-$default_path}}
} else {
	cat <<- meow
		Usage: ./bootstrap [options] PKG_ROOT [PKG_DIR]
		PKG_ROOT is an URI, either of the form proto://host/path, or just /path
		         Note: The path must be absolute, it may not contain a literal ~
		PKG_DIR is the path where caretaker and all further packages will be installed,
		        by default $default_path

		Options:

		  --pkglist-cgi URL
		      Get the package list from URL - most useful with a git:// PKG_ROOT
	meow
	exit 100
}

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

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

echo 'Fetching the caretaker package...'
cd $PKG_DIR
git clone $PKG_ROOT/caretaker caretaker
cd caretaker

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

cat > $XDG_CONFIG_HOME/caretaker/caretaker.conf <<- flurbl
	PKG_ROOT=($PKG_ROOT)
	PKG_DIR="${PKG_DIR/$HOME/\$HOME}"
flurbl

if (( pkglist_cgi )) {

	if which curl &> /dev/null; then
		getcmd='curl -s'
	elif which wget &> /dev/null; then
		getcmd='wget -q -O -'
	else
		print STDERR "Unable to find a proper download program, fix $PKG_DIR/.pkglist\n"
		getcmd='echo fixme >&2; exit 1;'
	fi

	cat >> $XDG_CONFIG_HOME/caretaker/caretaker.conf <<-flurbl
		PKGLIST_LOCAL=1
		PKGLIST_PATH=$PKG_DIR/.pkglist
	flurbl

	cat > $PKG_DIR/.pkglist <<-flurbl
		#!/bin/sh
		$getcmd $pkglist_cgi_url
	flurbl

	chmod +x $PKG_DIR/.pkglist
}

echo 'Installing caretaker package'
rehash

bin/checklinks
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
}

if [[ $PATH != *$HOME/bin* ]] {
	cat <<- tac
		Note: You may need to change your PATH to include ~/bin,
		otherwise certain parts of caretaker will not work.
	tac
}