home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / tools.zip / kmovedir < prev    next >
Text File  |  2011-08-17  |  4KB  |  140 lines

  1. #!/opt/local/bin/kermit +
  2.  
  3. # Creates a new directory for all files in the current directory whose
  4. # names start with the given prefix, and also makes tar.gz and Zip archives.
  5. # The files in the original directory are replaced by symlinks to themselves
  6. # in the new directory.  In the process *.hlp files become *hlp.txt and
  7. # *.doc files become *doc.txt, so they can be viewed in Web browsers.
  8. #
  9. # Example:
  10. #
  11. #  cd ~kermit/a
  12. #  kmovedir mskermit ms
  13. #
  14. # moves all the ms* files in the current directory into a new directory,
  15. # mskermit, naming the zip and tar archives mskermit.zip and mskermit.tar.gz.
  16. #
  17. # If the first argument is the name of the current directory, we just
  18. # make (or re-do) the tar and zip archives.
  19. #
  20. # If the second argument is not given, all files from the current directory
  21. # are done, regardless of their names.
  22. #
  23. # If the third argument is given, it is used as the basename for the archive
  24. # files; otherwise the first argument is used.
  25. #
  26. if < \v(argc) 2 {
  27.   echo ?\fword(\%0,-1): Too few arguments
  28.   echo
  29.   echo " Usage: \fword(\%0,-1) newdirectory [fileprefix [archivebasname]]"
  30.   echo " If archivebasename not given the newdirectory name is used."
  31.   echo " If fileprefix not given all files in the current directory are moved."
  32.   echo
  33.   exit 1
  34. }
  35. if  > \v(argc) 4 {
  36.   echo ?\fword(\%0,-1): Too many arguments
  37.   echo
  38.   echo " Usage: \fword(\%0,-1) newdirectory [fileprefix [archivebasname]]"
  39.   echo " Hint: don't put a wildcard character in the fileprefix."
  40.   echo
  41.   exit 1  
  42. }
  43. if not def \%3 .\%3 := \%1    # Default archivebasename to newdirectory name
  44.  
  45. .indestdir = 0                # Current directory == destination?
  46. .thisdir := \fword(\v(dir),-1,/,ALL)
  47. if equ "\m(thisdir)" "\%1" .indestdir = 1
  48.  
  49. # .k := \fword(\v(dir),-2)
  50. # if not equ "\m(k)" "kermit" {
  51. #     exit 1 "?\fword(\%0,-1): Run this only in a subdirectory of ~kermit"
  52. # }
  53.  
  54. # Set the parameters from the command-line arguments
  55.  
  56. # .dir  := \fword(\v(dir),-1)
  57. # .src  := ~kermit/\m(dir)    
  58. .src  := \v(dir)    
  59. .dest := ~kermit/\%1
  60. .pat  := \%2*
  61. .root := kermit
  62. .base := \%3
  63.  
  64. show mac root src dest dir pat base indestdir
  65.  
  66. if indestdir forward makearchives
  67.  
  68. cd \m(root)                # Make new directory if necessary
  69. if fail exit 1 CD \m(root)
  70. if not directory \m(dest) {
  71.     !~/bin/mmkdir \m(dest)
  72.     if fail exit 1 "FAILURE TO CREATE \m(dest)"
  73. }
  74. cd \m(dest)                # CD to it to make sure it's there
  75. if fail exit 1 "DISAPPEARED: \m(dest)"
  76.  
  77. cd \m(src)                # CD back to source directory
  78. if fail exit 1
  79. pwd
  80.  
  81. .n := \ffiles(\m(pat),&a)        # Get the list of files to move
  82. if not n exit 1 No files match "\m(pat)" in \m(src).
  83.  
  84. # Move each file, leaving behind a symlink to its new location.
  85. # A file with a name like xxx.hlp becomes xxxhlp.txt, and similarly for
  86. # .doc files, thanks to Microsoft's file associations.
  87.  
  88. for i 1 n 1 {
  89.     if link \&a[i] continue
  90.     xecho \&a[i]...
  91.     if match "\&a[i]" "*.hlp" {        # Special treatment for .hlp files
  92.         ren \&a[i] \m(dest)/\freplace(\&a[i],.hlp,hlp.txt)
  93.         if fail exit 1 RENAME [.hlp] failed - \&a[i]
  94.         !ln -s \m(dest)/\freplace(\&a[i],.hlp,hlp.txt) \&a[i]
  95.     if fail exit 1 SYMLINK [.hlp] failed - \&a[i]
  96.     echo OK
  97.     continue
  98.     }
  99.     if match "\&a[i]" "*.doc" {        # Also for .doc files
  100.         ren \&a[i] \m(dest)/\freplace(\&a[i],.doc,doc.txt)
  101.         if fail exit 1 RENAME [.doc] failed - \&a[i]
  102.         !ln -s \m(dest)/\freplace(\&a[i],.doc,doc.txt) \&a[i]
  103.     if fail exit 1 SYMLINK [.doc] failed - \&a[i]
  104.     echo OK
  105.     continue
  106.     }
  107.     rename \&a[i] \m(dest)/\&a[i]
  108.     if fail exit 1 RENAME failed - \&a[i]
  109.     !ln -s \m(dest)/\&a[i] \&a[i]
  110.     if fail exit 1 SYMLINK failed - \&a[i]
  111.     echo OK
  112. }
  113. cd \m(dest)                # CD to the result directory
  114.  
  115. :makearchives
  116.  
  117. for i 1 \ffiles(*,&a) 1 {        # Fix file permissions and group
  118.     if not def \&a[i] break
  119.     if link \&a[i] continue
  120.     xecho  \&a[i]...
  121.     .perms = 644
  122.     if directory \&a[i] .perms = 755
  123.     chmod \m(perms) \&a[i]
  124.     if fail exit
  125.     !chgrp kermit \&a[i]
  126.     if fail exit
  127.     echo OK
  128. }
  129.  
  130. !~/bin/kmakezipfile \m(base)        # Creat zipfile
  131. if fail echo WARNING: Zipfile failed
  132. !~/bin/kmaketarball \m(base)        # Create tarball
  133. if fail echo WARNING: Tarball failed
  134. exit 0 Done.
  135.  
  136. ; Local Variables:
  137. ; comment-column:40
  138. ; comment-start:"# "
  139. ; End:
  140.