#! /bin/sh
# $Id: create-var-image,v 1.2 2003/11/22 03:00:14 ensc Exp $

# Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
#  
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#  
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#  
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.


dst=$1
shift

umask 022

d=$(mktemp -d /tmp/var-image.XXXXXX)
trap "rm -rf $d" EXIT

cd $d
rpm="rpm --dbpath `pwd`/var/lib/rpm"

mkdir -p var/{cache,db,lib/{mach,games,misc,rpm,djinni/run},local,lock/subsys,log,mail,nis,opt,preserve,run,spool/{lpd,mail},tmp,yp}
chmod 1777 var/tmp

ln -s ../lib/mach/cache var/cache/mach

$rpm --initdb

# Workaround for https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=106057
python -c '
import bsddb
for i in ("Basenames", "Conflictname", "Filemd5s",
          "Group", "Name", "Packages", "Providename",
	  "Pubkeys", "Requirename",
	  "Sha1header", "Sigmd5", "Triggername"):
    bsddb.hashopen("var/lib/rpm/%s" % i, "c")

for i in ("Dirnames", "Installtid", "Provideversion",
          "Requireversion",):
    bsddb.btopen("var/lib/rpm/%s" % i, "c")
'

$rpm --import "$@"
$rpm --rebuilddb

tar czf $dst "." --owner=root --group=root
