#!/usr/local/bin/kermit +
#
# c h a n g e t y p e
#
# Given a list of files, changes the filetype of each file whose filetype
# matches argument \%1 to argument \%2.  Example:
#
#   changetype hlp txt *
#
# The third argument is a filename, a wildcard to match filenames, or
# a list of files.  If it is omitted, all files in the current directory
# are assumed.
#
# Illustrates:
#  . Command-line argument interpretation
#  . Array copying with offset
#  . FOR loop consruction
#  . Using \ffiles() to expand wildards and assign file list to an array
#  . Using \fstripx() to remove the "filetype" part of a filename
#  . RENAME command
#
# Author: F. da Cruz, The Kermit Project, Columbia University, July 1999

.\%n ::= \v(argc) - 1                    ; How many arguments on command line.

if ( < \%n 2 ) exit 1 {Usage: \fbasename(\%0) oldtype newtype [ filelist ]}

switch \%n {
  :2, .\%n := \ffiles(*.\%1,&a), break   ; No args, do * in current directory.
  :3, .\%n := \ffiles(\&_[3],&a), break  ; 1 arg, expand it.
  :default                               ; More than 1 arg, use them literally.
     array copy &_[3] &a[]
     decrement \%n 2
}

for \%i 1 \%n 1 {                        ; Loop through each file
    if not match \&a[\%i] *.\%1 continue
    rename /v \&a[\%i] \fstripx(\&a[\%i]).\%2
}
exit 0
