summaryrefslogtreecommitdiff
path: root/bin/pkg
blob: 54014927a329586046ad1adfc7e1edf57b3aa9cf (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
#!/usr/bin/env zsh
PDIR="$HOME/packages"
PKG_ROOT="ssh://derf.homelinux.org/packages"
VCS_CMD="hg"
VCS_OPTIONS="--quiet"
VCS_ADD="clone"
VCS_STATUS="status"
VCS_UPDATE="pull"
VCS_UPDATE_OPTIONS="--update"
CL_OPTIONS="-q"
info=$'\e[0;36m'
error=$'\e[1;31m'
reset=$'\e[0m'

info () {
	echo "${info}$*${reset}"
}

error () {
	echo "${error}$*${reset}"
	exit 100
}

if [ ! -d $PDIR ]; then
	error "$PDIR not found!!"
fi

check_deps () {
	[ -r $PDIR/$1/.deps ] || return 0
	DEPS=($(cat $PDIR/$1/.deps))
	INSTALL=()
	for dep in $DEPS; {
		if [ "$dep" = "$1" ]; then
			error "This package depends on itself. Therefore, I'm considering it borked. Not installing."
		fi
		if [ ! -d $PDIR/$dep ]; then
			echo -n "$1 depends on $dep. Install dependency? [Y/n] "
			read
			if [ ! "$REPLY" = 'n' ]; then
				INSTALL+="$dep"
			fi
		fi
	}
	if [ -n "$INSTALL" ]; then
		for pkg in $INSTALL; {
			pkg_add "$pkg"
		}
	fi
}

pkg_add () {
	if [ -d $PDIR/$1 ]; then
		info "Package '$1' is already installed!"
		return 100
	fi
	cd $PDIR || return 255
	info "Retrieving package $1..."
	$VCS_CMD $VCS_OPTIONS $VCS_ADD $PKG_ROOT/$1 || return 255
	if [ -f $PDIR/$1/hooks/post-add ]; then
		info 'Executing post-add hook'
		. $PDIR/$1/hooks/post-add
	fi
	check_deps "$1"
	info 'Checking symlinks...'
	cd $PDIR/$1
	checklinks $CL_OPTIONS
	return 0
}

pkg_remove () {
	if [ ! -d $PDIR/$1 ]; then
		info "Package '$1' is not installed!"
		return 100
	fi
	if [ -f $PDIR/$1/hooks/pre-remove ]; then
		info 'Executing pre-remove hook'
		. $PDIR/$1/hooks/pre-remove
	fi
	rm -r $PDIR/$1
	info "Package removed."
}

pkg_update () {
	cd $PDIR/$1
	info "Updating package $1"
	$VCS_CMD $VCS_OPTIONS $VCS_UPDATE $VCS_UPDATE_OPTIONS
	checklinks $CL_OPTIONS
	if [ -f hooks/post-update ]; then
		info 'Executing post-update hook'
		. hooks/post-update
	fi
	cd $PDIR
}

pkg_update_wrapper () {
	if [ -n "$1" ]; then
		pkg_update "$1"
	else
		cd $PDIR
		for i in *(/); {
			pkg_update "$i"
		}
	fi
}

pkg_changesrc () {
	cd $PDIR
	for i in *(/); {
		if [ -f $i/.hg/hgrc ]; then
			sed -ri "s!(default = )[^:]*://[^/]*/!\1$1/!" $i/.hg/hgrc
		fi
	}
}

pkg_list_installed () {
	=ls -1 $PDIR
}

pkg_list_available () {
	if grep "^ssh" <<< "$PKG_ROOT" &> /dev/null; then
		ssh -q $(sed 's!^ssh://!!' <<< "$PKG_ROOT" | sed -r 's!^([^/]*)/.*$!\1!') 'ls -1 packages'
	fi
}

pkg_status () {
	cd $PDIR/$1
	info "Checking $1 status..."
	check_deps $1
	$VCS_CMD $VCS_STATUS
}

pkg_status_wrapper () {
	if [ -n "$1" ]; then
		pkg_status "$1"
	else
		cd $PDIR
		for i in *(/); {
			pkg_status "$i"
		}
	fi
}

case "$1" in
	add) pkg_add "$2" ;;
	changeroot) pkg_changesrc "$2" ;;
	delete) pkg_remove "$2" ;;
	install) pkg_add "$2" ;;
	list) pkg_list_installed ;;
	list-all) pkg_list_available ;;
	remove) pkg_remove "$2" ;;
	status) pkg_status_wrapper "$2" ;;
	update) pkg_update_wrapper "$2" ;;
esac