#!/bin/sh
#
# NB: assumes /bin/sh is really bash
#
# bigv-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 -black -r`expr 256 - $WIDTH` $tempfile.cnv1 |\
pnmpad -black -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 (600x450) one.

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

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

# it's now 600x450, so header is "P4\n600 450\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" = "" ]; then
  echo "usage: $0 picture_file >out.sna" 1>&2
  echo '' 1>&2
  echo "The picture may be in jpeg/gif/png/mrf/pnm format and of any size" 1>&2
  echo "and shape - it is automatically converted." 1>&2
  exit 1
fi

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

tempfile=/tmp/tmp_bigv_$$

convert_file "$1" $tempfile.small $tempfile.big

# now we have the files in speccy format we can construct the snap.
# the order goes like this:
#	header*
#	screen and attrs (blank)
#	small bitmap
#	big bitmap
#	zero bytes, 298 of them
#	boss screen data*
#	code*
#	zero bytes, 1022-code_len
#	little-endian word for start address, i.e. FC00h*
# 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
;``````````````````````````````#^_P$'
`
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.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X
M.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X
M.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X
M.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X
M.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X
M.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X
M.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X
M.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X
M.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X
M.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X
M.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X
M.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X
M.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X
M.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.$='1T='1T='1T!`0$!`0$!`
M0$!`0$!`0$!`0G)T;&A`.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X
B.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.#@X.```
`
end
EOF
uudecode $tempfile.0

# code (source is bigv.z)
# this corresponds to this version of bigv.z:
# -rw-------   1 rus      users        6004 Sep 23 03:38 bigv.z
#
echo "begin 600 $tempfile.code" >$tempfile.0
cat <<'EOF' >>$tempfile.0
M\S'__Q@#````S5+].@;\IR@Z(0!;W2&T_3[`W5X`W2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`4\!4P%7`5L!7X%#@4>!2
*X%/@5.!5X%;@5P``
`
end
EOF
uudecode $tempfile.0

# first zero block
dd if=/dev/zero bs=298 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=$[1022-$code_len] count=1 of=$tempfile.zero2

# finally, the FC00h word.
echo "begin 600 $tempfile.wrd" >$tempfile.0
cat <<'EOF' >>$tempfile.0
"`/P`
`
end
EOF
uudecode $tempfile.0

rm -f $tempfile.0

# assemble them all to stdout.

cat $tempfile.hdr $tempfile.scrn \
	$tempfile.small $tempfile.big $tempfile.zero1 \
	$tempfile.boss $tempfile.code $tempfile.zero2 $tempfile.wrd

# also dels $tempfile.*
exit 0
