#!/bin/sh
#
# NB: assumes /bin/sh is really bash
#
# bigv3-make - make slideshow-like .sna from 7 pictures.


# usage: convert_file infile outfile_small outfile_big
#
convert_file() {
echo -n "$1 is" 1>&2
case $1 in
  *jpg )
    echo " a JPEG, using djpeg." 1>&2
    command="djpeg -grey"
    ;;
  *gif )
    echo " a GIF, using giftopnm." 1>&2
    command=giftopnm
    ;;
  *png )
    echo " a PNG, using pngtopnm." 1>&2
    command=pngtopnm
    ;;
  *mrf )
    echo " an mrf, using mrftopbm." 1>&2
    command=mrftopbm
    ;;
  *p?m )
    echo " a PNM, using cat." 1>&2
    command=cat
    ;;
  * )
    echo ', um, I don'\''t know what it is. Give it a filetype!' 1>&2
    echo '...quitting...' 1>&2
    exit 1
    ;; 
esac

# first the small (256x192) bitmap

$command $1 |ppmtopgm|pgmnorm -bp 15 -wp 15|pnmscale -xysize 256 192 | \
 pgmtopbm >$tempfile.cnv1

WIDTH=`pnmfile $tempfile.cnv1 |cut -d " " -f 3`
HEIGHT=`pnmfile $tempfile.cnv1 |cut -d " " -f 5`
pnmpad -white -r`expr 256 - $WIDTH` $tempfile.cnv1 |\
pnmpad -white -b`expr 192 - $HEIGHT` >$tempfile.cnv2

# it's now 256x192, so header is "P4\n256 192\n" - cut that off.
dd if=$tempfile.cnv2 of=$2 bs=11 skip=1 2>/dev/null
rm -f $tempfile.cnv[12]
        
# now the big (592x440) one.

$command $1 |ppmtopgm|pgmnorm -bp 15 -wp 15|pnmscale -xysize 592 440 | \
 pgmtopbm >$tempfile.cnv1

WIDTH=`pnmfile $tempfile.cnv1 |cut -d " " -f 3`
HEIGHT=`pnmfile $tempfile.cnv1 |cut -d " " -f 5`
pnmpad -white -r`expr 592 - $WIDTH` $tempfile.cnv1 |\
pnmpad -white -b`expr 440 - $HEIGHT` >$tempfile.cnv2

# it's now 592x440, so header is "P4\n592 440\n" - cut that off.
dd if=$tempfile.cnv2 of=$3 bs=11 skip=1 2>/dev/null
rm -f $tempfile.cnv[12]
        
return 0
}


# main program

if [ "$1" = "" -o "$3" = "" -o "$4" != "" ]; then
  echo "usage: $0 picture1 picture3 picture3 >out.z80" 1>&2
  echo '' 1>&2
  echo "The pictures may be in jpeg/gif/png/mrf/pnm format and of any" 1>&2
  echo "size and shape - they are automatically converted." 1>&2
  exit 1
fi

trap 'rm -f $tempfile.*' 0 1 3 15

tempfile=/tmp/tmp_bigv3_$$

convert_file "$1" $tempfile.s1 $tempfile.b1
convert_file "$2" $tempfile.s2 $tempfile.b2
convert_file "$3" $tempfile.s3 $tempfile.b3

# now we have the files in speccy format we can construct the snap.
# the order goes like this:
#	header*
#	screen and attrs (blank)
#	boss screen data*
#	code*
#	zero bytes, 6400-code_len
#	small bitmaps 1/2/3
#	big bitmaps with 104 zero bytes either side of each
# the asterisked ones above are embedded below as uuencoded data.
# the remainder are catted or dd'd.

# to avoid problems with not wanting shell expansion of the data
# but wanting expansion of the "begin" line (so we can specify a
# (hopefully) unique temp. name) we do the uudecoding in a slightly
# indirect manner.

# the header
echo "begin 600 $tempfile.hdr" >$tempfile.0
cat <<'EOF' >>$tempfile.0
M'5P``#M<``#[6P!O#A;L`01H`#@``$0Z7&S]```!%P``7P,'```.````````
*`/\```````#_````
`
end
EOF
uudecode $tempfile.0

# screen and attrs
dd if=/dev/zero bs=6912 count=1 of=$tempfile.scrn

# boss screen data
echo "begin 600 $tempfile.boss" >$tempfile.0
cat <<'EOF' >>$tempfile.0
M```````````````````````````````````!_@'^`0`8/#P`?#P\/CP`````
M``````````````````/\`_P#`"A"0@!"0D`(0@``````````````````````
M!_@'^`<`"`(\`'Q"/`A````````````````````````/\`_P#P`(/$(`0GX"
M"$```````````````````````!_@'^`?``A`0@!"0D((0@``````````````
M````````/\`_P#\`/GX\`'Q"/#X\``````````````````````!_@'^`?P``
M`````````````````````````````````/\`_P#_`,\X.#@X.#@X.#@X.#@X
M.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X
M.#@X.#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T!,0$U`3D!/
M8$A@26!*8$M@3&!-8$Y@3X!(@$F`2H!+@$R`38!.@$^@2*!)H$J@2Z!,H$V@
M3J!/P$C`2<!*P$O`3,!-P$[`3^!(X$G@2N!+X$S@3>!.X$\`4`!1`%(`4P!4
M`%4`5@!7(%`@42!2(%,@5"!5(%8@5T!00%%`4D!30%1`54!60%=@4&!18%)@
M4V!48%5@5F!7@%"`48!2@%.`5(!5@%:`5Z!0H%&@4J!3H%2@5:!6H%?`4,!1
<P%+`4\!4P%7`5L!7X%#@4>!2X%/@5.!5X%;@5P``
`
end
EOF
uudecode $tempfile.0

# filler zero block
dd if=/dev/zero bs=104 count=1 of=$tempfile.zero1

# second zero block
# first find out how big it needs to be (i.e. work out 1022-code_len)
# I had some fun :-/ getting the next bit to work, so excuse the
# rather bizarre use of sed...
code_len=`wc -c $tempfile.code | cut -d/ -f 1 | sed 's/ *//'`
dd if=/dev/zero bs=$[6400-$code_len] count=1 of=$tempfile.zero2

rm -f $tempfile.0

# assemble them all with the help of rawtoz80.
cat $tempfile.scrn $tempfile.boss \
	$tempfile.code $tempfile.zero2 \
	$tempfile.s1 $tempfile.s2 $tempfile.s3 \
	$tempfile.zero1 $tempfile.b1 $tempfile.zero1 \
	$tempfile.zero1 $tempfile.b2 $tempfile.zero1 \
	$tempfile.zero1 $tempfile.b3 $tempfile.zero1 >$tempfile.mem

# this goes to stdout
rawtoz80 $tempfile.hdr $tempfile.mem

# also dels $tempfile.*
exit 0
