home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume39 / trm105 / part01 / trm
Text File  |  1993-09-07  |  12KB  |  414 lines

  1. #!/bin/sh
  2. #
  3. # Trevor's rm. Version 1.05
  4. #  It's 'rm' but more... It can handle removeing directories.. recovering
  5. #  'removed' files and undoing the last 'remove'.. read on..
  6. #
  7. #  dont't forget to chmod 755 this file after installing it
  8. #
  9. #  trm file [file..]
  10. #    will MOVE the named files to the $TRMTMP directory
  11. #    If the file being moved already has a version under $TRMTMP then
  12. #    the version number is incremented and the file is moved.
  13. #        EX: trm gm.c      gm.c is currently not in TRMTMP)
  14. #                          TRMTMP would now contail a file called gm.c,01
  15. #                           This is the first version of gm.c under TRMTMP
  16. #              
  17. #            trm gm.c      there already is a 'gm.c' under TRMTMP. This
  18. #               file would be moved to gm.c,02. It is the second
  19. #                          version of gm.c
  20. #
  21. #  trm  -list | -l  [file..]
  22. #    will list the named files that are in the $TRMTMP directory
  23. #      This will list all the versions of the named files.
  24. #      If no file is given then the entire contents of TRMTMP are listed
  25. #
  26. #  trm  -recover | -rc  file [file..]
  27. #    will recover the biggest version of the named files from the
  28. #    $TRMTMP directory to `pwd`
  29. #        EX: trm -rc gm.c     There is are two versions of gm.c in TRMTMP.
  30. #                  gm.c,01 and gm.c,02. trm will grab gm.c,02
  31. #                  and recover it as gm.c
  32. #
  33. #  trm  -undo | -un
  34. #    will undo the last rm
  35. #     meaning you accidently removed a bunch of files and instead of
  36. #        listing them one by one and recovering each.. this will do all of
  37. #        them for you in one shot..
  38. #        NOTE: THIS WILL NOT AUTOMAGICALLY PUT THE FILES BACK FROM WHENCE
  39. #              THEY CAME. IT WILL PUT THE FILES TO `pwd`
  40. #
  41. #  trm  -listundo | -lun
  42. #    will list the undo files
  43. #
  44. #  trm  -purge | -p  [file..]
  45. #    will REMOVE ALL VERSIONS of the named files that are in the
  46. #      $TRMTMP directory
  47. #    [no files named will remove the $TRMTMP directory completly and then
  48. #     remake it]
  49. #
  50. # ---------------------------------------------------------------------------
  51. # Version 1.05  Aug 31 1993
  52. #
  53. # Bug Fix. grab the last part of the file to remove. ie:basename $file
  54. #
  55. # Version 1.04  Aug 26 1993
  56. #
  57. # Bug Fix. when removeing a dir that already exists it would try to use the last
  58. #   entry in the dir as the version number.. bad. Use ls -1d, not ls -1
  59. #
  60. # Version 1.03  Aug  25 1993
  61. #
  62. #  Add version information.
  63. #    Thanx to montanaro@ausable.crd.ge.com (Skip Montanaro) for this.
  64. #    Removing a dir/file that already exists in $TRMTMP, will cause it
  65. #    to be set to the next 'version' of that file. Recovering a file/dir
  66. #    will grab the biggest version.
  67. #  Add basename $0 for getting the program name
  68. #
  69. # Version 1.02  Aug  25 1993
  70. #
  71. #  Add short form options
  72. #  Bug Fix. enclose TRMDEBUG in quotes wherever needed
  73. #
  74. # Version 1.01  Aug  23 1993
  75. #
  76. #   - add TRMDEBUG to set -x and -v for debugging
  77. #   - add -purge option
  78. #   - add check for moving a file ontop of existing one (ONLY if moved failed)
  79. #
  80. # Version 1.00  July 22 1993
  81. #
  82. #  Initial release
  83. #
  84. # ===========================================================================
  85. #  How to Setup
  86. # ===========================================================================
  87. # I put the following in my .cshrc to create the trmlast and trmtmp
  88. # variables for each shell, only if interactive
  89.  
  90. # if ( $?prompt ) then 
  91. #   # create trm variables
  92. #   # NOTE: TRMLOCK is created in the .login
  93. #   setenv TRMLAST $HOME/.trmtmp/.trmlast.`hostname`.$$
  94. #   setenv TRMTMP  $HOME/.trmtmp
  95. # fi
  96.  
  97. # You may also want to put the following remove, recover and listing
  98. # aliases in your .cshrc
  99.  
  100. # alias    rm    'echo "Use trm instead"'
  101. # alias    rc    'trm -recover \!*'
  102. # alias    prc    'trm -purge \!*'
  103. # alias    lsrc    'trm -list \!* | more'
  104. # alias rcu    'trm -undo'
  105. # alias    lsrcu    'trm -listundo | more'
  106.  
  107. # ---------------------------------------------------------------------------
  108. # I also put the following in my .login
  109.  
  110. # # create recover directory.
  111. # # Only the process on the machine that created the directory should be able
  112. # # to remove it
  113. # # NOTE: $TRMTMP and $TRMLAST are created in the .cshrc
  114. # setenv TRMLOCK $HOME/.trmlock.`hostname`.$$
  115.  
  116. # if !( -d $TRMTMP ) then
  117. #    mkdir $TRMTMP
  118. #    chmod 700 $TRMTMP
  119. #    echo "$TRMTMP created at `date`. Lockfile is $TRMLOCK" > $TRMLOCK
  120. # fi
  121.  
  122. # ---------------------------------------------------------------------------
  123. # I also put the following in my .logout
  124.  
  125. # if ( -f $TRMLOCK ) then
  126. #   /bin/rm -rf $TRMTMP
  127. #   /bin/rm -f $TRMLOCK
  128. # fi
  129.  
  130. # ===========================================================================
  131. # start of trm
  132. # ===========================================================================
  133.  
  134. # get program name
  135. prog=`basename $0`
  136.  
  137. # setenv TRMDEBUG to 1 to display verbose debugging info
  138. if [ "$TRMDEBUG" = "1" ]; then
  139.    set -x
  140.    set -v
  141. fi
  142.  
  143. # if $TRMTMP is not set then look under $home/.trmtmp
  144. if [ "$TRMTMP" = "" ]; then
  145.    TRMTMP=$HOME/.trmtmp
  146. fi
  147.  
  148. # make sure $TRMTMP is a directory
  149. if [ ! -d $TRMTMP ]; then
  150.    echo $prog: ERROR: $TRMTMP is not a directory or does not exist
  151.    exit 1
  152. fi
  153.  
  154. # if no arguments passed then return
  155. if [ $# -eq 0 ]; then
  156.   exit 0
  157. fi
  158.  
  159. # ---------------------------------------------------------------------------
  160. # handle listing
  161. # ---------------------------------------------------------------------------
  162. if [ "$1" = "-list" -o "$1" = "-l" ]; then
  163.  
  164.    # shift out -list from the arg list
  165.    shift
  166.  
  167.    # if no futher args then list the directory
  168.    if [ $# -eq 0 ]; then
  169.      ls -laug $TRMTMP
  170.      exit 0
  171.    fi
  172.  
  173.    # cd to the directory and list each argument individually
  174.    cd $TRMTMP
  175.    for i in $*; do
  176.      ls -laug $i,*
  177.    done
  178.  
  179.    exit 0
  180. fi
  181.  
  182. # ---------------------------------------------------------------------------
  183. # handle recover
  184. # ---------------------------------------------------------------------------
  185. if [ "$1" = "-recover"  -o "$1" = "-rc" ]; then
  186.  
  187.    # shift out -recover from the arg list
  188.    shift
  189.  
  190.    # if no futher args then exit
  191.    if [ $# -eq 0 ]; then
  192.      exit 0
  193.    fi
  194.  
  195.    # the destination directory is where you are now
  196.    destdir=`pwd`
  197.  
  198.    # cd to the TRMTMP directory
  199.    cd $TRMTMP
  200.  
  201.    # for each arg passed try to 'mv' it to the destination dir
  202.    for i in $*; do
  203.  
  204.      # get back the biggest version
  205.      rev=`(ls -1d $TRMTMP/$i,* 2>/dev/null) |\
  206.         sort |\
  207.         tail -1 |\
  208.         sed -e 's/.*,\([0-9]*\)/\1/'`
  209.      
  210.      /bin/mv -f $i,$rev $destdir/$i > /dev/null 2>&1
  211.  
  212.      if [ $? != 0 ]; then
  213.        # the move failed... 
  214.  
  215.       # probably tried to remove a file that already exists in $TRMTMP
  216.       if [ -d $destdir/$i ]; then
  217.     echo $prog: ${i} already exists in $destdir as a dir, move if needed.
  218.       elif [ -f $destdir/$i ]; then
  219.         echo $prog: ${i} already exists in $destdir as a file, move if needed.
  220.       elif [ -d $i ]; then
  221.          # tried to move a directory.. most likely failure is trying to move
  222.          # a directory across filesystems.. sooo lets tar up the whole thing
  223.          # and then untar it in the destination directory
  224.          tar -cBf - $i,$rev | ( cd $destdir ; tar -xBpf - )
  225.      mv $destdir/$i,$rev $destdir/$i
  226.  
  227.      # now remove the directory from $TRMTMP
  228.          /bin/rm -rf $i,$rev
  229.  
  230.        fi
  231.      fi
  232.    done
  233.    exit 0
  234. fi
  235.  
  236. # ---------------------------------------------------------------------------
  237. # handle undo
  238. # ---------------------------------------------------------------------------
  239. if [ "$1" = "-undo" -o "$1" = "-un" ]; then
  240.  
  241.    # the destination directory is where you are now
  242.    destdir=`pwd`
  243.  
  244.    # cd to the TRMTMP directory
  245.    cd $TRMTMP
  246.  
  247.    # if $TRMLAST is not set then we have nothing to do
  248.    if [ "$TRMLAST" = "" ]; then
  249.      exit 0
  250.    fi
  251.  
  252.    # if $TRMLAST does not exist then we have nothing to do
  253.    if [ ! -f $TRMLAST ]; then
  254.      exit 0
  255.    fi
  256.  
  257.    # foreach file in the $TRMLAST list try to 'mv' it to the destination dir
  258.    for i in `cat $TRMLAST`; do
  259.  
  260.      # get back the biggest version
  261.      rev=`(ls -1d $TRMTMP/$i,* 2>/dev/null) |\
  262.         sort |\
  263.         tail -1 |\
  264.         sed -e 's/.*,\([0-9]*\)/\1/'`
  265.      
  266.      /bin/mv -f $i,$rev $destdir/$i > /dev/null 2>&1
  267.  
  268.      if [ $? != 0 ]; then
  269.        # the move failed... 
  270.  
  271.       # probably tried to remove a file that already exists in $TRMTMP
  272.       if [ -d $destdir/$i ]; then
  273.     echo $prog: ${i} already exists in $destdir as a dir, move if needed.
  274.       elif [ -f $destdir/$i ]; then
  275.         echo $prog: ${i} already exists in $destdir as a file, move if needed.
  276.       elif [ -d $i ]; then
  277.          # tried to move a directory.. most likely failure is trying to move
  278.          # a directory across filesystems.. sooo lets tar up the whole thing
  279.          # and then untar it in the destination directory
  280.          tar -cBf - $i,$rev | ( cd $destdir ; tar -xBpf - )
  281.      mv $destdir/$i,$rev $destdir/$i
  282.  
  283.      # now remove the directory from $TRMTMP
  284.          /bin/rm -rf $i,$rev
  285.  
  286.        fi
  287.      fi
  288.    done
  289.  
  290.    # at this point we want to reset the $TRMLAST file list
  291.    /bin/rm -f $TRMLAST
  292.    echo > $TRMLAST
  293.  
  294.    exit 0
  295. fi
  296.  
  297. # ---------------------------------------------------------------------------
  298. # handle listundo
  299. # ---------------------------------------------------------------------------
  300. if [ "$1" = "-listundo" -o "$1" = "-lun" ]; then
  301.  
  302.    # if $TRMLAST is not set then we have nothing to do
  303.    if [ "$TRMLAST" = "" ]; then
  304.      exit 0
  305.    fi
  306.  
  307.    # if $TRMLAST does not exist then we have nothing to do
  308.    if [ ! -f $TRMLAST ]; then
  309.      exit 0
  310.    fi
  311.  
  312.    # cat the $TRMLAST file
  313.    cat $TRMLAST
  314.  
  315.    exit 0
  316. fi
  317.  
  318. # ---------------------------------------------------------------------------
  319. # handle purge
  320. # ---------------------------------------------------------------------------
  321. if [ "$1" = "-purge" -o "$1" = "-p" ]; then
  322.  
  323.    # shift out -purge from the arg list
  324.    shift
  325.  
  326.    # if no futher args then remove $TRMTMP and remake it
  327.    if [ $# -eq 0 ]; then
  328.      /bin/rm -rf $TRMTMP
  329.      mkdir $TRMTMP
  330.      exit 0
  331.    fi
  332.  
  333.    # cd to the directory and remove each argument individually
  334.    cd $TRMTMP
  335.    for i in $*; do
  336.      /bin/rm -rf $i,*
  337.    done
  338.  
  339.    exit 0
  340. fi
  341.  
  342. # ---------------------------------------------------------------------------
  343. # handle remove
  344. # ---------------------------------------------------------------------------
  345.  
  346. # at this point we want to reset the old $TRMLAST file list
  347.  if [ "$TRMLAST" != "" ]; then
  348.    /bin/rm -f $TRMLAST
  349.  fi
  350.  
  351. for i in $*; do
  352.  
  353.   base=`basename $i`
  354.   dir=`dirname $i`
  355.  
  356.   # if the filename passed does not exist then echo what 'rm' would  
  357.   # for now only deal with files and dirs..
  358.   if [  ! -f $i  -a  ! -d $i  ]; then
  359.      echo $prog: ${i}: No such file or directory
  360.   else
  361.     # figure out a version number to move the file to
  362.     rev=`(ls -1d $TRMTMP/$base,* 2>/dev/null) |\
  363.         sort |\
  364.         tail -1 |\
  365.         sed -e 's/.*,\([0-9]*\)/\1/'`
  366.     if [ "$rev" = "" ] ; then
  367.        rev=1;
  368.     else
  369.        rev=`expr $rev + 1`
  370.     fi
  371.     if [ "$rev" = "" ]; then
  372.       rev=01
  373.     fi
  374.     rev=`echo $rev | awk '{if(length($1)==1){print 0$1}else{print $1}}'`
  375.     if [ $rev -gt 99 ]; then
  376.       echo $prog: ${i} already has 99 versions, staying at 99
  377.       rev=99
  378.     fi
  379.  
  380.     # try to move the file to $TRMTMP
  381.     /bin/mv -f $i $TRMTMP/$base,$rev > /dev/null 2>&1
  382.  
  383.     if [ $? != 0 ]; then
  384.       # the move failed
  385.  
  386.       # probably tried to remove a file that already exists in $TRMTMP
  387.       if [ -d $i ]; then
  388.         # tried to move a directory.. most likely failure is trying to move
  389.         # a directory across filesystems.. sooo lets tar up the whole thing
  390.         # and then untar it in the $TRMTMP directory
  391.     pd=`pwd`
  392.     cd $dir
  393.         tar -cBf - $base | ( cd $TRMTMP ; tar -xBf - ) > /dev/null 2>&1
  394.     mv $TRMTMP/$base $TRMTMP/$base,$rev
  395.  
  396.         # now remove the directory
  397.         /bin/rm -rf $base
  398.     cd $pd
  399.       fi
  400.  
  401.     fi
  402.  
  403.     # at this point the 'remove' was succesfull. Add the filename to the
  404.     # $TRMLAST file for possible undoing.
  405.     if [ "$TRMLAST" != "" ]; then
  406.       echo $i >> $TRMLAST
  407.     fi
  408.  
  409.   fi
  410.  
  411. done
  412.  
  413.