aboutsummaryrefslogtreecommitdiff
path: root/elvi/slack
blob: d3e2e7d22345f837a8c48802605a8ab3b5b2eb8d (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
#!/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

if [ "$( type -p wget )" = "" ]; then
	err "wget is required for this. please install it."
fi

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
	# TODO: rewrite to use sed or awk? maybe not that big a deal.
	sitevers="$(
		wget -qO- "https://packages.slackware.com" | \
			perl -ne 'print "$1 " while (/<option value="slackware-([\d.]+)"/go)'
	)"

	# 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).
                                For .0 releases, include the .0 (eg 14.0, not 14).
  -C, -cur                      Same as -v=current
  -32/-i586, -64/-x86_64        Architecture (default -$defarch)
  -pa, -pkg, -P                 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 -sS)
  -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, the only valid values
    are "64" and "".
EOF
	w3_global_usage
}

# have to use -pa/-pkg/-P for "package", not -p as it conflicts with
# the global -p/-print
# -v=<version> does NOT conflict with global -version
w3_parse_option_hook() {
	opt="$1"
	optarg="$2"
	case "$opt" in
		-v*=*)        SURFRAW_slack_ver="$optarg"      ;;
		-C*|-cur*)    SURFRAW_slack_ver="current"      ;;
		-32|-i?86)    SURFRAW_slack_arch=""            ;;
		-64|x86_64)   SURFRAW_slack_arch="64"          ;;
		-pa*|-pk*|-P) 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
}

w3_config
w3_parse_args "$@"

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

# try to make content search more user-friendlier.
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 sed.

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