#!/bin/sh
#
# mved     ---   Move-and-edit filenames.
#
#  Usage: mved [-n] [-m|-c|-l] from-pattern to-pattern
#  Usage: cped [-n] [-m|-c|-l] from-pattern to-pattern
#  Usage: lned [-n] [-m|-c|-l] from-pattern to-pattern
#  Options:  -n   do nothing, just show what would be done.
#            -m   rename move (mv)
#            -c   rename copy (cp -p)
#            -l   rename symbolic link (ln -s)
#
#    The '=' character in from-pattern is treated as a special wildcard,
# matching in the same way as the shell '*' wildcard character, except
# that the text matching the '=' in the first pattern is inserted in place
# of any = wildcards in the second.  One one '=' may appear in a pattern.
#    NOTE: the from-pattern need not have a wildcard if to-pattern does,
# a default
#
# Restrictions:
#   Only the first '=' sign in from-pattern is used.  Multiple = wildcards
# in from-pattern match up with the first from-pattern =, ie: there is no
# matching for multiple = signs.  (I'm sure someone could make it work if
# they wanted to... ?)
#
# EG: mved lib=.a =.a    moves   libhello.a to hello.a
#     mved =.o =.o.old   moves  fred.o to fred.o.old
#     mved '=.*' =       moves  fred.junk to fred
#     mved =.sh =        moves  mved.sh to mved
#     mved *.sh =.       moves  mved.sh to mved.sh.
#
# Original :   Brian Coogan      06 Jan 1987
# Modified :   Anthony Thyssen   25 Jun 1993
#
PATH=/bin:/usr/bin export PATH
myname=`basename $0`
shopt=x
cmd=mv

case $myname in
  mv*) cmd="mv";;
  cp*) cmd="cp -p";;
  ln*) cmd="ln -s";;
esac

case $# in
  2|3) ;;
  0|1) cat - 1>&2 <<CatUsage
Usage: mved [-n] [-c] from-pattern to-pattern
Usage: cped [-n] [-m] from-pattern to-pattern
Options:  -n   do nothing, just show what would be done.
          -m   rename move (mv)
          -c   rename copy (cp -p)
          -l   rename symbolic link (ln -s)

   The '=' character in from-pattern is treated as a special wildcard,
matching in the same way as the shell '*' wildcard character, except
that the text matching the '=' in the first pattern is inserted in place
of any = wildcards in the second.  One one '=' may appear in a pattern.
   NOTE: the from-pattern need not have a wildcard if to-pattern does,
a default

CatUsage
     exit 1
     ;;
esac

case "$1" in
-n) shopt=vn; shift ;;
-m) cmd="mv";    shift ;;
-c) cmd="cp -p"; shift ;;
-l) cmd="ln -s"; shift ;;
-*) echo $myname: unknown option $1 1>&2
    exit 1
    ;;
esac

# Check for appropriate wildcards.
# Source must have an = wildcard or already exist.
case "$1" in
*=*)  ;;
*)for n in $1
  do
    if [ ! -f "$n" ]
    then
      echo $myname: no files match $1 1>&2
      exit 1
    elif [ "$2x" = =x ]
    then
      echo $myname: impossible 1>&2
      exit 1
    fi
    break
  done
  ;;
esac
case "$2" in
*=*)  ;;
*) echo $myname: no \'=\' wildcards used in target 1>&2
   exit 1
   ;;
esac
# catch mved = =
case "$1$2" in
==) echo $myname: impossible $cmd 1>&2
    exit 1
    ;;
esac


globpatt="`echo "$1" | sed 's/=/\*/'; `"

frompatt="`echo "$1" | sed \
      -e 's/\./\\\\./g' \
      -e 's/\*/.*/g' \
      -e 's/?/./g' \
      -e 's/=/\\\\(\\.\\*\\\\)/' \
      -e '/\\\\(/ !s/.*/\\\\(&\\\\)/'
      `"
topatt="`echo "$2" | sed -e 's/=/\\\\1/g'`"

IFS='
'
for n in `ls -1d $globpatt`   # ls used in case spaces appears in filenames
do
  # Check the pattern got expanded.  (The file might also have vanished).
  if [ ! -f "$n" ]
  then
    echo $myname: no files matching $1 found 1>&2
    exit 1
  fi
  echo "$n"
done |
  # NOTE: used ; and substition seperator to avoid problems with / in paths
  sed -n "s;$frompatt;$cmd '&' '$topatt';p" | sh -$shopt

