home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / disk-man / mtools-3.000 / mtools-3 / mtools-3.0 / scripts / xcopy < prev   
Encoding:
Text File  |  1996-05-03  |  3.6 KB  |  187 lines

  1. #!/bin/sh
  2. #
  3. # xcopy
  4. #
  5. # Recursively copy a directory tree
  6. #
  7. # Requires cpio, find, tty, basename, and any reasonable /bin/sh
  8. #
  9. # 1994/01/15    DCN    Created
  10. # 1994/12/01    DCN    Cleanup and minor improvements
  11. #
  12. # Copyright (C) 1994 David C. Niemi (niemidc@erols.com)
  13. # The author requires that any copies or derived works include this
  14. # copyright notice; no other restrictions are placed on its use.
  15. #
  16.  
  17. set -e
  18. set -u
  19.  
  20. ## Exit and complain about improper usage
  21. usage_exit ()
  22. {    echo ''
  23.     if [ -n "${INTERACTIVE-}" ]; then
  24.         echo \
  25.         "Usage: $NAME [-s | -S] [ <source dir> [ <destination dir> ] ]" >&2
  26.         exit 2
  27.     else
  28.         echo \
  29. "Non-interactive usage: $NAME [-s|-S] <source dir> [<destination dir>]" >&2
  30.         exit 3
  31.     fi
  32. }
  33.  
  34. ##  Clean up path name $1
  35. fix_dir_name ()
  36. {
  37.     ## Remove leading "./" from path
  38.     _DIR=`echo "$1" | sed -e 's,^\./,,' -e 's,/./,,'`
  39.     _WD=${WD-}
  40.  
  41.     ## Parse away leading ../'s
  42.     while echo "$_DIR" | grep -s '^\.\./'; do
  43.            _DIR=`echo $_DIR | sed 's,^\.\./,,'`
  44.         _WD=`dirname "$_WD"`
  45.     done
  46.  
  47.     ## Clean up and return resulting path
  48.     case $_DIR in
  49.     '' | .)
  50.         ## Use current working directory
  51.         _DIR=$_WD
  52.         ;;
  53.  
  54.     /*)
  55.         ## Absolute path; no change needed
  56.         ;;
  57.  
  58.     ..)
  59.         _DIR=`dirname "$_WD"`
  60.         ;;
  61.  
  62.     *)
  63.         ## Path relative to "."
  64.         _DIR=$_WD/$_DIR
  65.         ;;
  66.     esac
  67.     echo $_DIR
  68. }
  69.  
  70. ## Determine if we are using a real tty or not
  71. if tty -s; then
  72.     INTERACTIVE=yes
  73. fi
  74.  
  75. ## Ensure these variables get set
  76. FROMDIR=${FROMDIR-}
  77. TODIR=${TODIR-}
  78. NAME=`basename $0`
  79.  
  80. ## Make sure that the current directory is really the same as $PWD
  81. if [ -z "${PWD-}" ]; then
  82.     ## PWD variable is null or not set
  83.     WD=`pwd`
  84. elif [ ! -d "$PWD" ]; then
  85.     ## $PWD does not exist or is not a directory
  86.     WD=`pwd`
  87. elif [ X"`ls -id`" != X"`cd $PWD; ls -id`" ]; then
  88.     ## PWD variable is not up to date, so ignore it
  89.     WD=`pwd`
  90. else
  91.     ## Use $PWD, as it is likely to be "cleaner" that `pwd`
  92.     WD=$PWD
  93. fi
  94.  
  95. ## Check usage
  96. case $# in
  97. 0)
  98.     if [ -z "$INTERACTIVE" ]; then
  99.         usage_exit
  100.     fi
  101.     ;;
  102. 1)
  103.     FROMDIR=`fix_dir_name $1`
  104.     TODIR="${WD}/`basename ${WD}/$FROMDIR`"
  105.     ;;
  106. 2)
  107.     FROMDIR=`fix_dir_name $1`
  108.     TODIR=`fix_dir_name $2`
  109.     ;;
  110. *)
  111.     usage_exit
  112.     ;;
  113. esac
  114.  
  115. ####    Ask for missing information
  116. while [ -z "$FROMDIR" -o -z "$TODIR" ]; do
  117.     if [ -z "$FROMDIR" ]; then
  118.         echo "Your current directory is \"$WD\"."
  119.         echo -n 'What directory do you want to copy (default is "'$WD'")? '
  120.         if read answer; then
  121.             if [ ! -d "$answer" ]; then
  122.                 echo ''
  123.                 echo "Source directory \"$answer\" does not exist!"
  124.                 echo
  125.             else
  126.                 FROMDIR=`fix_dir_name $answer`
  127.             fi
  128.         else
  129.             INTERACTIVE=no
  130.             usage_exit
  131.         fi
  132.     fi
  133.     if [ -z "$TODIR" ]; then
  134.         DEFAULT_TODIR=`basename \`cd $FROMDIR; pwd\``
  135.         echo 'Destination directory?  (<Return> for "'$DEFAULT_TODIR'"): '
  136.         if read answer; then
  137.             if [ -z "$answer" ]; then
  138.                 TODIR=`fix_dir_name $DEFAULT_TODIR`
  139.             else
  140.                 TODIR=`fix_dir_name $answer`
  141.             fi
  142.             if [ "$TODIR" = "$FROMDIR" ]; then
  143.                 echo ''
  144.                 echo "Cannot copy directory onto itself!"
  145.                 echo
  146.                 TODIR=
  147.             fi
  148.         else
  149.             INTERACTIVE=
  150.             usage_exit
  151.         fi
  152.     fi
  153. done
  154.  
  155. if [ ! -d "$FROMDIR" ]; then
  156.     echo ''
  157.     echo "Source directory \"$FROMDIR\" does not exist!" >&2
  158.     exit 4
  159. elif [ "$TODIR" = "$FROMDIR" ]; then
  160.     echo "Cannot copy directory onto itself!" >&2
  161.     exit 5
  162. elif [ ! -d "$TODIR" ]; then
  163.     if [ 'X-S' != "X${MODE-}" ]; then
  164.         echo "Creating destination directory \"$TODIR\"."
  165.     fi
  166.     mkdir -p "$TODIR"
  167. fi
  168.  
  169. if [ 'X-S' != "X${MODE-}" ]; then
  170.     echo "Copying directory \"$FROMDIR\" to \"$TODIR\"."
  171. fi
  172. cd $FROMDIR
  173.  
  174. OPTIONS='-pmdu'
  175. if [ -w "$FROMDIR" ]; then
  176.     ## Reset access times if permissions available
  177.     OPTIONS=${OPTIONS}a
  178. fi
  179.  
  180. if [ -z "${SILENT-}" ]; then
  181.     OPTIONS=${OPTIONS}v
  182. fi
  183.  
  184. exec find . -xdev -depth -print | sed 's,^./,,' | cpio "$OPTIONS" "$TODIR"
  185.  
  186. ## Notreached
  187.