#!/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:
filename=rp8_linux20_libc6_i386_cs2_rpm

# Trap escape, bad things can happen if this script is 
# interrupted partway.

trap ":" INT QUIT TSTP

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

# This package doesn't pre-depend on cpio, so if it is being preconfigured
# and cpio is not present, exit. This script will be re-ran when the postinst
# runs.
if [ ! -x /bin/cpio ]; then
	exit 0
fi

# This script will check to see if a new file needs to be downloaded, and if
# so will prompt the user to do so.

# Use debconf.
. /usr/share/debconf/confmodule || exit

# Establish the preliminaries.
db_version 2.0
#db_capb 'backup'

# 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
}

# Called if we cannot communicate with the user.
abort() {
    # They can't see it..
    # Try to leave them a message anyway.
    db_fset realplayer/needfile seen false
    db_input critical realplayer/needfile || true
    db_go
    exit 1
}

# Go ahead and check if we're a new upstream or not
if [ ! -e "/usr/X11R6/lib/X11/realplayer/version" -o \
     "`cat /usr/X11R6/lib/X11/realplayer/version 2>/dev/null`" != "$version" ]
then

# Loopdee loop
RUN_LOOP=1
while [ "$RUN_LOOP" = "1" ]
do

    db_reset realplayer/intro
    db_input critical realplayer/intro || abort
    db_go

    # Get response (continue y/n)
    db_get realplayer/intro

    if [ "$RET" != "true" ]; then
	# If No, tell them how to do it later and quit.
	db_input medium realplayer/later || true
	db_go
	exit 0
    fi

    # Otherwise continue
    db_fset realplayer/download_dir seen false
    db_subst realplayer/download_dir filename "$filename"

    db_beginblock
    # ask them where to find the rpm, important
    db_input critical realplayer/download_dir || abort
    # ask them if we can delete the rpm, lower priority
    db_fset realplayer/deletefile seen false
    db_input medium realplayer/deletefile || true
    db_endblock

    db_go || exit 1

    # check if rpm is ok, and .. deal with it
    db_get realplayer/download_dir
    if [ -e "$RET/$filename" ] && (rpm2cpio $RET/$filename | cpio --quiet --list >/dev/null); then
	# happy days.
	RUN_LOOP=0
    else	
	# shit happens
	# display a message and then loop.
	db_subst realplayer/badfile filename "$RET/$filename"
	db_fset realplayer/badfile seen false
	db_input critical realplayer/badfile || exit 1
	RUN_LOOP=1
    fi

done
else
inst_ver=`cat /usr/X11R6/lib/X11/realplayer/version` 
	echo "Skipping RPM stage, version ${version} matches installed ${inst_ver}."
	exit 0
fi
