#!/bin/sh
#
############################################################################
#
# MODULE:	d.out.file for GRASS 6 (2005/9/3)
# AUTHOR(S):	Michael Barton 
#		 Routine to read current display geometry written by 
#		 Hamish Bowman & Glynn Clements
# PURPOSE:	To redraw current displayed maps to graphics file output
# COPYRIGHT:	(C) 2005 by the GRASS Development Team
#
#		This program is free software under the GNU General Public
#		License (>=v2). Read the file COPYING that comes with GRASS
#		for details.
#
#############################################################################


#%Module
#%  description: Saves active display monitor to graphics file in home directory
#%  keywords: display, export
#%End
#%option
#% key: output
#% type: string
#% description: Name of output file (do NOT add extension)
#% required : yes
#%end
#%option
#% key: res
#% type: integer
#% answer: 1
#% description: Resolution of output file (single=1, double=2, quad=4)
#% required : no
#%end
#%option
#% key: format
#% type: string
#% options: png,ppm,tif,jpg,bmp
#% answer: png
#% description: Graphics file format
#% required : yes
#%end
#%option
#% key: compression
#% type: string
#% options: 0,1,9
#% answer: 0
#% description: Compression for PNG files (0=none, 1=fastest, 9=most)
#% required : no
#%end
#%option
#% key: quality
#% type: string
#% answer: 75
#% description: Size/quality for JPEG files (10=smallest/worst, 100=largest/best)
#% required : no
#%end
#%flag 
#%key: t
#%description: Transparent background
#%end
#%flag 
#%key: b
#%description: Set background color to black (white default)
#%end


if  [ -z "$GISBASE" ] ; then
 echo "You must be in GRASS GIS to run this program."
 exit 1
fi   

if [ "$1" != "@ARGS_PARSED@" ] ; then
  exec g.parser "$0" "$@"
fi

# setting environment, so that awk works properly in all languages
unset LC_ALL
LC_NUMERIC=C
export LC_NUMERIC

# Check the current status of the monitor
monitorcheck=`d.mon -p|sed s/'No monitor currently selected for output'//`

if  [ "$monitorcheck" = "" ]
then
	echo ""
	echo "***You must select a display monitor***"
	echo ""
	exit 1
fi
     

# Save old settings
old_GRASSW=`eval g.gisenv get=GRASS_WIDTH`
old_GRASSH=`eval g.gisenv get=GRASS_HEIGHT`
old_TRUECOLOR=`eval g.gisenv get=GRASS_TRUECOLOR`
old_TRANSPARENT=`eval g.gisenv get=GRASS_BACKGROUNDCOLOR`
old_BACKGROUND=`eval g.gisenv get=GRASS_TRANSPARENT`
old_COMPRESSION=`eval g.gisenv get=GRASS_PNG_COMPRESSION`


curr_mon=`d.mon -p | awk '{printf "%s", $4}'`

#setting variables (get rid of extra png)
#output="`basename $GIS_OPT_OUTPUT .png`"
res="$GIS_OPT_RES"

#get GRASS version for display monitor name
GRASS_VER=`g.version | awk '{printf "%s", $1 " " $2}'`

#capture current display monitor and geometry
# identify current monitor
currmon=`eval d.mon -L | grep "(selected)" | awk '{print $1}'`
curr_width=`d.frame -D | sed -n -e '1,/^FRAME: full_screen/d' -e 's/d_win://p' | awk '{printf $4}'`
curr_height=`d.frame -D | sed -n -e '1,/^FRAME: full_screen/d' -e 's/d_win://p' | awk '{printf $2}'`

#set resolution for PNG output
out_width=`expr $res \* $curr_width`
out_height=`expr $res \* $curr_height`

#set window geometry for PNG output
unset GRASS_WIDTH
unset GRASS_HEIGHT
GRASS_WIDTH=$out_width
export GRASS_WIDTH
GRASS_HEIGHT=$out_height
export GRASS_HEIGHT

#make sure that output is 24 bit color
GRASS_TRUECOLOR=TRUE
export GRASS_TRUECOLOR

#set other output options

if [ "$GIS_FLAG_B" -eq 1 ] ; then
    GRASS_BACKGROUNDCOLOR=000000
    export GRASS_BACKGROUNDCOLOR
else
    GRASS_BACKGROUNDCOLOR=FFFFFF
    export GRASS_BACKGROUNDCOLOR
fi

if [ "$GIS_FLAG_T" -eq 1 ] ; then
    GRASS_TRANSPARENT=TRUE
    export GRASS_TRANSPARENT
else
    GRASS_TRANSPARENT=FALSE
    export GRASS_TRANSPARENT
fi

GRASS_PNG_COMPRESSION=$GIS_OPT_COMPRESSION
export GRASS_PNG_COMPRESSION

#set output file name and format
if [ "$GIS_OPT_FORMAT" = "png" ] ; then
    output=$GIS_OPT_OUTPUT".png"
fi

if [ "$GIS_OPT_FORMAT" != "png" ] ; then
    output=$GIS_OPT_OUTPUT".ppm"
fi

echo "******************"
echo "Saving display from [$GRASS_VER- Monitor: $curr_mon] to $output"
echo "in home directory at $out_width x $out_height resolution"
echo "******************"

GRASS_PNGFILE=$output
export GRASS_PNGFILE

#export display to PNG driver
dsave=`d.save -a`
d.mon start=PNG
eval "$dsave"
d.mon stop=PNG


#returning old settings
d.mon select=$curr_mon
GRASS_WIDTH=$old_GRASSW
export GRASS_WIDTH
GRASS_HEIGHT=$old_GRASSH
export GRASS_HEIGHT
GRASS_TRUECOLOR=$old_TRUECOLOR
export GRASS_TRUECOLOR
GRASS_PNGFILE=map.png
export GRASS_PNGFILE
GRASS_BACKGROUNDCOLOR=$old_TRANSPARENT
export GRASS_BACKGROUNDCOLOR
GRASS_TRANSPARENT=$old_BACKGROUND
export GRASS_TRANSPARENT
GRASS_PNG_COMPRESSION=$old_COMPRESSION
export GRASS_PNG_COMPRESSION


echo " "
echo "Screen export complete (writing the file may take a small amount of time)"


if [ "$GIS_OPT_FORMAT" != "png" -a  "$GIS_OPT_FORMAT" != "ppm" ] ; then
    output2=$GIS_OPT_OUTPUT"."$GIS_OPT_FORMAT
    sleep 5
else 
    exit 1
fi
#size=`ls -s $output | awk '{printf $1}'`

echo " "
echo "******************"
echo "Waiting for file to write"
echo " "

if [ "$GIS_OPT_FORMAT" = "tif" ] ; then
    echo "Translating to TIFF format"
    gdal_translate $output $output2 -of GTIFF
    rm $output
fi

if [ "$GIS_OPT_FORMAT" = "jpg" ] ; then
    echo "Translating to JPEG format"
    gdal_translate $output $output2 -of JPEG -co QUALITY=$GIS_OPT_QUALITY
    rm $output
fi

if [ "$GIS_OPT_FORMAT" = "bmp" ] ; then
    echo "Translating to BMP format"
    gdal_translate $output $output2 -of BMP
    rm $output
fi

    echo "Done"


