#!/bin/sh -e

# A string that denotes the current version of the player:
version=8.1

# The name of the file that comes from Real's web site:
archive=rp8_linux20_libc6_i386_cs2_rpm

############################################################################

# Use debconf.
. /usr/share/debconf/confmodule
db_version 2.0

# Minimalist rpm2cpio in perl.
# Code taken from http://www.eleves.ens.fr:8080/home/espel/rpm2cpio
rpm2cpio() {
	perl -e '
		undef $/;
		$|=1;
		$rpm = <>;

		($magic, $major, $minor, undef) = unpack("NCC C90", $rpm);
		exit "Not an RPM\n" if $magic != 0xedabeedb;
		exit "Not a version 3 RPM\n" if $major != 3;
		
		$rpm = substr($rpm, 96);
		
		while ($rpm ne "") {
			$rpm =~ s/^\c@*//s;
			($magic, undef, $sections, $bytes) = unpack("N4", $rpm);
			$smagic = unpack("n", $rpm);
			last if $smagic eq 0x1f8b;
			die "Error: header not recognized\n" if $magic != 0x8eade801;
			$rpm = substr($rpm, 16*(1+$sections) + $bytes);
		}
		
		exit "bogus RPM\n" if $rpm eq "";
		
		open(ZCAT, "|gzip -cd") || die "cannot pipe to gzip\n";
		print ZCAT $rpm;
		close ZCAT;
	' $1
}

abort() {
	# Give up. This should never happen (unless the disk fills up or
	# something).
	cd ..                                                                           
	rm -r ${rvtmpdir}                                                                 
	echo "Couldn't extract the rpm, aborting"
	echo "You can set \$TMPDIR if /var/tmp is not an option"
	exit 1  
}

rvtmpdir="${TMPDIR:-/var/tmp}/realplayer-unpacked.$$"

# Remove old files.
# (Note that this section is duplicated in the prerm.)
# Also note that I should never remove any of the files from
# this list; they were all present in some old version or
# other.
rm -rf	/usr/lib/libdecdnet.so /usr/lib/libra14_4.so \
	/usr/lib/libra28_8.so /usr/lib/libradnet.so \
	/usr/lib/libradnet.so /usr/lib/librarv10.so \
	/usr/lib/librasipr.so /usr/lib/librv10dec.so \
	/usr/lib/librvplayer.so \
	/usr/doc/rvplayer/README.FIRST \
	/usr/doc/rvplayer/License.txt \
	/usr/doc/rvplayer/examples/welcome.rm \
	/usr/doc/rvplayer/examples/Mailcap \
	/usr/doc/rvplayer/examples/rvplayer.ad \
	/usr/X11R6/lib/X11/rvplayer/rvplayer \
	/usr/X11R6/lib/X11/rvplayer/version \
	/usr/lib/librvcore.so \
	/usr/lib/netscape/plugins/librvplayer.so \
	/usr/lib/netscape/plugins/RAObserver.class \
	/usr/lib/netscape/plugins/RAPlayer.class \
	/usr/doc/rvplayer/README \
	/usr/doc/rvplayer/index.htm \
	/usr/doc/rvplayer/doc \
	/usr/doc/realplayer/LICENSE \
	/usr/doc/realplayer/README \
	/usr/doc/realplayer/RealHelp \
	/usr/doc/realplayer/RealHelp.htm \
	/usr/lib/RealPlayerG2 \
	/usr/lib/RealPlayer7 \
	/usr/share/doc/realplayer/Mailcap \
	/usr/share/doc/realplayer/Mime.types \
	/usr/share/doc/realplayer/README.RealHelp \
	/usr/share/doc/realplayer/RealHelp.html \
	/usr/share/doc/realplayer/README \
	/usr/share/doc/realplayer/RealHelp.htm \
	/usr/share/doc/realplayer/RealHelp

# Fix up from a broken version: if these files exist, they are in the wrong
# place, and must be moved.
if [ -e /usr/lib/netscape/plugins/rpnp.so ]; then
	mv -f /usr/lib/netscape/plugins/rpnp.so \
		/usr/lib/netscape/plugins-libc6/
fi
if [ -e /usr/lib/netscape/plugins/raclass.zip ]; then
	mv -f /usr/lib/netscape/plugins/raclass.zip \
		/usr/lib/netscape/plugins-libc6/
fi

	# Check if external files are already installed.
if [ "$1" = "configure" -a \( ! -e "/usr/X11R6/lib/X11/realplayer/version" -o \
     "`cat /usr/X11R6/lib/X11/realplayer/version 2>/dev/null`" != "$version" \) ]; then

	# Check and make sure that we're supposed to be doing anything
		db_get realplayer/intro
		if [ "$RET" != "true" ]
			then
			# We haven't been configured so exit
			exit 0
		fi

	# The config script should have handled prompting for a file
	# and ensuring it exists and can be extracted with cpio.
	# Check anyway, confusing errors may result otherwise.
	db_get realplayer/download_dir
	if [ ! -e "$RET/$archive" ]; then
		echo "Something is horribly wrong, debconf has stale/incorrect rpm info."
		echo "Please file a bug explaining how you received this error."
		echo "Purging and reinstalling realplayer may help."
		exit 1
	fi	

	DIR="$RET"
	
	mkdir $rvtmpdir
	cd $rvtmpdir
	rpm2cpio $DIR/$archive | cpio --quiet --extract --make-directories >/dev/null || abort

	# Actually install the files, but first, fix permissions.
	chown -R root.root *
	chmod -R go=rX *
	chmod -R u+rw *
	find usr/lib/RealPlayer8/Help -type f | xargs chmod a-x

	rm -rf /usr/lib/RealPlayer8/
	cp -a usr/lib/RealPlayer8/ /usr/lib/RealPlayer8/
	cp -a usr/lib/netscape/plugins/* /usr/lib/netscape/plugins-libc6/

	
	echo "$version" > /usr/X11R6/lib/X11/realplayer/version

	cd /
	rm -r $rvtmpdir
	
	db_get realplayer/deletefile
	if [ "$RET" = true ]; then
		rm -f $DIR/$archive
	fi
fi

# Make sure this link exists, it makes realplayer work with esd.
ln -sf ../libesd.so.0 /usr/lib/RealPlayer8/libesd.so

#DEBHELPER#



