added 'unify' and 'pkg' commands
[util-vserver.git] / util-vserver / scripts / vpkg
1 #!/bin/bash
2
3 # Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 #  
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2, or (at your option)
8 # any later version.
9 #  
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #  
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 : ${UTIL_VSERVER_VARS:=$(dirname $0)/util-vserver-vars}
20 test -e "$UTIL_VSERVER_VARS" || {
21     echo "Can not find util-vserver installation; aborting..."
22     exit 1
23 }
24 . "$UTIL_VSERVER_VARS"
25
26 function showHelp()
27 {
28     echo \
29 $"Usage: $0 <vserver-name> <tag>
30
31 Report bugs to <$PACKAGE_BUGREPORT>."
32     exit 0
33 }
34
35 function showVersion()
36 {
37     echo $"\
38 pkgmgmt-info $PACKAGE_VERSION -- shows information about packages in vservers
39 This program is part of $PACKAGE_STRING
40
41 Copyright (C) 2004 Enrico Scholz
42 This program is free software; you may redistribute it under the terms of
43 the GNU General Public License.  This program has absolutely no warranty."
44     exit 0
45 }
46
47 function guessStyle()
48 {
49     local vdir=$($_VSERVER_INFO "$1" VDIR) || {
50         echo $"Can not determine vserver-root" >&2
51         exit 1
52     }
53
54     if test -e "$vdir"/etc/redhat-release -o -e "$vdir"/etc/fedora-release; then
55         style=redhat
56     elif test -e "$vdir"/etc/mandrake-release; then
57         style=mandrake
58     else
59         echo $"Can not determine packagemanagement style" >&2
60     fi
61 }
62
63 case "$1" in
64     (--help)    showHelp $(basename $0);;
65     (--version) showVersion ;;
66 esac
67
68 test "$1" -a "$2" || {
69     echo $"No vserver and/or tag given; use '--help' for more information" >&2
70     exit 1
71 }
72
73 vserver=$1
74 tag=$2
75 shift 2
76
77 case "$tag" in
78     get-conffiles|install)      ;;
79     *)          echo $"Unsupport tag '$tag'" >&2; exit 1;;
80 esac
81
82 cfgdir=$($_VSERVER_INFO "$vserver" APPDIR pkgmgmt) || {
83     echo $"Package management not configured for vserver '$vserver'" >&2
84     exit 1
85 }
86
87 vdir=$($_VSERVER_INFO "$1" VDIR) || :
88
89 style=
90 is_external=
91 {   read style <"$cfgdir"/style; } 2>/dev/null
92 test "$style" || guessStyle "$vserver"
93
94 test -e "$cfgdir"/internal || is_external=1
95
96
97 cmd=()
98
99 case "$style" in
100     (redhat|mandrake)
101         rpm_param=
102         apt_param=
103         case "$tag" in
104             ## rpm outputs sometimes '(contains no files)', so return
105             ## only the valid output
106             (get-conffiles)
107                 rpm_param=( -qac --pipe "sed '\!^/!p;d'" );;
108             (install)
109                 rpm_param=( -Uvh "$@" )
110                 apt_param=( install "$@" )
111                 ;;
112         esac
113         
114         if test "$is_external"; then
115             have_apt=1
116             test -d "$cfgdir"/base/apt -o -d "$cfgdir"/aptetc || have_apt=
117         else
118             have_apt=
119             for i in /bin /usr/bin /usr/local/bin; do
120                 test ! -x "$vdir$i"/apt-get || { have_apt=1; break; }
121             done
122         fi
123         
124         if test "$is_external"; then
125             if test "$have_apt" -a "$apt_param"; then
126                 cmd=( "$_VAPT_GET" "$vserver" -- "${apt_param[@]}" )
127             else
128                 cmd=( "$_VRPM" "$vserver" -- "${rpm_param[@]}" )
129             fi
130         else
131             if test "$have_apt" -a "$apt_param"; then
132                 cmd=( "$_VSERVER" --silent "$vserver" exec apt-get "${apt_param[@]}" )
133             else
134                 cmd=( "$_VSERVER" --silent "$vserver" exec rpm "${rpm_param[@]}" )
135             fi
136         fi
137         ;;
138 esac
139
140 export LANG=C
141 exec "${cmd[@]}"