- moved here from sysv/
[util-vserver.git] / scripts / distrib-info
1 #!/bin/bash
2
3 # Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 # based on distrib-info by Jacques Gelinas
5 # Debian support shoe-horned in by Matthew Lavy <mml@mupsych.org>
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2, or (at your option)
10 # any later version.
11 #  
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #  
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20   
21
22 # This scripts knows about every possible distribution (well, it should)
23 # It is passed a vserver name and a key (a command). The key represent a task.
24 # It executes the command and output on stdout.
25 # For example
26 # distrib-info vserver1 pkgversion
27 # If vserver1 is a redhat system, it executes
28 # rpm -qa --queryformat "%{name}=%{version}-%{release}
29 : ${UTIL_VSERVER_VARS:=$(dirname $0)/util-vserver-vars}
30 test -e "$UTIL_VSERVER_VARS" || {
31     echo "Can not find util-vserver installation; aborting..."
32     exit 1
33 }
34 . "$UTIL_VSERVER_VARS"
35
36 if [ "$1" = "" ] ; then
37         echo distrib-info vserver-name command [ args ... ] >&2
38         echo Commands are: >&2
39         echo dumpfiles: Shows all files owned by a package >&2
40         echo pkgversion: reports all packages and their version/release >&2
41         echo unifiles: reports all unify-able file of a package >&2
42         exit 1
43 fi
44 if [ "$1" = "/" ] ; then
45         DISTDIR=/
46         CHROOTCMD=
47 elif [ -d "$1" ] ; then
48         DISTDIR=$1
49         CHROOTCMD="$SBINDIR/chroot $DISTDIR"
50 else
51         DISTDIR=$VROOTDIR/$1
52         CHROOTCMD="$SBINDIR/chroot $DISTDIR"
53 fi
54 KEY=$2
55 shift
56 shift
57 if [ -f $DIRDIR/etc/redhat-release -o -f $DISTDIR/etc/mandrake-release ] ; then
58         case $KEY in
59         pkgversion)
60                 $CHROOTCMD /bin/rpm -qa --queryformat "%{name}=%{version}-%{release}\n"
61                 ;;
62         unifiles)
63                 # We remove /etc and /var/log to make sure no special file
64                 # there will be unified
65                 $CHROOTCMD /bin/rpm -ql --dump $* \
66                         | $PKGLIBDIR/parserpmdump /etc/
67                 ;;
68         dumpfiles)
69                 $CHROOTCMD /bin/rpm -ql $*
70                 ;;
71         *)
72                 echo unknown request $KEY >&2
73                 ;;
74         esac
75 elif [ -f $DISTDIR/etc/debian_version ] ; then
76         case $KEY in
77         pkgversion)
78             $CHROOTCMD /usr/bin/dpkg-query -W \
79                 --showformat='${Package}=${Version}#${Status}\n' \
80                 | perl -pe 's/(.+)-.*/$1/' \
81                 | grep "install ok installed" | cut -d"#" -f1
82             ;;
83         unifiles)
84             echo $* | perl -pe 's/(.+)-.*/$1/' \
85                 | xargs $CHROOTCMD /usr/bin/dpkg -L \
86                 | grep -v  "^/etc\|^/var"
87             ;;
88         dumpfiles)
89             echo $* | perl -pe 's/(.+)-.*/$1/' \
90                 | xargs $CHROOTCMD /usr/bin/dpkg -L
91             ;;
92         *)
93             echo unknown request $KEY >&2
94             ;;
95         esac
96 else
97         echo Distribution not supported yet >&2
98 fi
99