#!/bin/bash
#
# $Id: gmtlogo 13715 2014-11-18 19:06:50Z pwessel $
#
# usage: gmtlogo dx dy scale [-Gbackground] [-Wboxpen] >> t.ps
#
# Logo is 2" wide and 1" high and origin is lower left
# It has no header nor trailer

if [ $# -eq 0 ]; then
	echo "gmtlogo - Adding a GMT graphics logo overlay to an illustration" >&2
	echo " " >&2
	echo "The logo is 2\" wide and 1\" high with lower left corner at present origin" >&2
	echo "but may be translated and scaled using the settings below." >&2
	echo " " >&2
	echo "usage: gmtlogo dx dy scale [-Gfill] [-W[pen]] >> plot.ps" >&2
	echo "		dx, dy shifts logo relative to plot origin" >&2
	echo "		scale sets the logo height in inches" >&2
	echo "		Optional -G, -W draws underlying [filled] [outlined] box" >&2
	exit -1
fi

export GMT_TMPDIR=`mktemp -d ${TMPDIR:-/tmp}/gmt.XXXXXX`

# Store outline of GMT letters
cat > $GMT_TMPDIR/gmt5logo.txt << EOF
> G
-59.5 -41.8
-59.5 2.2
-105 2.2
-105 -7.7
-72.8 -7.7
-72.94 -11
-73.24 -13.75
-73.65 -16.5
-74.50 -19.5
-75.88 -22
-77.65 -24.5
-80.7 -27.5
-84 -29.7
-87.1 -31.28
-91 -32.78
-98 -34.1
-105 -34.87
-112 -34.3
-119 -33
-122.5 -31.8
-126 -30.25
-129.5 -27.5
-133 -24.64
-135.8 -22
-137.55 -19.4
-138.46 -16.5
-139.5 -11
-140 -5.5
-140.14 0
-139.86 5.5
-138.88 11
-136.36 16.5
-133 20.9
-126 27.28
-119 30.36
-112 32.12
-105 32.78
-100 32.45
-94.3 31.15
-91 30.14
-87.55 28.89
-84.45 27.39
-82.12 25.9
-80.12 24.1
-78.22 21.9
-77.1 19.8
-76.55 17.52
-76.3 15.18
-61.6 15.18
-62 18.75
-63.1 21.89
-64.75 24.83
-66.8 27.5
-70 30.58
-73.0 33
-77 35.65
-84 38.72
-91 40.7
-98 41.8
-105 42.13
-112 41.8
-119 41.14
-126 39.6
-133 35.97
-137.2 33
-143.78 27.5
-147 24.2
-151.2 16.5
-153.02 11
-154.28 5.5
-154.80 0
-154.63 -5.5
-153.85 -11
-152.32 -16.5
-150.10 -22
-147 -26.95
-141.4 -33
-133 -38.5
-126 -41.58
-119 -43.01
-112 -44
-105 -44.33
-98 -43.78
-91 -42.35
-84.5 -40.37
-80 -38.2
-76.5 -36
-74.65 -34.3
-73.5 -33
-72.7 -32
-71.96 -30.8
-68.04 -41.8
> M
-37.8 -41.8
-23.8 -41.8
-23.8 27.39
5.04 -41.8
19.46 -41.8
49 27.39
49 -41.8
62.58 -41.8
62.58 40.48
42 40.48
12.46 -29.7
-17.22 40.48
-37.8 40.48
> T
124.46 -41.8
124.46 30.47
160.02 30.47
160.02 40.48
74.34 40.48
74.34 30.47
110.32 30.47
110.32 -41.8
EOF

# colors
c_font=51/51/51
c_grid=150/160/163
c_land=125/168/125
c_water=189/224/223
c_gmt_fill=238/86/52
c_gmt_outline=112/112/112
c_gmt_shadow=92/102/132

gmt gmtset MAP_FRAME_PEN 0.5p,gray20 \
MAP_GRID_PEN_PRIMARY thinnest,${c_grid} \
MAP_POLAR_CAP none

echo "% Start of GMT Graphics logo"
echo "% Generated by $*"
echo "gsave"

scl1=$3
scl2=`gmt gmtmath -Q $3 1.55 MUL =`
scl3=`gmt gmtmath -Q $3 2.47 MUL =`

# Do optional outline
if [ $# -gt 3 ]; then
  sclf=`gmt gmtmath -Q $3 1.03 MUL =` # extra space between frame and logo
  gmt psxy -R0/2/0/1 -Jx${sclf}i -L -O -K -X$1 -Y$2 $4 $5 << EOF
  0 0
  2 0
  2 1
  0 1
EOF
  dx=`gmt gmtmath -Q $3 0.06 MUL =` # extra space between frame and logo
  gmt psxy -R0/2/0/1 -Jx${scl1}i -X$dx -O -K -T # shift half width of extra space
else
  gmt psxy -R0/2/0/1 -Jx${scl1}i -O -K -X$1 -Y$2 -T
fi

# This scaling is to stretch the font a bit
echo "gsave 1 1.5 scale"
f=`gmt gmtmath -Q $3 9.5 MUL =`
#echo "1 0.027 Generic Mapping Tools Graphics" | gmt pstext -F+f$f,Bookman-Demi+jBC -R -J -O -K -N
echo "1 0.027 @#THE@# G@#ENERIC@# M@#APPING@# T@#OOLS@#" | gmt pstext -F+f$f,AvantGarde-Demi,${c_font}+jBC -R -J -O -K -N
echo "grestore"

# Undone font scaling
dx=`gmt gmtmath -Q $3 0.225 MUL =`
dy=`gmt gmtmath -Q $3 0.220 MUL =`

# Draw globle
frame_pen=`gmt gmtmath -Q $3 0.3 MUL =`
gmt gmtset MAP_GRID_PEN faint,${c_grid} MAP_FRAME_PEN ${frame_pen}p,${c_grid}
gmt psclip -T -R-180/180/-90/90 -JI0/${scl2}i -N -O -K -X${dx}i -Y${dy}i
gmt pscoast -R -J -S${c_water} -G${c_land} -A35000+l -Dc -O -K
gmt psclip -R -J -C -O -K -Bxg45 -Byg30

# Print GMT letter
dx=`gmt gmtmath -Q $3 0.483 MUL =`
dy=`gmt gmtmath -Q $3 0.230 MUL =`
gmt psxy -R167/527/-90/90 -JI-13/${scl2}i -O -K -L -G${c_gmt_shadow}@40 $GMT_TMPDIR/gmt5logo.txt # shadow
gmt psxy -R -JI-13/${scl3}i -O -K -L -G${c_gmt_fill} -W${frame_pen}p,${c_gmt_outline} -X-${dx}i -Y-${dy}i $GMT_TMPDIR/gmt5logo.txt
echo "grestore"
echo "% End of GMT Graphics logo"

rm -rf $GMT_TMPDIR
unset GMT_TMPDIR
exit 0
