#!/bin/sh
#
# Copyright (C) 1998-1999 Florian La Roche
#
# convert Raw Hide (ftp://rawhide.redhat.com) from rpm to jurix binary format
#

# just a shortcut for me. you don't have to keep all your dirs
# below this subdir...
RAWHIDE=/hdc1/mirror/rawhide

# directory with the binary rpm-packages that should be converted
RPMSOURCE=$RAWHIDE/i386/RedHat/RPMS
# this script will generate a chroot-environment in this directory.
# for each package, we will even generate a chroot-environment within
# this chroot-environment. (at least until my conversion-progs get smarter.)
JUR=/hda7/rawhide

# you shouldn't need to change anything below. RPMSOURCE and JUR should be
# the only things you have to adjust and you need jurix.tgz and rpm.tgz
# to do the conversion.

# this directory below JUR will hold all new jurix binary packages
NPACK=packages
DEST=$JUR/$NPACK

# this temp directory is created and used to change the rpm into
# a jurix binary package.
RROOT=$JUR/root

# we will place the redhat scripts into this subdirectory of the new
# jurix binary packages
S=usr/lib/scripts

passwd()
{
	cat <<EOF
root::0:0:root:/root:/bin/bash
bin:*:1:1:bin:/bin:
daemon:*:2:2:daemon:/sbin:
adm:*:3:4:adm:/var/adm:
lp:*:4:7:lp:/var/spool/lpd:
sync:*:5:0:sync:/sbin:/bin/sync
shutdown:*:6:0:shutdown:/sbin:/sbin/shutdown
halt:*:7:0:halt:/sbin:/sbin/halt
mail:*:8:12:mail:/var/spool/mail:
news:*:9:13:news:/var/spool/news:
uucp:*:10:14:uucp:/var/spool/uucp:
operator:*:11:0:operator:/root:
games:*:12:100:games:/usr/games:
gopher:*:13:30:gopher:/usr/lib/gopher-data:
ftp:*:14:50:FTP User:/home/ftp:
gdm:*:42:42::/home/gdm:
postgres:*:80:80:PostgreSQL Server:/var/lib/pgsql:/bin/bash
squid:*:81:81::/var/spool/squid:/dev/null
xfs:*:83:83:X Font Server:/etc/X11/fs:/bin/false
nobody:*:99:99:Nobody:/:
EOF
}

group()
{
	cat <<EOF
root::0:root
bin::1:root,bin,daemon
daemon::2:root,bin,daemon
sys::3:root,bin,adm
adm::4:root,adm,daemon
tty::5:
disk::6:root
lp::7:daemon,lp
mem::8:
kmem::9:
wheel::10:root
mail::12:mail
news::13:news
uucp::14:uucp
man::15:
floppy::19:
games::20:
slocate::21:
console::22:
gopher::30:
dip::40:
gdm::42:
ftp::50:
postgres::80:
squid::81:
utmp::82:
xfs::83:
nobody::99:
users::100:
EOF
}

# invoking this script with "$0 --list package.rpm" will list all tags
# in package.rpm. this has nothing to do with the rest of the script,
# but is very convenient if I want to check a rpm package some more...
if test "x$1" = x--list ; then
	for i in `rpm --querytags`; do
		j=$(rpm -q --queryformat "[%{$i}\n]" -p $2)
		echo "$i=$j"
	done
	exit 0
fi

chroot_init()
{
	mkdir -p $JUR/{etc,usr/sbin} $DEST	# $JUR/lib
	mkdir -p $JUR/var/jurix/{packages,unchanged}
	passwd > $JUR/etc/passwd
	group > $JUR/etc/group
#	cat > $JUR/etc/nsswitch.conf <<EOF
#passwd:         files
#shadow:         files
#group:          files
#hosts:          files dns
#networks:       files dns
#services:       files
#protocols:      files
#rpc:            files
#ethers:         files
#netmasks:       files
#netgroup:       files
#bootparams:     files
#automount:      files
#aliases:        db files
#EOF
	cp /usr/sbin/jur-pack $JUR/usr/sbin/
#	cp /lib/ld-linux.so.2 /lib/lib{nss_files.so.2,c.so.6} $JUR/lib/
}

rpm_init()
{
	mkdir -p $RROOT/var/{tmp,lib/rpm} $RROOT/etc
	rpm --initdb --root $RROOT
	passwd > $RROOT/etc/passwd
	group > $RROOT/etc/group
}

read_name()
{
	NAME=$(rpm -q --queryformat %{NAME} -p $RRPM)
}

read_header()
{
	for i in `rpm --querytags`; do
		eval "$i=\$(rpm -q --queryformat \"[%{$i}\n]\" -p $RRPM); test \"\$$i\" = \"(none)\" && $i="
	done
}

warning()
{
	echo "Warning in package $RRPM."
	echo $1
}

