aboutsummaryrefslogtreecommitdiff
path: root/slack
blob: 8f8b644204f0932cfe994b8c1d4fe33d6bd80128 (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
222
223
224
225
226
227
228
229
#!/bin/sh

# elvis: slack           -- Search Slackware.com packages

# Author: B. Watson (yalhcru at gmail)
# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# Unfortunately we can't use GET with this form. Well, we can,
# except the checkboxes for "search sets" don't work. So it has
# to be a POST request, which means this elvis depends on wget. I
# suppose it could support curl instead (or in addition) but I don't
# see the point.

# The source search is pretty useless, but that's not this script's fault.

. surfraw || exit 1

check_bins() {
	local i missing
	for i in "$@"; do
		if [ -z "$( type -p $i )" ]; then
			warn "required external binary $i not found on PATH."
			missing="${missing}$i "
		fi
	done
	if [ -n "$missing" ]; then
		err "please install the missing binaries, or adjust PATH (currently \"$PATH\")"
	fi
}

url="https://packages.slackware.com/"

get_slack_ver() {
	local ver sitevers i

	# initially set to the currently running version, if we can.
	# unfortunately, this can be wrong: if someone upgraded to -current
	# but didn't upgrade aaa_base, it'll still say 14.2 (or whatever they
	# upgraded from).
	if [ -e /etc/slackware-version ]; then
		ver="$( cut -d' ' -f2 < /etc/slackware-version )"
	else
		# we're not on slackware, assume current
		echo "current"
		return
	fi

	# now check the site, make sure it's a supported version. this would
	# be easier in perl, but I wanna keep external deps to a minimum.
	sitevers="$(
		wget -qO- "$url" | \
		sed 's,<,\n<,g'  | \
		sed -n 's/<option *value *= *"slackware-\([^"]*\)".*/\1/p'
	)"

	# if we can't get the the list of versions from the site, use contents
	# of slackware-version, or current if we ain't got one. probably doesn't
	# matter, if the site is broken.
	if [ -z "$sitevers" ]; then
		echo "${ver:-current}"
		return
	fi

	# if it's a supported version, use it
	for i in $sitevers; do
		if [ "$i" = "$ver" ]; then
			echo "$ver"
			return
		fi
	done

	# fall back to current
	echo "current"
	return
}

get_slack_arch() {
	case "$( uname -m )" in
		x86_64) SURFRAW_slack_arch="64" ;;
		*)      SURFRAW_slack_arch=""   ;;
	esac
	echo "$SURFRAW_slack_arch"
}

w3_config_hook() {
	def SURFRAW_slack_ver  "$( get_slack_ver )"
	def SURFRAW_slack_arch "$( get_slack_arch )"
	def SURFRAW_slack_mode "package"
	def SURFRAW_slack_sets "eps"
}

w3_usage_hook() {
	local defarch
	[ "$SURFRAW_slack_arch" = 64 ] && defarch=64 || defarch=32
   cat <<EOF
Usage: $w3_argv0 [options] [search words]...
Description:
  Surfraw search Slackware.com packages
Local options:
  -v=<version>                  Slackware version (default $SURFRAW_slack_ver).
  -<version>                    Same as -v=<version>. E.g. -14.2
  -C, -cur                      Same as -v=current
  -32/-i586, -64/-x86_64        Architecture (default -$defarch)
  -p, -pkg                      Package search (default)
  -d, -desc                     Description search
  -l, -label                    Label search
  -c, -content                  Content search
  -s=<sets>                     Search sets, <sets> may consist of:
                                  e = extra
                                  P = pasture
                                  p = patches
                                  s = slackware
                                  S = source
                                  t = testing
                                Default: $SURFRAW_slack_sets
  -S, -src                      Search source (same as -c -s=S)
  -a, -all                      Search all sets (same as -s=ePpsSt)
Notes:
  - Default version and architecture read from the OS (/etc/slackware-version
    and uname -m). You can hardcode them in ~/.config/surfraw/conf (see below).
  - Search modes: "package" looks at package names, "description"
    looks at the .txt files named after the packages (created from the
    slack-desc), "label" looks at the first line of .txt file, and "content"
    looks at the contents of packages.
  - "content" searches are for finding out which package a file belongs to.
    Be aware that symlinks aren't found by this, although if the symlink
    exists on the local system, its target will be used instead.
  - The source search is basically useless without "content" mode. Actually
    it's pretty useless anyway.
  - Defaults can be set in the config file "~/.config/surfraw/conf". Example:
SURFRAW_slack_ver=14.2
SURFRAW_slack_arch=64
SURFRAW_slack_mode=package
SURFRAW_slack_sets=eps
    For 32-bit architecture (i586), use:
SURFRAW_slack_arch=""
    In other words do NOT set it to 32 (or i586), the only valid values
    are "64" and "".
EOF
	w3_global_usage
}

