aboutsummaryrefslogtreecommitdiff
path: root/.local/bin/pkgsrc
blob: e6b1a7e56b2c6ce6083e1ac88c15dee2cabefc7f (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
211
212
213
214
215
216
217
218
219
220
221
#!/bin/sh

# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.

readonly PKGSRC_ROOT="$HOME/.local/src/pkgsrc" # TODO: Make configurable
readonly PKGSRC_DURATIONS="$XDG_DATA_HOME/pkgsrc/durations"

main()
{
	PKGSRC_INIT="$(time_unix)"

	# Handle opts
	while getopts ":h" opt
	do
		case "$opt" in
			h) usage && exit 0 ;;
			*)
				printf "Invalid option passed: %s\n" "$OPTARG" >&2
				;;
		esac
	done

	shift $(( OPTIND - 1 ))

	if [ $# -lt 1 ]
	then
		usage
		exit 1
	fi

	PKGSRC_ACTION="action_$1" ; shift

	if ! command -V "$PKGSRC_ACTION" 2>/dev/null | grep -qwi function
	then
		usage
		exit 1
	fi

	$PKGSRC_ACTION "$@"
}

action_add()
{
	mkdir -p -- "$PKGSRC_DURATIONS"

	for package in "$@"
	do
		cd -- "$PKGSRC_ROOT"

		# cd wont fail here if pkg_resolve had no output
		if ! cd -- "$(pkg_resolve "$package")"
		then
			printf "No package found for %s\n" "$package"
			continue
		fi

		bmake install \
			&& bmake clean-depends \
			&& bmake clean \
			|| continue

		printf "%d\n" "$(( $(time_unix) - PKGSRC_INIT ))" \
			>> "$PKGSRC_DURATIONS/$package"
	done

	printf "Installation took %s\n" "$(time_duration "$PKGSRC_INIT" "$(time_unix)")"
}

action_del()
{
	for package in "$@"
	do
		cd -- "$PKGSRC_ROOT"

		if ! cd -- "$(pkg_resolve "$package")"
		then
			printf "No package found for %s\n" "$package"
			continue
		fi

		bmake deinstall
	done
}

action_grep()
{
	cd -- "$PKGSRC_ROOT"

	find . -maxdepth 2 -type d \
		| cut -c 3- \
		| grep "$@"
}

action_init()
{
	export CVS_RSH="ssh"

	if [ ! -d "$PKGSRC_ROOT" ]
	then
		mkdir -p -- "${PKGSRC_ROOT%/*}"
		cd -- "${PKGSRC_ROOT%/*}"
		cvs -d anoncvs@anoncvs.NetBSD.org:/cvsroot checkout -P pkgsrc || die 1
	fi

	if [ -d "$HOME/.pkgsrc" ]
	then
		printf "pkgsrc seems to have been initialized already at $HOME/.pkgsrc\n"
		exit 1
	fi

	cd -- "$PKGSRC_ROOT/bootstrap"
	rm -fr -- work
	./bootstrap --unprivileged --prefix="$HOME/.pkgsrc" # TODO: Make configurable

	printf "Bootstrapped in %s\n" "$(time_duration "$PKGSRC_INIT" "$(time_unix)")"
}

action_opts()
{
	cd -- "$(pkg_resolve "$1")" || die "No package found for $1"

	bmake show-options
}

action_time()
{
	for package in "$@"
	do
		if [ ! -f "$PKGSRC_DURATIONS/$package" ]
		then
			printf "%s: No timings\n" "$package"
			continue
		fi

		package_min="$(sort "$PKGSRC_DURATIONS/$package" | head -n 1)"
		package_max="$(sort "$PKGSRC_DURATIONS/$package" | tail -n 1)"
		package_avg="$(awk '{ sum += $1 } END { printf("%.0f", (sum / NR)) }' "$PKGSRC_DURATIONS/$package")"

		printf "%s: %s / %s / %s \n" \
			"$package" \
			"$(time_duration "$package_min")" \
			"$(time_duration "$package_avg")" \
			"$(time_duration "$package_max")"
	done
}

action_update()
{
	cd -- "$PKGSRC_ROOT"

	cvs up

	printf "Updated in %s\n" "$(time_duration "$PKGSRC_INIT" "$(time_unix)")"
}

usage()
{
	cat <<EOF
Usage:
	${0##*/} -h <action> [target]

Interact with pkgsrc.

Options:
	-h  Show this help text and exit.

Actions:
	add    Install one or more TARGETs.
	del    Uninstall one or more TARGETs.
	grep   Search for TARGET.
	init   Initialize the pkgsrc repository, and run the bootstrap script.
	opts   Show the options of TARGET.
	time   Show min, avg, max installation times for TARGETs.
	update Update the pkgsrc repository.
EOF
}

#
# Helper utilities
#

die()
{
	printf "%s\n" "$*" >&2
	exit 1
}

pkg_resolve()
{
	readlink -f "$PKGSRC_ROOT"/*/"$1" || readlink -f "$PKGSRC_ROOT/$1" # What if both fail?
}

time_unix()
{
	awk 'BEGIN{srand();print srand()}'
}

time_duration()
{
	if [ ! -z "$2" ]
	then
		elapsed="$(( $2 - $1 ))"
	else
		elapsed="$1"
	fi

	printf "%dh %02dm %02ds" \
		"$(( elapsed / 60 / 60 ))" \
		"$(( elapsed / 60 % 60))" \
		"$(( elapsed % 60 ))"
}

main "$@"