check()
{
	RPMB=`basename $RRPM`
	if test "X${NAME}-${VERSION}-${RELEASE}.i386.rpm" != "X$RPMB" \
		-a "X${NAME}-${VERSION}-${RELEASE}.noarch.rpm" != "X$RPMB" ; then
		warning "Wrong NAME/VERSION/RELEASE."
	fi
	if test -n "$PREINPROG" -a "$PREINPROG" != /bin/sh ; then
		warning "Wrong PREINPROG=$PREINPROG"
	fi
	if test "$POSTINPROG" = /sbin/ldconfig -a "$POSTIN" = "" ; then
		POSTINPROG=
	fi
	if test -n "$POSTINPROG" -a "$POSTINPROG" != /bin/sh ; then
		warning "Wrong POSTINPROG=$POSTINPROG"
	fi
	if test -n "$PREUNPROG" -a "$PREUNPROG" != /bin/sh ; then
		warning "Wrong PREUNPROG=$PREUNPROG"
	fi
	if test "$POSTUNPROG" = /sbin/ldconfig -a -n "$POSTUN" ; then
		echo "-------------------------------------------------"
		echo "Warning: wrong POSTUN in $RPMB:"
		echo "$POSTUN"
		echo "-------------------------------------------------"
		echo "$POSTUN" | sed 's/^#.*$//' > /tmp/hugo1.$$
		POSTUN=$(cat /tmp/hugo1.$$)
		rm -f /tmp/hugo1.$$
	fi
	if test "$POSTUNPROG" = /sbin/ldconfig -a "$POSTUN" = "" ; then
		POSTUNPROG=
	fi
	if test -n "$POSTUNPROG" -a "$POSTUNPROG" != /bin/sh ; then
		warning "Wrong POSTUNPROG=$POSTUNPROG"
	fi
	if test -n "$TRIGGERSCRIPTPROG" -a "$TRIGGERSCRIPTPROG" != /bin/sh ; then
		warning "Wrong TRIGGERSCRIPTPROG=$TRIGGERSCRIPTPROG"
	fi
	if test -n "$VERIFYSCRIPTPROG" -a "$VERIFYSCRIPTPROG" != /bin/sh ; then
		warning "Wrong VERIFYSCRIPTPROG=$VERIFYSCRIPTPROG"
	fi
}

chroot_init

for RRPM in $RPMSOURCE/*.rpm ; do
	read_name
	test -f $DEST/$NAME.jur -a $DEST/$NAME.jur -nt $RRPM && continue

	#echo "converting $RRPM"	# XXX
	read_header
	check

	rpm_init
	rpm --nodeps --noscripts --notriggers --root $RROOT -i $RRPM
	#rpm2cpio $RRPM | (cd $RROOT && cpio --extract --make-directories \
	# --quiet --no-absolute-filenames --preserve-modification-time)

	(cd $RROOT;
	if test -n "$PREIN" ; then
		mkdir -p       $S/prein
		( echo "#!/bin/sh"; echo "$PREIN"; ) > $S/prein/$NAME
	fi
	if test -n "$POSTIN" ; then
		mkdir -p       $S/postin
		( echo "#!/bin/sh"; echo "$POSTIN"; ) > $S/postin/$NAME
	fi
	if test -n "$PREUN" ; then
		mkdir -p       $S/preun
		( echo "#!/bin/sh"; echo "$PREUN"; ) > $S/preun/$NAME
	fi
	if test -n "$POSTUN" ; then
		mkdir -p       $S/postun
		( echo "#!/bin/sh"; echo "$POSTUN"; ) > $S/postun/$NAME
	fi
	if test -n "$TRIGGERSCRIPTS" ; then
		mkdir -p       $S/trigger
		( echo "#!/bin/sh"; echo "$TRIGGERSCRIPTS"; ) > $S/trigger/$NAME
	fi
	if test -n "$VERIFYSCRIPT" ; then
		mkdir -p       $S/verify
		( echo "#!/bin/sh"; echo "$VERIFYSCRIPT"; ) > $S/verify/$NAME
	fi
	chmod 755 $S/*/* 2>/dev/null
	rm -f etc/{passwd,group}
	test -f etc/passwd.rpmnew && mv etc/passwd.rpmnew etc/passwd
	test -f etc/group.rpmnew  && mv etc/group.rpmnew etc/group
	rm -f var/lib/rpm/*
	test $NAME != filesystem && {
		rmdir var/lib/rpm var/tmp 2>/dev/null
	}
	rmdir etc var/lib var 2>/dev/null
	test -d etc/news && {
		chown news:news etc/news
		chmod 775 etc/news
	}
	test -d var/lock && {
		chown root:uucp var/lock
		chmod 775 var/lock
	}
	test -d usr/lib/python1.5 && chmod 755 usr/lib/python1.5
	test -d root && chmod 700 root
	chmod 755 .
	# XXX "/root" should not be hardcoded
	chroot $JUR /usr/sbin/jur-pack -d /root /$NPACK/$NAME.jur )
	rm -fr $RROOT
done

# generate the index file
(cd $DEST
 find . -name "*.jur" \! -name index.jur | jur-index) || echo "Nix index"

# make a default config file with all packages
(echo "start_directory $DEST/"
 echo "nocheckugids"
 cd $DEST && for i in *.jur ; do
	test $i = index.jur && continue
	echo "package $i"
done) > $JUR/var/jurix/config.new

exit 0
