aboutsummaryrefslogtreecommitdiff
path: root/sbostuff.sh
blob: 84b77d916fb0f7a55568163c7b970c82ee4160cf (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
# sbostuff.sh

# bash functions for sbostuff. And I do mean *bash*: I don't know a
# completely portable way to write this stuff, and I don't use other
# shells. Patches accepted, if you're really motivated to make this
# work in your favorite shell.

# Source this file from your ~/.bashrc or similar:
# source /path/to/sbostuff/functions.sh
# ...or place this file in /etc/profile.d/ with +x permission.

if [ -e ~/.sbostuff.cfg ]; then
	source ~/.sbostuff.cfg
	export SBO_GITROOT SBO_NAME SBO_EMAIL SBO_GITBRANCH
else
	echo "sbostuff: No ~/.sbostuff.cfg, please create one."
fi

_run_editor() {
	local editor
	if [ -n "$VISUAL" ]; then
		editor="$VISUAL"
	elif [ -n "$EDITOR" ]; then
		editor="$EDITOR"
	else
		editor="vim"
	fi
	eval "$editor" $@
}

visl() {
	local file
	local editor_opts

	case "$1" in
		-h|--help)
			cat <<EOF
visl - edit the .SlackBuild, .info, README, and slack-desc in the
       current directory.

Usage: visl <opts>

The editor used is controlled by environment variables. If VISUAL
is set, it's used as the editor. Otherwise, if EDITOR is set, it's
used. If neither are set, the default is "vim".

If given, <opts> are passed to the editor as options.
EOF
			return 0 ;;
		*) editor_opts="$@" ;;
	esac

	file="$( basename "$( pwd )" )".SlackBuild
	[ ! -e "$file" ] && file="*.SlackBuild"
	_run_editor $editor_opts "$file" "${file/SlackBuild/info}" README slack-desc "$@"
}

_cdsbexp() {
	/bin/ls -d1 "$@" 2>/dev/null | grep -v /local/ | head -1
}

cdsb() {
	# FIXME: get from config file
	local dir
	local oldshopt

	case "$1" in
		-h|--help)
			cat <<EOF
cdsb - change directory to a SlackBuild.

Usage: cdsb <build>

With no <build> argument, changes to the root of the SBo tree.

<build> may be a complete build name (with or without a category), or a
partial name, which will be matched case-insensitively.
EOF
			return 0 ;;
		--) shift ;;
		-*) echo "cdsb: unknown option $1" ; return 1 ;;
	esac

	# temporarily do case-insensitive globbing
	oldshopt="$( shopt -p nocaseglob )"
	shopt -s nocaseglob

	if [ -d $SBO_GITROOT/$1 ]; then
		# category/prgnam (exact)
		cd $SBO_GITROOT/$1
	elif [ -d "$( _cdsbexp $SBO_GITROOT/*/$1)" ]; then
		# prgnam without category (exact)
		cd "$( _cdsbexp $SBO_GITROOT/*/$1)"
	else
		echo "cdsb: no exact match, guessing dir" 1>&2
		dir="$( _cdsbexp $SBO_GITROOT/*/$1* )"
		[ -z "$dir" ] && dir="$( _cdsbexp $SBO_GITROOT/*/*$1* )"
		[ -n "$dir" ] && cd "$dir" || echo "cdsb: no match" 1>&2
	fi

	eval $oldshopt
}