summaryrefslogtreecommitdiff
path: root/src/xbs/xbs
blob: 099e1a1d7dca21391e04312492fcfb4f8cf84aff (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
#!/bin/bash

#   Copyright (c) 2013 Luke Shumaker <lukeshu@sbcglobal.net>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 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 General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.

. libremessages
. $(librelib conf)

errusage() {
	if [[ $# -gt 0 ]]; then
		error "$@"
	fi
	usage >&2
	exit 1
}

usage() {
	print 'Usage: %s [-b SYSTEM|-h] COMMAND [ARGUMENTS]' "${0##*/}"
	print 'Tool for working with arbitrary ABS-like build systems'
	echo
	print 'This is a pluggable tool. The BUILDSYSTEM it uses is configured in:'
	print '  - /etc/xbs.conf'
	print '  - ${XDG_CONFIG_HOME}/xbs.conf'
	print '  - with the `-b` flag'
	print 'Later items take precidence over earlier ones.'
	print 'It looks for a helper program at `/lib/xbs/helper-${BUILDSYSTEM}`.'
	echo
	print 'Options:'
	print '  -b BUILDSYSTEM             Use BUILDSYSTEM instead of the'
	print '                             BUILDSYSTEM configured in xbs.conf'
	print '  -h                         Show this message'
	echo
	print 'Commands:'
	print '  status                         Are there uncommited changes in `.`?'
	print '  download                       Download or update the tree'
	print '  release REPO ARCH              Release `.`'
	print '  unrelease PKGBASE REPO ARCH    Unrelease'
	print '  move FROMREPO TOREPO PKGBASE'
	print '  releasepath PKGBASE REPO ARCH  Path to staged version'
}

status() {
	if [[ ! -f PKGBUILD ]]; then
		error 'PKGBUILD not found'
		exit 1
	fi
	"$HELPER" status "$@"
}

download() {
	"$HELPER" download "$@"
}

release() {
	if [[ ! -f PKGBUILD ]]; then
		error 'PKGBUILD not found'
		exit 1
	fi
	if ! status; then
		error 'You have not committed your changes yet!'
		exit 1
	fi
	"$HELPER" release "$@"
}

unrelease() {
	"$HELPER" unrelease "$@"
}

move() {
	"$HELPER" move "$@"
}

releasepath() {
	"$HELPER" releasepath "$@"
}

main() {
	BUILDSYSTEM=''
	while getopts 'b:h' arg; do
		case $arg in
			b) BUILDSYSTEM=$OPTARG;;
			h) usage; return 0;;
			*) errusage;;
		esac
	done
	shift $(($OPTIND - 1))

	if [[ -z $BUILDSYSTEM ]]; then
		load_files xbs || return 1
		check_vars xbs BUILDSYSTEM || return 1
	fi
	
	HELPER="/lib/xbs/helper-${BUILDSYSTEM}"
	if [[ ! -x "$HELPER" ]]; then
		error 'No helper for build system found: %s' "$BUILDSYSTEM"
		return 1;
	fi

	if [[ $# -lt 1 ]]; then
		errusage "Must specify a command"
	fi

	if [[ -w / ]]; then
		error 'Run as a normal user'
	fi

	local cmd=$1; shift
	case "$cmd" in
		status|download)
			[[ $# -eq 0 ]] || errusage 'bad number of argments'
			$cmd "$@"
			;;
		release)
			[[ $# -eq 2 ]] || errusage 'bad number of argments'
			$cmd "$@"
			;;
		move|unrelease|releasepath)
			[[ $# -eq 3 ]] || errusage 'bad number of argments'
			$cmd "$@"
			;;
		*) errusage 'unknown command: %s' "$cmd";;
	esac
}

main "$@"