copying colr files

Hello !

I hope you people out there don't get tired of my color copying problems.
Today I found some cases where my script didn't work. So here is todays
version that should be much better or general at least. I would be glad if
someone could confirm if this version is ok now. It seems to work for
me now on the test examples that I could come up with. This is of importance
for me since I usually work with several mapsets and some of my programs aren't
taking care of the color files. So this script should be my safe way of doing
this.

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, otherwise we use the colr file.
#
# Copies the colortable to: $LOCATION/colr2/mapset/$out_name 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/$out_name.
#
# 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: 930325
# 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*)
      IN=`echo $i | sed s/input=//` ;;
    in*)
      IN=`echo $i | sed s/in=//` ;;
    i*)
      IN=`echo $i | sed s/i=//` ;;
    output*)
      OUT=`echo $i | sed s/output=//` ;;
    out*)
      OUT=`echo $i | sed s/out=//` ;;
    o*)
      OUT=`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=$OUT`
if [ ! "$file" ] ; then
   echo "$OUT - cell file not found"
   exit 2
fi

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

IN_FILE=$file
IN_MAPSET=$mapset

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

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

# Check if the input map has a colr2 file in
# current mapset. Use this if it exists.
# Here we shouldn't use the mapset
# specification in the file parameter
# if one is given on the command line

eval `g.findfile element=colr2/$IN_MAPSET file=$IN_NAME`
if [ "$file" ] ; then
   IN_FILE=$file
fi
#-----------------------------------------------------------------------------

# Check for existance of colr file and if output
# file is in current mapset.

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

eval `g.findfile element=cell file=$OUT`
OUT_MAPSET=$mapset

eval `g.findfile element=colr mapset=$OUT_MAPSET file=$OUT_NAME`
if [ ! "$file" -a "$OUT_MAPSET" = "$mapset" ] ; then
   OUT_FILE=$LOCATION/colr/$OUT_NAME
   SAMEMAPSET=1
else
   OUT_FILE=$LOCATION/colr2/$OUT_MAPSET/$OUT_NAME
   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/$OUT_MAPSET -a $SAMEMAPSET -eq 0 ] ; then
   echo "Creating: $LOCATION/colr2/$OUT_MAPSET"
   mkdir $LOCATION/colr2/$OUT_MAPSET
fi

# Copy the color file

echo "Executing: cp $IN_FILE $OUT_FILE"
cp $IN_FILE $OUT_FILE