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 |
# |
30 |
# I know it's ugly, but it's still faster than aptitude :-) |
31 |
# |
32 |
# It will automatically use sudo if installed or require user to enter root |
33 |
# password when needed. It will also install iselect deb package |
34 |
# if it's not already there. |
35 |
# |
36 |
# WARNING: due to iselect limitation, maximum number of results is |
37 |
# 1020 and maximum length of all results is 1048576 bytes |
38 |
|
39 |
if [ ! -z "`which sudo`" ] ; then |
40 |
use_sudo=1 |
41 |
else |
42 |
use_sudo=0 |
43 |
fi |
44 |
|
45 |
do_sudo() { |
46 |
msg=$1 ; shift |
47 |
if [ "$use_sudo" = 1 ] ; then |
48 |
sudo -p "$msg, please enter your password: " $* |
49 |
else |
50 |
if [ "`id -u`" != 0 ] ; then |
51 |
echo "$msg, please enter root password..." |
52 |
fi |
53 |
su -c "$*" |
54 |
fi |
55 |
} |
56 |
|
57 |
if [ -z "`which iselect`" ] ; then |
58 |
echo "You really need iselect for apt-iselect, installing..." |
59 |
do_sudo "Installing iselect" apt-get install iselect |
60 |
fi |
61 |
|
62 |
if [ -z "$*" ] ; then |
63 |
echo "Usage: $0 [search pattern for apt-cache search]" |
64 |
exit 1 |
65 |
fi |
66 |
|
67 |
res=`mktemp /tmp/tmp-res.XXXXXX` || ( echo "Can't create temp file!" ; exit 1 ) |
68 |
res2=`mktemp /tmp/tmp-res2.XXXXXX` || ( echo "Can't create temp file!" ; rmtemp ; exit 1 ) |
69 |
sel=`mktemp /tmp/tmp-sel.XXXXXX` || ( echo "Can't create temp file!" ; rmtemp ; exit 1 ) |
70 |
pkg=`mktemp /tmp/tmp-pkg.XXXXXX` || ( echo "Can't create temp file!" ; rmtemp ; exit 1 ) |
71 |
|
72 |
rmtemp() { |
73 |
rm -f $res |
74 |
rm -f $res2 |
75 |
test -f $sel && rm -f $sel |
76 |
rm -f $pkg |
77 |
} |
78 |
|
79 |
# need to update apt cache? |
80 |
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 |
81 |
|
82 |
trap 'rmtemp; exit 1' INT QUIT TERM SEGV |
83 |
|
84 |
apt_cache_search() { |
85 |
search_words="$@" |
86 |
|
87 |
echo "Searching apt-cache for \"$search_words\"..." |
88 |
|
89 |
apt-cache search "$search_words" | head -1020 > $res |
90 |
nr=`wc -l $res | sed 's/^ *//' | cut -d" " -f1` |
91 |
if [ $nr = 0 ] ; then |
92 |
echo "No results for \"$search_words\"" > $res2 |
93 |
nr="no" |
94 |
else |
95 |
echo "$nr results for \"$search_words\", enter new search {s:search=%[Search for]S}" > $res2 |
96 |
echo >> $res2 |
97 |
cat $res | sed 's/^/{s}/' >> $res2 |
98 |
fi |
99 |
if [ $nr -gt 0 ] ; then |
100 |
echo >> $res2 |
101 |
echo "{s:_quick_install_}Install all selected" >> $res2 |
102 |
fi |
103 |
echo >> $res2 |
104 |
echo "Enter new apt-cache search {s:search=%[Search for]S}" >> $res2 |
105 |
} |
106 |
|
107 |
apt_cache_search "$@" |
108 |
|
109 |
pkg_nr=3 |
110 |
loop=1 |
111 |
|
112 |
while [ $loop = 1 ] ; do |
113 |
|
114 |
loop=0; |
115 |
iselect -m -d '{,}' -P -p $pkg_nr -n "apt-iselect: $nr results for \"$search_words\"" < $res2 > $sel |
116 |
|
117 |
if [ ! -s $sel ] ; then |
118 |
exit 0; |
119 |
elif tmp=`grep search= <$sel 2>/dev/null` ; then |
120 |
apt_cache_search `echo $tmp | grep search= | cut -d= -f2` |
121 |
loop=1 |
122 |
elif tmp=`grep -i ':_quick_install_' $sel` ; then |
123 |
debs=`grep -v ':_quick_install_' $sel | cut -d: -f2 | cut -d" " -f1 | xargs echo` |
124 |
do_sudo "Installing '$debs'" apt-get install $debs |
125 |
else |
126 |
# not search, find packages info |
127 |
|
128 |
echo '{s}Back to search results' > $pkg |
129 |
echo >> $pkg |
130 |
hl_regex=`echo $search_words | sed -e 's/ /|/' -e 's,^,s#(,' -e 's,$,)#{b}\\\1{/b}#g,'` |
131 |
|
132 |
cat "$sel" | while read tmp ; do |
133 |
|
134 |
pkg_nr=`echo $tmp | cut -d: -f1` |
135 |
pkg_full=`echo $tmp | cut -d: -f2` |
136 |
pkg_name=`echo $pkg_full | cut -d" " -f1` |
137 |
pkg_list="$pkg_list $pkg_name" |
138 |
|
139 |
apt-cache show "$pkg_name" | cut -c-128 | sed 's/^\(Package: \)/{s}\1/' | sed -r "$hl_regex" | sed '1,3 s,{/*b},,g' >> $pkg |
140 |
|
141 |
done |
142 |
echo '{s}Back to search results' >> $pkg |
143 |
|
144 |
tmp=`iselect -d '{,}' -n "Packages: $pkg_list" -m < $pkg` |
145 |
if echo $tmp | grep -i back >/dev/null ; then |
146 |
loop=1 |
147 |
elif echo $tmp | grep '^Package: ' >/dev/null ; then |
148 |
deb=`echo $tmp | sed -e 's,{b/*},,g' -e 's,Package: *,,g'` |
149 |
do_sudo "Installing '$deb'" apt-get install $deb |
150 |
elif [ -z "$tmp" ] ; then |
151 |
loop=1 |
152 |
else |
153 |
echo -n "Can't parse output '$tmp' from iselect! Press enter to continue..." |
154 |
read tmp |
155 |
loop=1 |
156 |
fi |
157 |
fi |
158 |
done |
159 |
|
160 |
rmtemp |