-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkeg
executable file
·124 lines (94 loc) · 2.25 KB
/
keg
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
#!/bin/bash
if [ `whoami` != "root" ]; then
echo "No root, no keg!"
exit 1
fi
UPD_INTERVAL=86400
CMD_DPKG=`which dpkg`
CMD_APTGET=`which apt-get`
CMD_APTCACHE=`which apt-cache`
CMD_APTFILE=`which apt-file`
[ -z $CMD_APTFILE ] && USE_APTFILE=0 || USE_APTFILE=1
CMD_AWK=`which awk`
function check_update {
if [[ -a ~/.keg.update && ! -f ~/.keg.update ]]; then
echo "Aborting! ~/.keg.update is not a regular file."
exit 1
fi
nextupd=0
now=`date +%s`
if [ -a ~/.keg.update ]; then
nextupd=`cat ~/.keg.update`
fi
if [ $nextupd -lt $now ]; then
echo -n "Updating cache..."
$CMD_APTGET update > /dev/null
if [ $USE_APTFILE -eq "1" ]; then
$CMD_APTFILE update > /dev/null
fi
echo "done."
future=$(($now + $UPD_INTERVAL))
echo $future > ~/.keg.update
fi
}
function show_help {
cat <<EOM
Example usage:
keg list [<filter>]
Shows a list of all installed packages.
If the optional filter param is given only packages matching filter is listed.
keg install <package>
Will download and install <package>.
keg uninstall <package>
Will uninstall the specified package.
keg search <pattern>
Will search for and list any package that matches <pattern>.
keg provides <file>
Will search for packages that provides the file <file>.
keg info <package>
Will show some information about the package.
EOM
}
case "$1" in
list)
$CMD_DPKG -l $2 | $CMD_AWK '{ if ( NR > 5 && $1 == "ii" ) print $2, "(" $3 ")"; }'
;;
install)
check_update
$CMD_APTGET install $2
;;
uninstall)
check_update
$CMD_APTGET --purge remove $2
;;
search)
check_update
$CMD_APTCACHE search $2
;;
provides)
$CMD_DPKG -S $2 2> /dev/null
retval=$?
if [[ $retval != "0" ]]; then
retval=0
if [[ $USE_APTFILE = "1" ]]; then
check_update
$CMD_APTFILE search $2 | $CMD_AWK '{ print $1, $2; }END { if ( NR == 0 ) exit 1; }'
retval=$?
else
echo "None of your installed packages provides $2"
echo "If you install apt-file we could search in packages that haven't been installed."
echo "Type: keg install apt-file to install."
fi
fi
if [ $retval -gt 0 ]; then
echo Could not find any packages providing $2
fi
;;
info)
check_update
$CMD_APTCACHE show $2
;;
*)
show_help
;;
esac