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


1.2
date	2001.05.30.00.22.14;	author stack;	state Exp;
branches;
next	1.1;

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


desc
@@


1.2
log
@Require specifying dir to filter.
@
text
@#!/bin/sh
## 
## Filter all files in the passed directory ending in '.sql' 
## transforming one string to another.  Results are put into
## a directory named /tmp/THIS_SCRIPT_NAME.output.
##
this_script="`basename $0`"
output_dir="/tmp/${this_script}.output"
silent=0
src_dir=
str_to_replace=
str_replacement=
file_suffix=".sql"


## Usage
##
## Function to print out how to use this command
##
function usage
{
    cat <<USAGE
Usage: ${this_name} [options] -d SRC_DIR STR_TO_REPLACE STR_REPLACEMENT

Options:
 -h,--help          Prints this usage message and exits.
 -s,--silent        Silence output

This script operates on database dump files (Output from
mysqldump) looking for STR_TO_REPLACE and replacing all
instances w/ STR_REPLACEMENT.  You must pass it a src directory,
the '-d' parameter, that contains dumps from a tigris database.  
It filters files w/ a '.sql' suffix and a 'issues_' or 'tigris' 
prefix putting the output into /tmp/THIS_SCRIPTS_NAME.output.  

USAGE
}


##
## Command-line processing
##
## Note that we use `"$@@"' to let each command-line parameter expand to a 
## separate word. The quotes around `$@@' are essential!
## We need TEMP as the `eval set --' would nuke the return value of getopt.
##
function cmdline
{
    TEMP=`getopt -o hsd: --long help,silent,dir: -n "${this_script}" -- "$@@"`
    if [ $? != 0 ]
    then 
        echo "ERROR: getopt returned $?" >&2 
        exit 1
    fi

    eval set -- "$TEMP" # Note the quotes around `$TEMP' are necessary.

    while true ; do
        case "$1" in
            -h|--help) usage; exit 0 ;;
            -s|--silent) silent=1; shift 1 ;;
            -d|--dir) src_dir=$2; shift 2 ;;
            --) shift ; break ;;
            *) echo "Internal error!" ; exit 1 ;;
        esac
    done

    if [ ! -n "$src_dir" ]
    then
        echo "ERROR: You must pass a SRC_DIR"
        usage
        exit 1
    fi

    if [ ! -d "$src_dir" ]
    then
        echo "ERROR: SRC_DIR $src_dir does not exist"
        usage
        exit 1
    fi

    # Extra arguments are what to filter for.
    #
    if [ $# -ne 2 ]
    then 
        echo "ERROR: You must specify STR_TO_REPLACE & STR_REPLACEMENT"
        usage
        exit 1
    fi
    str_to_replace=$1
    str_replacement=$2
}


## Main
##
cmdline $@@
files=`find ${src_dir} -type f -name "*${file_suffix}" -mindepth 1 -maxdepth 1`

if [ ${silent} -eq 0 ]
then 
    echo "Outputting to ${output_dir}"
    if [ ! -d ${output_dir} ]
    then
        mkdir -p --verbose ${output_dir}
    fi
else
    mkdir -p ${output_dir}
fi

for file in ${files}
do
    new_file="${output_dir}/`basename ${file}`"
    if [ ${silent} -eq 0 ];
    then
        echo -n "."
    fi
    cat ${file} | \
         sed --expression="s/${str_to_replace}/${str_replacement}/g" \
             > ${new_file}
done

if [ ${silent} -eq 0 ];
then
    echo
fi
@


1.1
log
@First time add of domain name changing filter script to run against mysqldump data.
@
text
@d10 1
a10 1
src_dir=.
d23 2
a24 1
Usage: ${this_name} [options] STR_TO_REPLACE STR_REPLACEMENT
a27 1
 -d DIR,--dir=DIR   Directory w/ source files. Default is '.'.
d31 4
a34 3
instances w/ STR_REPLACEMENT.  You'd run this script when
changing the domain name that is in the dump data from 
say production.site.com to staging.site.com.
d68 14
d86 1
a86 1
        echo "ERROR: Specify STR_TO_REPLACE & STR_REPLACEMENT"
a89 1

d94 1
d102 1
a102 1
    echo "Putting output into ${output_dir}"
d113 5
d120 1
a120 1
             > ${output_dir}/${file}
d122 5
@

