#!/bin/sh -e

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

# Url to the above:
url=http://www.real.com/products/player/50player/downloadrealplayer.html

# A string that denotes the current version of the player:
version=5.0-rpm

# A file that may not appear in the package, if it does, the version is too
# old (Real has a nasty habit of doing slipstream releases where they move
# files around.)
badfile=README.FIRST

# A file that _must_ be in the package or it's bad.
needfile=usr/bin/rvplayer

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

TMPDIR=$HOME

rvtmpdir="$TMPDIR/rvplayer-unpacked.$$"

# Check if the external files are currently installed.
if [ -e "/usr/X11R6/lib/X11/rvplayer/rvplayer" ]; then
	# Yes, they are. Are they from an old version?
	if [ -e "/usr/doc/rvplayer/$badfile" -o \
		! -e "/usr/X11R6/lib/X11/rvplayer/version" -o \
		"`cat /usr/X11R6/lib/X11/rvplayer/version 2>/dev/null`" != "$version" ]; then

		echo "A new upstream version of rvplayer is available."
		echo ""

		# Upgrade/downgrade - need to reinstall. 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.
		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
	fi
fi

# Check if external files are already installed.
if [ "$1" != "abort-upgrade" -a "$1" != "abort-remove" -a "$1" \
	!= "abort-deconfigure" -a \
	! -e "/usr/X11R6/lib/X11/rvplayer/rvplayer" ]; then

	while [ -z "$ok" ]; do
		ok=1
		if [ ! -e "$TMPDIR/$archive" ]; then
			echo "Error: cannot find rvplayer archive in $TMPDIR/$archive"
			echo "Visit $url"
			echo "and download the Redhat Linux 5.x version of Real Video Player."
			echo "(Yes, that sounds counterintuitive, but that version is the one"
			echo "this installer deals with.)"
			ok=''
		else
			if [ ! -r "$TMPDIR/$archive" ]; then
				echo "Error: $TMPDIR/$archive is not readable."
				ok=''
			fi
			if [ ! -O "$TMPDIR/$archive" ]; then
				echo "Error: $TMPDIR/$archive must be owned by user root."
				ok=''
			fi
			if [ ! -G "$TMPDIR/$archive" ]; then
				echo "Error: $TMPDIR/$archive must be owned by group root."
				ok=''
			fi
			if file $TMPDIR/$archive 2>/dev/null | grep -vq 'RPM'; then
				echo "Error: $TMPDIR/$archive is not a rpm file."
				echo "Maybe you need to re-download it from"
				echo "$url"
				ok=''
			fi
		fi
		if [ "$ok" ]; then
			# Ok, we have an archive now. Let's try unpacking it.
			mkdir $rvtmpdir;
			cd $rvtmpdir
			rpm2cpio $TMPDIR/$archive | cpio --extract --make-directories
			if [ ! -e "$needfile" ]; then
				echo "Error: $TMPDIR/$archive does not contain a file"
				echo "named $needfile ."
				echo "The file may be corrupt. Please try again to download it from"
				echo "$url"
				echo "If it still doesn't work, file a bug report on this package."
				ok=""
				cd ..
				rm -r $rvtmpdir
			fi
		fi
		if [ -z "$ok" ]; then
			echo ""
			echo "Correct the above problems, and when you are done, enter 'Y'"
			echo "below. Or, enter 'N' to abort the installation."
			echo ""
			while [ ! "$answer" ]; do
				echo -n "Continue with install? [Y/n] "
				read answer
				if [ -z "$answer" ] ; then
					answer=y
				fi	
				case "$answer" in
					[Yy]*)
					;;			
					[Nn]*)
						echo "Aborting."
						exit 1
					;;
					*)
						answer=""
						echo "Answer Y or N, please."
					;;
				esac
			done
			answer=""
			echo ""
		fi
	done

	# Actually install the files, but first, fix permissions.
	chown -R root.root *
	chmod -R go=rX *
	chmod -R u+rw *

	install -s usr/bin/rvplayer /usr/X11R6/lib/X11/rvplayer/rvplayer
	cp usr/lib/Real/libdecdnet.so /usr/lib/
	cp usr/lib/Real/libra14_4.so /usr/lib/
	cp usr/lib/Real/libra28_8.so /usr/lib/
	cp usr/lib/Real/libradnet.so /usr/lib/
	cp usr/lib/Real/librarv10.so /usr/lib/
	cp usr/lib/Real/librasipr.so /usr/lib/
	cp usr/lib/Real/librv10dec.so /usr/lib/
	cp usr/lib/Real/librvcore.so /usr/lib/
	cp usr/lib/Real/librvplayer.so /usr/lib
	cp usr/doc/rvplayer-5.0/index.htm usr/doc/rvplayer-5.0/License.txt \
		/usr/doc/rvplayer/
	cp -a usr/doc/rvplayer-5.0/doc /usr/doc/rvplayer/
	cp usr/lib/rvplayer/welcome.rm usr/lib/rvplayer/rvplayer.ad \
		/usr/doc/rvplayer/examples/

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

	cd /
	rm -r $rvtmpdir

	if [ "$1" = "configure" ]; then
		ldconfig
	fi

	echo "$TMPDIR/$archive has been unpacked and installed."
	echo "You can now delete it, if you wish."
fi

if [ -x /usr/sbin/update-mime ]; then
	update-mime
fi

#DEBHELPER#
