#!/bin/sh
# Script to preview fonts

usage() {
	printf '%b\n' "${0##*/} [-F FILE | -S | -t STRING] [OPTIONS]"                         \
	              "actions:"                                                              \
	              "\t-F FILE   - read from file"                                          \
	              "\t-S        - read from stdin"                                         \
	              "\t-t STRING - use a string"                                            \
	              "\noptions:"                                                            \
	              "\t-b SIZE   - border size"                                             \
	              "\t-c COLORS - set the color of the text and background"                \
	              "\t-f FONT   - which font to preview"                                   \
	              "\t-h        - display this message"                                    \
	              "\t-i        - generate the inverse image (horizontal, vertical, none)" \
	              "\t-o FILE   - output file for the generated image"                     \
	              "\t-s SIZE   - size of the font"                                        \
	              "\ndefault values:"                                                     \
	              "These values can be set before execution to change default values"     \
	              "\tPREFON_ACTION=text (file, stdin, or text)"                           \
	              "\tPREFON_BORDER_SIZE=16"                                               \
	              "\tPREFON_COLORS=\"000000,ffffff\""                                     \
	              "\tPREFON_FONT=\"monospace\""                                           \
	              "\tPREFON_OUTPUT=\"/tmp/prefon.png\""                                   \
	              "\tPREFON_SIZE=16"                                                      \
	              "\tPREFON_TEXT=\"The quick brown fox jumps\\\n    over the lazy dog\""  \
	              "\tPREFON_INVERSE=vertical (horizontal, vertical, none)"
}

info() {
	case $1 in
		i ) prefix=INFO ;;
		w ) prefix=WARNING ;;
		e ) prefix=ERROR ;;
		* ) printf '%s\n' "sus!" 1>&2; exit 1 ;;
	esac; shift

	printf "${0##*/}: $prefix: %b\n" "$*" 1>&2
}

# Default values
PREFON_ACTION=${PREFON_ACTION:-text}
PREFON_BORDER_SIZE=${PREFON_BORDER_SIZE:-16}
PREFON_COLORS=${PREFON_COLORS:-000000,ffffff}
PREFON_FONT="${PREFON_FONT:-monospace}"
PREFON_OUTPUT="${PREFON_OUTPUT:-/tmp/prefon.png}"
PREFON_SIZE=${PREFON_SIZE:-16}
PREFON_TEXT="${PREFON_TEXT:-The quick brown fox jumps\n    over the lazy dog}"
PREFON_INVERSE=${PREFON_WITHOUT_INVERSE:-vertical}

# Handle arguments
while [ "$*" ]
do
	case $1 in
		-  ) shift; continue ;;
		-- ) shift; break ;;
		-* ) flag=${1#-}; shift ;;
		*  ) shift; continue ;;
	esac

	while [ "$flag" ]
	do
		arg=${flag%${flag#?}}

		case $arg in
			F ) PREFON_ACTION="file"; PREFON_FILE=$1; shift ;;
			S ) PREFON_ACTION=stdin; PREFON_FILE=/dev/stdin ;;
			b ) PREFON_BORDER_SIZE=$1; shift ;;
			c ) PREFON_COLORS=$1; shift ;;
			f ) PREFON_FONT=$1; shift ;;
			h ) usage; exit 0 ;;
			i ) PREFON_INVERSE=$1; shift ;;
			o ) PREFON_OUTPUT=$1; shift ;;
			s ) PREFON_SIZE=$1; shift ;;
			t ) PREFON_ACTION=text; PREFON_TEXT=$1; shift ;;
			* ) printf '%s\n' "${0##*/}: -$arg: invalid argument" 1>&2
			    usage 1>&2; exit 1 ;;
		esac

		flag=${flag#?}
	done
done

_gen_img() {
	convert -background "#$2" -bordercolor "#$2" -border "$PREFON_BORDER_SIZE" \
		pango:"<span foreground=\"#$1\" font_desc=\"$PREFON_FONT $PREFON_SIZE\">$4</span>" "$3"
}

_output_file() {
	while IFS= read -r line
	do printf '%s\n' "$line"
	done < "$PREFON_FILE"
}

generate_image() {
	fgcolor=${PREFON_COLORS%,*}
	bgcolor=${PREFON_COLORS#*,}

	_gen_img "$fgcolor" "$bgcolor" "$PREFON_OUTPUT" "$1" || {
		info e failed to generate image
		exit 1
	}

	[ "$PREFON_INVERSE" != "none" ] && {
		inverse="${PREFON_OUTPUT%.*}"
		inverse="${inverse}-inverse.${PREFON_OUTPUT##*.}"

		_gen_img "$bgcolor" "$fgcolor" "$inverse" "$1" || {
			info e failed to generate image
			exit 1
		}

		case $PREFON_INVERSE in
			horizontal ) convert "$PREFON_OUTPUT" "$inverse" +append "$PREFON_OUTPUT" \
					|| { info e failed to generate image; exit 1; } ;;
			vertical   ) convert "$PREFON_OUTPUT" "$inverse" -append "$PREFON_OUTPUT" \
					|| { info e failed to generate image; exit 1; } ;;
			* ) info e "${PREFON_INVERSE}: is not a valid value for PREFON_INVERSE"
			    exit 1 ;;
		esac

		rm "$inverse" || info w failed to remove temporary file: "${inverse}"
	}

	printf '%s\n' "$PREFON_OUTPUT"
}

read_input() {
	input="$(_output_file)"
	generate_image "$input"
}

case $PREFON_ACTION in
	file )
		[ -e "$PREFON_FILE" ] || { info e "'$PREFON_FILE' does not exist"; exit 1; }
		read_input
	;;
	stdin )
		[ -e /dev/stdin ] || { info e "/dev/stdin does not exist"; exit 1; }
		read_input
	;;
	text )
		[ "$PREFON_TEXT" ] || { info e "string not provided"; exit 1; }
		generate_image "$PREFON_TEXT"
	;;
	* ) info e invalid action: $PREFON_ACTION; exit 1 ;;
esac
