#!/bin/bash

# display a big emoji

# 0.1" character pitch horizontally
# 0.16" vertically
# default to small
width=32
height=20
if [[ "$1" == "-big" ]]; then
    # large
    width=72
    height=45
    shift
fi

tmps=$(mktemp -t XXXXXX.svg)
tmpj=$(mktemp -t XXXXXX.jpg)
function cleanup() {
    rm $tmps
    rm $tmpj
}
trap cleanup EXIT

# ASR33 typewheel characters approximately sorted by print density
chars=" .-,/)+>^_=?7I][*J5C#V96$P&%4OZSDAH@GX2U8EBQWKRM"

url=$(emojiurl "$1")
if [[ $? == 0 ]]; then
    # fetch the SVG and convert to a JPG
    wget -q -O "$tmps" "$url"
    convert -density 200 -background white "$tmps" -background none -colorspace Gray -gamma 2 -auto-level jpg:"$tmpj"

    # convert to ascii (and trim blanks from line endings, to save printing time)
    jp2a --invert --chars="$chars" --width=$width --height=$height "$tmpj" | sed 's/[[:blank:]]*$//'
fi
