1 |
#!/bin/sh |
2 |
|
3 |
# apt-iselect: interactive apt-cache search tool |
4 |
# |
5 |
# Dobrica Pavlinusic <dpavlin@rot13.org> |
6 |
# http://www.rot13.org/~dpavlin/apt-iselect.html |
7 |
# |
8 |
# 2003-08-02 first version |
9 |
# 2003-08-03 using mktemp to create temp files, auto-install iselect |
10 |
# 2003-08-04 catch signals so temp files are not left behind, added |
11 |
# enter new search at end of results, autoconfig |
12 |
# 2003-08-06 changed delimiter to \s/ to prevent conflict with e-mails |
13 |
# 2003-08-15 automatically run apt-get update if there is no package list |
14 |
# (e.g. changing apt/sources.list without update), cut lines |
15 |
# to 80 chars before sending to iselect (bug workaround) |
16 |
# 2003-08-31 changed delimiters to {}, q on detail page brings back list of |
17 |
# results, highlighted search word(s) in package details |
18 |
# 2003-09-01 changed script to work with any POSIX complient shell, and not |
19 |
# only bash (thanks to Ulrich Doehner for bug report and |
20 |
# suggestions), added support to use su if sudo is not |
21 |
# installed, fixed search with more than one word (thanks |
22 |
# to Tobias Gruetzmacher who reported this bug) |
23 |
# 2003-09-04 moved do_sudo before first call (fixes bug when trying to |
24 |
# install iselect) |
25 |
# 2003-10-09 allow multi-selection of packages to view details |
26 |
# 2003-11-13 fix for RedHat 9.0 mktemp bug suggested by Dobes Vandermeer |
27 |
# 2004-07-27 allow multi-selection of packages to install |
28 |
# 2004-12-04 add quick install options |
29 |
# 2004-12-06 fixed multi-word search |
30 |
# |
31 |
# I know it's ugly, but it's still faster than aptitude :-) |
32 |
# |
33 |
# It will automatically use sudo if installed or require user to enter root |
34 |
# password when needed. It will also install iselect deb package |
35 |
# if it's not already there. |
36 |
# |
37 |
# WARNING: due to iselect limitation, maximum number of results is |
38 |
# 1020 and maximum length of all results is 1048576 bytes |
39 |
|
40 |
if [ ! -z "`which sudo`" ] ; then |
41 |
use_sudo=1 |
42 |
else |
43 |
use_sudo=0 |
44 |
fi |
45 |
|
46 |
do_sudo() { |
47 |
msg=$1 ; shift |
48 |
if [ "$use_sudo" = 1 ] ; then |
49 |
sudo -p "$msg, please enter your password: " $* |
50 |
else |
51 |
if [ "`id -u`" != 0 ] ; then |
52 |
echo "$msg, please enter root password..." |
53 |
fi |
54 |
su -c "$*" |
55 |
fi |
56 |
} |
57 |
|
58 |
if [ -z "`which iselect`" ] ; then |
59 |
echo "You really need iselect for apt-iselect, installing..." |
60 |
do_sudo "Installing iselect" apt-get install iselect |
61 |
fi |
62 |
|
63 |
if [ -z "$*" ] ; then |
64 |
echo "Usage: $0 [search pattern for apt-cache search]" |
65 |
exit 1 |
66 |
fi |
67 |
|
68 |
res=`mktemp /tmp/tmp-res.XXXXXX` || ( echo "Can't create temp file!" ; exit 1 ) |
69 |
res2=`mktemp /tmp/tmp-res2.XXXXXX` || ( echo "Can't create temp file!" ; rmtemp ; exit 1 ) |
70 |
sel=`mktemp /tmp/tmp-sel.XXXXXX` || ( echo "Can't create temp file!" ; rmtemp ; exit 1 ) |
71 |
pkg=`mktemp /tmp/tmp-pkg.XXXXXX` || ( echo "Can't create temp file!" ; rmtemp ; exit 1 ) |
72 |
|
73 |
rmtemp() { |
74 |
rm -f $res |
75 |
rm -f $res2 |
76 |
test -f $sel && rm -f $sel |
77 |
rm -f $pkg |
78 |
} |
79 |
|
80 |
# need to update apt cache? |
81 |
LANG=C apt-cache stats 2>&1 | grep "^W: Couldn't stat source package list" >/dev/null && do_sudo "apt-get update needed" apt-get update |
82 |
|
83 |
trap 'rmtemp; exit 1' INT QUIT TERM SEGV |
84 |
|
85 |
apt_cache_search() { |
86 |
search_words="$*" |
87 |
|
88 |
echo "Searching apt-cache for \"$search_words\"..." |
89 |
|
90 |
apt-cache search "$@" | head -1020 > $res |
91 |
nr=`wc -l $res | sed 's/^ *//' | cut -d" " -f1` |
92 |
if [ $nr = 0 ] ; then |
93 |
echo "No results for \"$search_words\"" > $res2 |
94 |
nr=0 |
95 |
else |
96 |
echo "$nr results for \"$search_words\", enter new search {s:search=%[Search for]S}" > $res2 |
97 |
echo >> $res2 |
98 |
cat $res | sed 's/^/{s}/' >> $res2 |
99 |
echo >> $res2 |
100 |
echo "{s:_quick_install_}Install all selected" >> $res2 |
101 |
fi |
102 |
echo >> $res2 |
103 |
echo "Enter new apt-cache search {s:search=%[Search for]S}" >> $res2 |
104 |
} |
105 |
|
106 |
apt_cache_search "$@" |
107 |
|
108 |
pkg_nr=3 |
109 |
loop=1 |
110 |
|
111 |
while [ $loop = 1 ] ; do |
112 |
|
113 |
loop=0; |
114 |
iselect -m -d '{,}' -P -p $pkg_nr -n "apt-iselect: $nr results for \"$search_words\"" < $res2 > $sel |
115 |
|
116 |
if [ ! -s $sel ] ; then |
117 |
exit 0; |
118 |
elif tmp=`grep search= <$sel 2>/dev/null` ; then |
119 |
apt_cache_search `echo $tmp | grep search= | cut -d= -f2` |
120 |
loop=1 |
121 |
elif tmp=`grep -i ':_quick_install_' $sel` ; then |
122 |
debs=`grep -v ':_quick_install_' $sel | cut -d: -f2 | cut -d" " -f1 | xargs echo` |
123 |
do_sudo "Installing '$debs'" apt-get install $debs |
124 |
else |
125 |
# not search, find packages info |
126 |
|
127 |
echo '{s}Back to search results' > $pkg |
128 |
echo >> $pkg |
129 |
hl_regex=`echo $search_words | sed -e 's/ /|/' -e 's,^,s#(,' -e 's,$,)#{b}\\\1{/b}#g,'` |
130 |
|
131 |
cat "$sel" | while read tmp ; do |
132 |
|
133 |
pkg_nr=`echo $tmp | cut -d: -f1` |
134 |
pkg_full=`echo $tmp | cut -d: -f2` |
135 |
pkg_name=`echo $pkg_full | cut -d" " -f1` |
136 |
pkg_list="$pkg_list $pkg_name" |
137 |
|
138 |
apt-cache show "$pkg_name" | cut -c-128 | sed 's/^\(Package: \)/{s}\1/' | sed -r "$hl_regex" | sed '1,3 s,{/*b},,g' >> $pkg |
139 |
|
140 |
done |
141 |
echo '{s}Back to search results' >> $pkg |
142 |
|
143 |
tmp=`iselect -d '{,}' -n "Packages: $pkg_list" -m < $pkg` |
144 |
if echo $tmp | grep -i back >/dev/null ; then |
145 |
loop=1 |
146 |
elif echo $tmp | grep '^Package: ' >/dev/null ; then |
147 |
deb=`echo $tmp | sed -e 's,{b/*},,g' -e 's,Package: *,,g'` |
148 |
do_sudo "Installing '$deb'" apt-get install $deb |
149 |
elif [ -z "$tmp" ] ; then |
150 |
loop=1 |
151 |
else |
152 |
echo -n "Can't parse output '$tmp' from iselect! Press enter to continue..." |
153 |
read tmp |
154 |
loop=1 |
155 |
fi |
156 |
fi |
157 |
done |
158 |
|
159 |
rmtemp |