#!/opt/local/bin/kermit +

# Creates a new directory for all files in the current directory whose
# names start with the given prefix, and also makes tar.gz and Zip archives.
# The files in the original directory are replaced by symlinks to themselves
# in the new directory.  In the process *.hlp files become *hlp.txt and
# *.doc files become *doc.txt, so they can be viewed in Web browsers.
#
# Example:
#
#  cd ~kermit/a
#  kmovedir mskermit ms
#
# moves all the ms* files in the current directory into a new directory,
# mskermit, naming the zip and tar archives mskermit.zip and mskermit.tar.gz.
#
# If the first argument is the name of the current directory, we just
# make (or re-do) the tar and zip archives.
#
# If the second argument is not given, all files from the current directory
# are done, regardless of their names.
#
# If the third argument is given, it is used as the basename for the archive
# files; otherwise the first argument is used.
#
if < \v(argc) 2 {
  echo ?\fword(\%0,-1): Too few arguments
  echo
  echo " Usage: \fword(\%0,-1) newdirectory [fileprefix [archivebasname]]"
  echo " If archivebasename not given the newdirectory name is used."
  echo " If fileprefix not given all files in the current directory are moved."
  echo
  exit 1
}
if  > \v(argc) 4 {
  echo ?\fword(\%0,-1): Too many arguments
  echo
  echo " Usage: \fword(\%0,-1) newdirectory [fileprefix [archivebasname]]"
  echo " Hint: don't put a wildcard character in the fileprefix."
  echo
  exit 1  
}
if not def \%3 .\%3 := \%1	# Default archivebasename to newdirectory name

.indestdir = 0				# Current directory == destination?
.thisdir := \fword(\v(dir),-1,/,ALL)
if equ "\m(thisdir)" "\%1" .indestdir = 1

# .k := \fword(\v(dir),-2)
# if not equ "\m(k)" "kermit" {
#     exit 1 "?\fword(\%0,-1): Run this only in a subdirectory of ~kermit"
# }

# Set the parameters from the command-line arguments

# .dir  := \fword(\v(dir),-1)
# .src  := ~kermit/\m(dir)    
.src  := \v(dir)    
.dest := ~kermit/\%1
.pat  := \%2*
.root := kermit
.base := \%3

show mac root src dest dir pat base indestdir

if indestdir forward makearchives

cd \m(root)				# Make new directory if necessary
if fail exit 1 CD \m(root)
if not directory \m(dest) {
    !~/bin/mmkdir \m(dest)
    if fail exit 1 "FAILURE TO CREATE \m(dest)"
}
cd \m(dest)				# CD to it to make sure it's there
if fail exit 1 "DISAPPEARED: \m(dest)"

cd \m(src)				# CD back to source directory
if fail exit 1
pwd

.n := \ffiles(\m(pat),&a)		# Get the list of files to move
if not n exit 1 No files match "\m(pat)" in \m(src).

# Move each file, leaving behind a symlink to its new location.
# A file with a name like xxx.hlp becomes xxxhlp.txt, and similarly for
# .doc files, thanks to Microsoft's file associations.

for i 1 n 1 {
    if link \&a[i] continue
    xecho \&a[i]...
    if match "\&a[i]" "*.hlp" {		# Special treatment for .hlp files
        ren \&a[i] \m(dest)/\freplace(\&a[i],.hlp,hlp.txt)
        if fail exit 1 RENAME [.hlp] failed - \&a[i]
        !ln -s \m(dest)/\freplace(\&a[i],.hlp,hlp.txt) \&a[i]
	if fail exit 1 SYMLINK [.hlp] failed - \&a[i]
	echo OK
	continue
    }
    if match "\&a[i]" "*.doc" {		# Also for .doc files
        ren \&a[i] \m(dest)/\freplace(\&a[i],.doc,doc.txt)
        if fail exit 1 RENAME [.doc] failed - \&a[i]
        !ln -s \m(dest)/\freplace(\&a[i],.doc,doc.txt) \&a[i]
	if fail exit 1 SYMLINK [.doc] failed - \&a[i]
	echo OK
	continue
    }
    rename \&a[i] \m(dest)/\&a[i]
    if fail exit 1 RENAME failed - \&a[i]
    !ln -s \m(dest)/\&a[i] \&a[i]
    if fail exit 1 SYMLINK failed - \&a[i]
    echo OK
}
cd \m(dest)				# CD to the result directory

:makearchives

for i 1 \ffiles(*,&a) 1 {		# Fix file permissions and group
    if not def \&a[i] break
    if link \&a[i] continue
    xecho  \&a[i]...
    .perms = 644
    if directory \&a[i] .perms = 755
    chmod \m(perms) \&a[i]
    if fail exit
    !chgrp kermit \&a[i]
    if fail exit
    echo OK
}

!~/bin/kmakezipfile \m(base)		# Creat zipfile
if fail echo WARNING: Zipfile failed
!~/bin/kmaketarball \m(base)		# Create tarball
if fail echo WARNING: Tarball failed
exit 0 Done.

; Local Variables:
; comment-column:40
; comment-start:"# "
; End:
