head	1.1;
access;
symbols;
locks; strict;
comment	@# @;


1.1
date	2001.05.12.17.50.56;	author stack;	state Exp;
branches;
next	;


desc
@@


1.1
log
@Script to change references to ProjectDownload servlet to the new name for
this servlet, ProjectDownloadList in fileserver '.htaccess' files.  Pass
the script and OLD_DOMAIN and a NEW_DOMAIN and all instances of OLD_DOMAIN
will be replaced by NEW_DOMAIN in fileserver '.htaccess' files (useful
when you are changing a tigris domain).
@
text
@#!/bin/sh
##
## This script finds all .htaccess files.  If an OLD_DOMAIN and
## NEW_DOMAIN parameter set are passed, we change mentions of
## OLD_DOMAIN for NEW_DOMAIN in the .htaccess files found. If 
## any mention of ProjectDownload servlet is found in the 
## .htaccess file, we update it to ProjectDownloadList, the new 
## name for this servlet.
##
## You'd run this script updating the .htaccess files that were
## made by an older version of sourcecast of if you were changing
## the domain a fileserver was serving (i.e. from TIGRIS.ORG to 
## STAGING.TIGRIS.ORG).
## 
## $Id$


## Global variables.
##

# Name of this script.
#
this_script="`basename $0`"

# Name of the directory we're to start looking in.  Passed on cmd-line. 
#
wd=

# Old domain. To be replace w/ new domain.
#
old_domain=

# New domain. To replace old domain.
#
new_domain=


## Usage
##
## Function to print out how to use this command
##
usage( )
{
    cat <<USAGE
Usage: ${this_script} [-o OLD_DOMAIN -n NEW_DOMAIN] DIRECTORY

Options:
 -h|--help  	Prints this usage message and exits
 -o OLD_DOMAIN  Old domain to replace.  
 -n NEW_DOMAIN  New domain to put in place of old domain.

This script finds all .htaccess files in DIRECTORY and does 
the following: 

1. If an OLD_DOMAIN and NEW_DOMAIN parameter set are passed, 
we change mentions of OLD_DOMAIN for NEW_DOMAIN in the .htaccess 
files found. 

2. If any mention of ProjectDownload servlet is found in 
the .htaccess file, we update it to ProjectDownloadList, the 
new name for this servlet.

You would run this script updating the .htaccess files that were
made by an older version of tigris and/or you were changing
the domain a fileserver was serving (i.e. from TIGRIS.ORG to 
STAGING.TIGRIS.ORG).

USAGE
}


## Call usage and then exit w/ the passed in value for result.
##
usageAndExit( )
{
    result="$1"
    usage
    exit $result
}


## Do command-line arguments.
##
cmdline_args( )
{
    local argv="$@@"

    if [ -n "$argv" ] 
    then 
        eval set -- "$argv" --
    else
        eval set -- "-h" --
    fi

    while : # true
    do 
        case $1 in 
            -h|--help) usageAndExit 0;;
            -o) old_domain=$2; shift 2;;
            -n) new_domain=$2; shift 2;;
            --) shift; break;;
            *) 
                if [ -z $wd ]
                then
                    wd=$1; shift; continue;
                fi
                echo "ERROR: Unexpected argument: $1"
                usageAndExit 1;;
        esac
    done
            
    if [ -z $wd -o ! -d $wd ]
    then
        echo "ERROR: DIRECTORY <$wd> is undefined or does not exist."
        usageAndExit 1
    elif [ -n "$old_domain" -a -n "$new_domain" ]
    then : # pass
    elif [ "x$old_domain" = "x" -a "x$new_domain" = "x" ]
    then : # pass
    else
        echo "ERROR: Must define OLD_DOMAIN and NEW_DOMAIN."  
        usageAndExit 1
    fi
}


## Find all .htaccess files.  If it has mention of NEW_DOMAIN, 
## then assume its 'new' and not to be updated.  Else, 
## for all other files, change ProjectDownload to ProjectDownloadList
## and 'old_domain' to 'new_domain'.
##
filterHtaccess( )
{
    local files=

    if [ -n "$new_domain" ]
    then 
        files=`find $wd -type f -name .htaccess | \
            xargs grep -L $new_domain`
    else
        files=`find $wd -type f -name ".htaccess"`
    fi

    for file in $files
    do
        if [ -n "$old_domain" ]
        then
            cat $file \
                | sed "s/\/ProjectDownload?/\/ProjectDownloadList?/g" \
                | sed "s/${old_domain}/${new_domain}/g" \
                > $file.$$
            mv $file.$$ $file
        else
            cat $file \
                | sed "s/\/ProjectDownload?/\/ProjectDownloadList?/g" \
                > $file.$$
            mv $file.$$ $file
        fi
    done
}
     

## Main
##
cmdline_args $@@
filterHtaccess

@
