#! /bin/bash
# lreplace: replace one string with another in a file

# Copyright (C) 2004-2013 by Brian Lindholm.  This file is part of the
# littleutils utility set.
#
# The lreplace utility is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 3, or (at your option) any later version.
#
# The lreplace utility is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# the littleutils.  If not, see <http://www.gnu.org/licenses/>.

# get a valid temporary directory and set up traps
TMPWILD=`tempname -w lreplace_$$` || exit 99
trap 'rm -f ${TMPWILD} ; exit 1' 1 2 3 13 15
trap 'rm -f ${TMPWILD} ; exit 0' 0

# get command-line options
DELIMITER='#'
INSTRING=''
INSTRINGSET=n
OUTSTRING=''
OUTSTRINGSET=n
VERBOSITY=1
while getopts d:hi:o:qv opts
do
  case $opts in
    d) DELIMITER=${OPTARG:0:1} ;;
    h) echo 'lreplace 1.0.30'
       echo 'usage: lreplace [-d delimiter] -i INSTRING -o OUTSTRING [-h(elp)] filename ...'
       exit 0 ;;
    i) INSTRING=${OPTARG}
       INSTRINGSET=y ;;
    o) OUTSTRING=${OPTARG}
       OUTSTRINGSET=y ;;
    q) VERBOSITY=$((${VERBOSITY}-1)) ;;
    v) VERBOSITY=$((${VERBOSITY}+1)) ;;
    *) echo 'lreplace 1.0.30'
       echo 'usage: lreplace [-d delimiter] -i INSTRING -o OUTSTRING [-h(elp)] filename ...'
       exit 1 ;;
  esac
done
shift `expr ${OPTIND} - 1`

# make sure we have meaningful input
if [ "$INSTRINGSET" = 'n' -o "$OUTSTRINGSET" = 'n' ]; then
  echo 'lreplace error: both input string and output string must be specified'
  echo 'usage: lreplace [-d delimiter] -i INSTRING -o OUTSTRING [-h(elp)] filename ...'
  exit 1
fi
if [ "X$INSTRING" = 'X' ]; then
  echo 'lreplace error: the input string to be replaced must be of non-zero length'
  exit 1
fi

# run through the files
while [ $# -gt 0 ]; do

  # make sure we can read and modify file
  if [ ! -f "$1" -o ! -r "$1" -o ! -w "$1" ]; then
    echo "lreplace warning: $1 is not a writeable non-directory file"
    shift; continue
  fi

  # run through sed
  TMPFILE=`tempname lreplace_$$` || exit 99
  sed -e "s${DELIMITER}${INSTRING}${DELIMITER}${OUTSTRING}${DELIMITER}g" "$1" > ${TMPFILE}

  # replace if changes occurred
  cmp -s "$1" ${TMPFILE}
  if [ $? -eq 1 ]; then
    cp ${TMPFILE} "$1"
    if [ "$VERBOSITY" -gt 0 ]; then
      echo "$1: text replaced"
    fi
  elif [ "$VERBOSITY" -gt 1 ]; then
    echo "$1: unchanged"
  fi

  # clean up afterwards
  rm -f ${TMPFILE}
  shift
done
rm -f ${TMPWILD}
