#!/usr/bin/env bash
# LEGAL: COPYRIGHT (C) 2010 JIM E. BROOKS WWW.PALOMINO3D.ORG
#
# Produce an OSG scene graph from DEM and GeoTIF files
# downloaded from USGS Seamless Data Distribution System (SDDS).
#
# Usage:
# tile-sdds.sh *.tif terrain.osga
# ------------------------------------------------------------------------------

LEVELS=5
VERT_SCALE="0.075"
SCALE="5000.0,5000.0,5000.0"
POSITION="0,0,0"
TIF_SIZE="20% 20%"
OSGDEM_ARGS="--PagedLOD --RGB-16 --default-color 0,0,0,0 --compressed"
OSGDEM_ARGS="$OSGDEM_ARGS --no-terrain-simplification"
OSGDEM_ARGS="$OSGDEM_ARGS --mip-mapping-hardware --mip-mapping-imagery"
OSGDEM_ARGS="$OSGDEM_ARGS -l $LEVELS -v $VERT_SCALE"
OSGA=""
NAME=""  # implied from name.osga arg

# Prepare
rm -f temp*

if [ "$1" = "" ] || [ "$1" = "-h" ]; then
    echo "Usage: [-l levels] [-s percent] output.osga file.dem file.tif [file2.tif ...]"
    echo "Args can be in any order as their purpose is implied by their suffix."
    echo "DEM has the height data.  TIF has data for color texture."
    exit 1
fi

# Return in EXT var a file's extension or empty string.
FileExt()
{
    FILE=$1

    # If file doesn't have an extension,
    # sed will return the whole filename.
    EXT=`echo $FILE | sed -e 's/.*\.//'`
    if [ "$EXT" = "$FILE" ]; then
        EXT=""
    fi
}

# Build args for osgdem
while [ $# -gt 0 ]
do
    # USGS provides DEM files as GeoTIF files name NED_*.tif
    if [ "$1" = "-l" ]; then
        shift
        LEVELS="$1"
        echo "Levels = $LEVELS"
    elif [ "$1" = "-s" ]; then
        shift
        TIF_SIZE="${1}% ${1}%"
        echo "TIF reduction size = $TIF_SIZE"
    else
        FILE=$1
        FileExt $1  # result in EXT
        BASENAME=`basename $FILE .${EXT}`

		if ( echo $FILE | grep -q temp ); then
	        # In case *.tif picked a temp file.
	        :
	    elif ( echo $FILE | grep -q NED_ ); then
	        [ -f $FILE ] || exit 1
	        #OSGDEM_ARGS="$OSGDEM_ARGS -d $FILE"
	        # -d arg must precede -t args
	        OSGDEM_DEM_ARG="$OSGDEM_DEM_ARG -d $FILE"
	    elif [ "$EXT" = "tif" ] || [ "$EXT" = "TIF" ]; then
	        [ -f $FILE ] || exit 1
	        # Reduce TIF.
	        echo "Reducing the size of ${FILE}..."
	        rm -f temp.tif
	        SMALL_TIF="temp_${FILE}"
	        gdal_translate -outsize $TIF_SIZE $FILE $SMALL_TIF || exit 1
	        # Add reduced TIF as arg.
	        OSGDEM_ARGS="$OSGDEM_ARGS -t $SMALL_TIF"
	    elif [ "$EXT" = "osga" ]; then
	        NAME="$BASENAME"
	        OSGA="$FILE"
	        OSGDEM_ARGS="$OSGDEM_ARGS -o ${BASENAME}.ive -a $FILE"
	    else
	        echo "ERROR: $1 arg unrecognized."
	        exit 1
	    fi
    fi

    shift
done

# -d arg must precede -t args
OSGDEM_ARGS="$OSGDEM_DEM_ARG $OSGDEM_ARGS"

# Checks.
if [ "$OSGA" = "" ]; then
    echo "ERROR: .osga arg required."
    exit 1
fi

if [ "$NAME" = "" ] || [ "$OSGDEM_DEM_ARG" = "" ]; then
    echo "ERROR: oops."
    exit 1
fi

# Confirm.
echo "---------------------"
echo "osgdem args shall be:"
echo "$OSGDEM_ARGS"
echo "Other args shall be:"
echo "$VERT_SCALE $SCALE $POSITION $TIF_SIZE"
#echo "Press Enter to proceed..."
#read INPUT

# Prepare.
rm -f $OSGA

# Produce .osga file from GIS data.
osgdem $OSGDEM_ARGS || exit 1

# Reduce size of OSG geometry.
# Extract .ive, convert its scale, re-insert it in .osga archive.
osgarchive -a ${NAME}.osga -e ${NAME}.ive || exit 1
osgconv -s $SCALE -t $POSITION ${NAME}.ive temp.ive || exit 1
mv -f temp.ive ${NAME}.ive || exit 1
osgarchive -a ${NAME}.osga -i ${NAME}.ive || exit 1

# Clean.
rm -f ${NAME}.ive temp*

# Yippy!
echo " "
echo "Succeeded."
exit 0
