#!/bin/ksh
#
# ae-play - "play" anything
#
# Designed to be invoked from Andys Editor
# Uses external programs to display the contents of files textually.
# Or in some cases, fires up XWindows programs.
#

javas=
openssls=
case `uname -s` in
	SunOS)
		javas="/usr/j2se /usr/java /usr/java1.2 /usr/java1.1"
		openssls="/usr/local/ssl"
		;;
	AIX)
		javas="/usr/java6 /usr/java6_64 /usr/java5 /usr/java5_64 /usr/java14 /usr/java14_64 /usr/java131 /usr/java13_64"
		openssls="/opt/freeware"
		;;
esac

jar=jar
for i in $javas ; do
	if [[ -x $i/bin/jar ]] ; then
		jar=$i/bin/jar
		break
	fi
done

keytool=keytool
for i in $javas ; do
	if [[ -x $i/bin/keytool ]] ; then
		keytool=$i/bin/keytool
	fi
done

openssl=openssl
for i in $openssls ; do
	if [[ -x $i/bin/openssl ]] ; then
		openssl=$i/bin/openssl
		break
	fi
done

while [[ "$1" != "" ]] ; do
	print "=== $1 ==="
	case "`print "$1" | tr 'A-Z' 'a-z'`" in
		# Sound related
		*.wav)
			# playwave can play back at the wrong speed,
			# regardless of rate encoded within the file and/or
			# specified using the -r command line argument.
			# /usr/sbin/play seems to work better.
			# Fully qualify path, to avoid /usr/local/bin/play.
			/usr/bin/play "$1"
			;;
		*.mp3)
#			# Recently I've seen files that this can't handle
#			# And it depends on /dev/dsp (missing on Fedora 14)
#			mpg123 -q "$1"
#			# mpg321 is a drop-in replacement for mpg123
#			mpg321 -q "$1"
			# no mpg321 on Fedora 25, so use mplayer
			mplayer "$1"
			;;
		*.mid)
			# Don't use playmidi - it can't open a sound device.
			# TiMidity++ works by converting to .wav and streaming
			# to the output device.
			timidity "$1"
			;;
		*.m3u)
			# These are playlists, and typically refer to the
			# contents of a ripped CD.
			# Pathnames in .m3u file are typically relative,
			# hence the temporary change of current directory.
			# Can have comment lines too (ignored).
			while read m ; do
				if [[ m != \#* ]] ; then
					pwd=`pwd`
					cd `dirname "$1"`
					ae-play "$m"
					cd "$pwd"
				fi
			done < "$1"
			;;
		# Video related
#		*.mpg)
#			# plaympeg is better, but missing a shared library
#			mpeg_play -dither color "$1"
#			;;
		*.mpg|*.mpeg|*.mov|*.wmv|*.avi|*.mod)
			mplayer -really-quiet "$1"
			;;
		# Picture related
		*.bmp|*.gif|*.pcx|*.tif|*.tiff|*.tga|*.vst|*.afi|*.iff|*.lbm|*.vid|*.pgm|*.ppm|*.kps|*.iax|*.xbm|*.spr|*.sprite|*.pse|*.pseg*|*.img|*.cvp|*.jpg|*.jpeg|*.jpe)
			show "$1"
			;;
		# Office document related
		*.sxw|*.doc|*.ppt|*.xls|*.rtf)
			ooffice "$1"
			;;
		*.pdf)
#			acroread "$1"
#			xpdf "$1"
			evince "$1"
			;;
		# Archive related
		*.tar)
			tar -tvf "$1" | more
			;;
		*.tar.Z|*.taz)
			uncompress -c "$1" | tar -tvf - | more
			;;
		*.tar.gz|*.tgz)
			gunzip -c "$1" | tar -tvf - | more
			;;
		*.tar.bz|*.tar.bz2|*.tbz|*.tbz2)
			bunzip2 -c "$1" | tar -tvf - | more
			;;
		*.jar|*.ear|*.war|*.rar|*.par)
			$jar -tvf "$1" | more
			;;
		*.zip)
			unzip -v "$1" | more
			;;
		*.rpm)
			rpm -q --filesbypkg -p "$1" | more
			;;
		*.cab)
			cabextract -l "$1" | more
			;;
		# Crypto related
		*.rawkey.pem)
			# Assume unencrypted private key,
			# as created using openssl req
			$openssl pkcs8 -in "$1" -inform PEM -nocrypt | more
			;;
		*.key.pem)
			# Assume encrypted private key,
			# as created using openssl req
			# and encrypted using openssl pkcs8 -topk8
			$openssl pkcs8 -in "$1" -inform PEM | more
			;;
		*.csr.pem|*.csr)
			$openssl req -in "$1" -inform PEM -text | more
			;;
		*.cert.pem|*.cer|*.arm)
			$openssl x509 -in "$1" -inform PEM -text | more
#			$keytool -printcert -file "$1" | more
			;;
		*.p12)
			$openssl pkcs12 -in "$1" | more
			;;
		*.jks)
			$keytool -list -v -keystore "$1" | more
			;;
		*.jceks)
			$keytool -list -v -keystore "$1" -storetype JCEKS | more
			;;
		*.sth)
			be "$1"
			;;
		*.mtx|*.MTX)
			memu -vid-win-big -snd-portaudio "$1"
			;;
		*.run|*.RUN)
			memu -vid-win-big -snd-portaudio "$1"
			;;
		*.com|*.COM)
			memu -vid-win-big -snd-portaudio "$1"
			;;
		*.baf|*.BAF)
			memu -vid-win-big -snd-portaudio MTXL.COM "$1"
			;;
		*.mfloppy-03)
			cpmls -f memotech-type03 -l "$1"
			;;
		*.mfloppy-07)
			cpmls -f memotech-type07 -l "$1"
			;;
		# Give up
		*)
			print "$0: don't know how to \"play\" $1"
			;;
	esac
	shift
done

exit 0
