copying colr files

Hello Grass users !

For those of you that followed my problems with color files yesterday and
the day before yesterday I will here give you my latest solution to this
problem. I hope that I have learned my lesson about colr2 files now.
I had overseen some possibilities in yesterdays script so here is a new
version of "cp.colr.sh"

I think this script could be educational how the colr2 files work and also
how g.findfile can be used. Please tell me if I have missed something !!
I know that this should be sent to grassp instead but since I sent yesterdays
script to grassu I thought it was better to send this to grassu as well.

Lars

Lars Schylberg Email: larss@fmi.kth.se
Dept. of Geodesy and Photogrammetry
Royal Institute of Technology (KTH) Tel. +46 8 790 86 33
S-100 44 STOCKHOLM, SWEDEN Fax. +46 8 790 66 10

#!/bin/sh
#
# cp.colr.sh
#
# Script to copy one color table from one raster map to another raster map
#
# The script checks if there exists a colr2 file in current mapset
# for the input map and uses this file in that case, since this is the
# file that is used by d.rast.
# Copies the colortable to: $LOCATION/colr2/mapset/cellname unless the output
# map is in the current mapset and there exist no colr file for the output
# then it is written to $LOCATION/colr/cellname.
#
# Read 4.0 Programmers Manual 12.10.3.1 for details about colr2 files
#
# Author: Lars Schylberg ( larss@fmi.kth.se )
# Department of Geodesy and Photogrammetry
# Royal Inst. of Technolgy
# Stockholm, Sweden
#
# Date: 930324
# History of changes:
#
#--------------------------------------------------------------------------
# Check if GRASS is running
#
test "$GISRC" || echo "GRASS is not running" || exit 2
#----------------------------------------------------------------------------
#
# Evaluate arguments
#
if [ $# != 2 ]
then
    echo
    echo Usage: `basename $0`
    echo ' input=mapname '
    echo ' output=mapname '
    echo
    exit 1
fi
#
# parse input arguments
#
for i do
  case $i in
    input*)
      INPUT=`echo $i | sed s/input=//` ;;
    in*)
      INPUT=`echo $i | sed s/in=//` ;;
    i*)
      INPUT=`echo $i | sed s/i=//` ;;
    output*)
      OUTPUT=`echo $i | sed s/output=//` ;;
    out*)
      OUTPUT=`echo $i | sed s/out=//` ;;
    o*)
      OUTPUT=`echo $i | sed s/o=//` ;;
    *)
      echo ""
      echo "Unrecognized option: $i"
      echo 'Options: input=mapname '
      echo ' output=mapname '
                        echo
      exit 1
  esac
done
#-----------------------------------------------------------------------

# Check the input arguments

eval `g.findfile element=cell file=$OUTPUT`
if [ ! "$file" ] ; then
   echo "$OUTPUT - cell file not found"
   exit 2
fi

eval `g.findfile element=colr file=$INPUT`
if [ ! "$file" ] ; then
   echo "$INPUT - color file not found"
   exit 2
fi

INFILE=$file
INMAPSET=$mapset

# Use only the first part of the file
# name since the file paratmeter requires that

INPUT_NAME=`echo $INPUT | awk -F@ '{ printf "%s\n", $1 }'`

# Check if the input map has a colr2 file in
# current mapset. Use this if it exists.

eval `g.findfile element=colr2/$INMAPSET mapset=$MAPSET file=$INPUT_NAME`
if [ "$file" ] ; then
   INFILE=$file
fi

# Check for existance of colr file and if output
# file is in current mapset otherwise we can't
# write to it

OUTPUT_NAME=`echo $OUTPUT | awk -F@ '{ printf "%s\n", $1 }'`
OUTPUT_MAPSET=`echo $OUTPUT | awk -F@ '{ printf "%s\n", $2 }'`

eval `g.findfile element=colr mapset=$OUTPUT_MAPSET file=$OUTPUT_NAME`
if [ ! "$file" -a "$MAPSET" = "$mapset" ] ; then
   OUTFILE=$LOCATION/colr/$OUTPUT_NAME
   SAMEMAPSET=1
else
   OUTFILE=$LOCATION/colr2/$INMAPSET/$OUTPUT
   SAMEMAPSET=0
fi

# Make sure that output path exists
# if the output is in another mapset.

if [ ! -d $LOCATION/colr2 -a $SAMEMAPSET -eq 0 ] ; then
   echo "Creating: $LOCATION/colr2"
   mkdir $LOCATION/colr2
fi

if [ ! -d $LOCATION/colr2/$INMAPSET -a $SAMEMAPSET -eq 0 ] ; then
   echo "Creating: $LOCATION/colr2/$INMAPSET"
   mkdir $LOCATION/colr2/$INMAPSET
fi

# Copy the color file

cp $INFILE $OUTFILE