# -p conflicts with the global -p, but -print still works.
# -v=<version> does NOT conflict with global -v/-version.
w3_parse_option_hook() {
	opt="$1"
	optarg="$2"
	case "$opt" in
		-v*=*)        SURFRAW_slack_ver="$optarg"      ;;
		-32|-i?86)    SURFRAW_slack_arch=""            ;;
		-64|-x86_64)  SURFRAW_slack_arch="64"          ;;
		-[0-9]*)      SURFRAW_slack_ver=${opt/-/}      ;;
		-C*|-cur*)    SURFRAW_slack_ver="current"      ;;
		-p|-p[ak]*)   SURFRAW_slack_mode="package"     ;;
		-d*)          SURFRAW_slack_mode="description" ;;
		-l*)          SURFRAW_slack_mode="label"       ;;
		-c*)          SURFRAW_slack_mode="content"     ;;
		-s*=*)        SURFRAW_slack_sets="$optarg"     ;;
		-S*|-src*)    SURFRAW_slack_sets="S"
		              SURFRAW_slack_mode="content"     ;;
		-a*)          SURFRAW_slack_sets="ePpsSt"      ;;
		*) return 1 ;;
	esac
	return 0
}

check_bins wget sed
w3_config
w3_parse_args "$@"

# Add .0 if the user asked for e.g. version 13 or 14.
case "$SURFRAW_slack_ver" in
	current)       ;;
	[0-9]*.[0-9]*) ;;
	*)             SURFRAW_slack_ver="$SURFRAW_slack_ver.0" ;;
esac

# Try to make content search more user-friendlier:
# 1. The form expects no leading slash, so you'd have to remember to
#    search for "bin/sh" instead of "/bin/sh"...
# 2. Symlinks aren't in the search database, so if we're searching for
#    a symlink that exists locally, search for its target instead.
# Applying both the above allows us to search for e.g. "/usr/bin/gcc".
if [ "$SURFRAW_slack_mode" = "content" ]; then
	if echo "$w3_args" | grep -q ^/; then
		# resolve symlink, if possible
		w3_args_new="$( readlink -e "$w3_args" )"
		[ -n "$w3_args_new" ] && w3_args="$w3_args_new"
		# remove leading /'s since the form fails with them
		w3_args="$( echo "$w3_args" | sed 's,^/*,,' )"
	fi
fi

postdata="search=$( w3_url_of_arg $w3_args )"
postdata="${postdata}&release=slackware${SURFRAW_slack_arch}-${SURFRAW_slack_ver}"
postdata="${postdata}&mode=${SURFRAW_slack_mode}"

# Pick apart sets. Might be a cleaner way to do this.
sets="$( echo "$SURFRAW_slack_sets" | sed 's,\(.\),\1 ,g' )"
for i in $sets; do
	case "$i" in
		e) postdata="${postdata}&extra=on"       ;;
		P) postdata="${postdata}&pasture=on"     ;;
		p) postdata="${postdata}&patches=on"     ;;
		s) postdata="${postdata}&slackware=on"   ;;
		S) postdata="${postdata}&source=on"      ;;
		t) postdata="${postdata}&testing=on"     ;;
		*) err "invalid -s arg '$i'."            ;;
	esac
done

# hardcode number of results, more is always better, right?
postdata="${postdata}&result=1000"

# wget should be quiet unless -print is enabled.
[ "$SURFRAW_print" = "yes" ] || Q="-q"

# Do the post, show the results.
# All the image, css, js URLs are absolute paths so they'll load OK.
# The "Packages" and form target URLs are relative, so fix them with fugly
# sed code.

dir="$( mktemp -td sr.slack.XXXXXX )"
[ -d $dir ] || exit 1
wget $Q -O $dir/slack.html --post-data="$postdata" $url
sed -i "s,= *\"[./]\",=\"$url\",g" $dir/slack.html
w3_browse_url "file://$dir/slack.html"
[ "$SURFRAW_print" = "yes" ] && echo "POST data: $postdata"
rm -rf $dir