home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / ade-misc-src.tgz / ade-misc-src.tar / scripts / packfiles.sh < prev    next >
Linux/UNIX/POSIX Shell Script  |  1996-10-13  |  7KB  |  263 lines

  1. #! /bin/sh
  2. #
  3. # Create archives in a destination directory from files in an input tree
  4. # using manifest files listed on stdin.
  5. #
  6. # Takes the following arguments:
  7. #
  8. #    --to=<dir>    The directory in which to create the archives,
  9. #            typically something like "ADE-dist:951208".
  10. #
  11. #    --from=<dir>    The root of the tree containing the files named in
  12. #            the manifest files whose names are read from stdin.
  13. #            Typically something like "ADE-src" or "ADE-bin".
  14. #
  15. #    --tmp=<dir>    A temporary directory in which to create temporary
  16. #            subdirs used in creating the archives.
  17. #
  18. #    --tgz        Build gzip compresed tar archives rather than
  19. #            lha archives.  This is a more portable format
  20. #            and is used for things like source archives, which
  21. #            are potentially useful on non-Amiga systems.
  22. #
  23. # Builds the output file name from components of the input manifest file
  24. # pathname and the version number read from an associated Product-Info file.
  25. #
  26. # The associated product info file for each archive is found either by
  27. # looking for an appropriate *-pi file in the manifests subdir, or from the
  28. # Product-Info in the same directory as the manifests subdir.
  29. #
  30. # For example, if the manifest file is "fsf/cpio/manifests/bin", then the
  31. # archive filename will be something like "cpio-X.Y-bin.lha" where X.Y is
  32. # read from the Product-Info file in "fsf/cpio".
  33. #
  34. # As another example, if the manifest file is "manifests/GNU-Startup-bin"
  35. # then the archive filename will be "GNU-Startup-bin.lha".
  36.  
  37. # Process the options and remember them for future use.
  38.  
  39. artype="lha"
  40. to=
  41. from=
  42. tmp=
  43. prev=
  44. for option
  45. do
  46.  
  47.     # If the previous option needs an argument, assign it.
  48.     if test -n "$prev"; then
  49.         eval "$prev=\$option"
  50.         prev=
  51.         continue
  52.     fi
  53.  
  54.     case "$option" in
  55.         -*=*) optarg=`/bin/echo "$option" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
  56.         *) optarg= ;;
  57.     esac
  58.  
  59.     case "$option" in
  60.         --to)        prev=to ;;
  61.         --to=*)        to="$optarg" ;;
  62.         --from)        prev=from ;;
  63.         --from=*)    from="$optarg" ;;
  64.         --tmp)        prev=tmp ;;
  65.         --tmp=*)    tmp="$optarg" ;;
  66.         --tgz)        artype="tgz" ;;
  67.         -*) { echo "error: $option: invalid option" 1>&2; exit 1; } ;;
  68.     esac
  69. done
  70.  
  71. # Verify that we have all the options we expect to have.
  72.  
  73. if test -z "$to"; then
  74.     echo "error: no --to=<dir> argument given" 1>&2
  75.     exit 1
  76. fi
  77.  
  78. if test -z "$from"; then
  79.     echo "error: no --from=<dir> argument given" 1>&2
  80.     exit 1
  81. fi
  82.  
  83. if test -z "$tmp"; then
  84.     echo "error: no --tmp=<dir> argument given" 1>&2
  85.     exit 1
  86. fi
  87.  
  88. # Check to see that the output directory exists.  If not, create it.
  89.  
  90. if test ! -d "$to"
  91. then
  92.     echo "mkdir -p $to"
  93.     mkdir -p $to
  94. fi
  95.  
  96. # Check to see that the temporary directory exists.  If not, create it.
  97.  
  98. WORKDIR="$tmp/GNUpack"
  99. if test ! -d "$WORKDIR"
  100. then
  101.     echo "mkdir -p $WORKDIR"
  102.     mkdir -p $WORKDIR
  103. fi
  104.  
  105. # Read and process each manifest file from stdin.
  106.  
  107. while read manifest
  108. do
  109.     echo "Processing $manifest ..."
  110.  
  111.     # Locate the product info files (if any) that corresponds
  112.     # to this manifest file.  There may be one that is the same
  113.     # name as the manifest file but with "-pi" appended, and there
  114.     # may be one in the parent directory of the location where the
  115.     # manifest file is found.  If there are two, they must agree
  116.     # on the version number.  If neither exist, there is no version
  117.     # number (which is OK).
  118.  
  119.     vers1=""
  120.     vers2=""
  121.     pifilein=""
  122.     mdot=`dirname $manifest`
  123.     mdotdot=`dirname $mdot`
  124.     product=`basename $mdotdot`
  125.     if test -f "$mdotdot/Product-Info"
  126.     then
  127.         pifilein=$mdotdot/Product-Info
  128.         product=`pitool -b -u -F "%N" -f - $pifilein`
  129.         vers1=`pitool -b -u -F "%V" -f - $pifilein`
  130.     fi
  131.     if test -f "$manifest-pi"
  132.     then
  133.         pifilein=$manifest-pi
  134.         product=`pitool -b -u -F "%N" -f - $pifilein`
  135.         vers2=`pitool -b -u -F "%V" -f - $pifilein`
  136.     fi
  137.     if test -n "$vers1" -a -n "$vers2"
  138.     then
  139.         if test "$vers1" != "$vers2"
  140.         then
  141.             echo "WARNING - conflicting version numbers $vers1 and $vers2" 2>&1
  142.             vers1=""
  143.         fi
  144.     fi
  145.     if test "$vers2" = "?.?"
  146.     then
  147.         version=""
  148.     elif test "$vers1" = "?.?"
  149.     then
  150.         version=""
  151.     elif test -n "$vers2"
  152.     then
  153.         version=$vers2
  154.     elif test -n "$vers1"
  155.     then
  156.         version=$vers1
  157.     else
  158.         version=""
  159.     fi
  160.  
  161.     # Now create the name of the archive.
  162.     #
  163.     # If the manifest file is from our special global manifest file
  164.     # directory, then the name of the archive is just the name of the
  165.     # manifest file.
  166.     #
  167.     # Otherwise we start with the product name, tack on a version
  168.     # number if one exists, and then tack on the name of the manifest
  169.     # file as a qualifier.
  170.     #
  171.     # Finally we tack on the archive type.
  172.  
  173.     if test "$mdot" = "manifests"
  174.     then
  175.         archive="`basename $manifest`"
  176.     else
  177.         archive="$product"
  178.         if test -n "$version"
  179.         then
  180.             archive="$archive-$version"
  181.         fi
  182.         archive="$archive-`basename $manifest`"
  183.     fi    
  184.     archive="$archive.$artype"
  185.  
  186.     # Verify that we have an input Product-Info file
  187.  
  188.     pifileout="$to/$archive.pi"
  189.     if test -z "$pifilein"
  190.     then
  191.         echo "WARNING: no product info file found to generate $pifileout - skipped"
  192.         continue
  193.     fi
  194.  
  195.     # Check to see if this archive already exists.  If so, skip
  196.     # creating one.  This allows us to restart a failed build of
  197.     # an complete archive directory.  The tradeoff is that when
  198.     # we want to replace an archive, we have to first delete it
  199.     # manually.
  200.  
  201.     if test -f $to/$archive
  202.     then
  203.         #echo "$to/$archive exists, not superceded"
  204.         if test -f $pifileout
  205.         then
  206.             true
  207.             #echo "$pifileout exists, not superceded"
  208.         else
  209.             # Install a copy of the product info file alongside the archive.
  210.             echo "    cp $pifilein $pifileout"
  211.             cp $pifilein $pifileout
  212.         fi
  213.         continue
  214.     fi
  215.  
  216.     # Remove any old trash and create a fresh working directory.
  217.  
  218.     if test -d $WORKDIR
  219.     then
  220.         sleep 2
  221.         echo "    rm -rf $WORKDIR/*"
  222.         rm -rf $WORKDIR/*
  223.     fi
  224.  
  225.     absmanifest=`pwd`/$manifest
  226.  
  227.     # Make a tar archive in $WORKDIR of all the files in the manifest.
  228.     # The files listed in the manifests files are relative to the
  229.     # directory specified in the --from arg.
  230.  
  231.     if test "$artype" = "tgz"
  232.     then
  233.         echo "    (cd $from && tar -cp -T $absmanifest -f - | gzip -9 >$WORKDIR/tar.out.gz)"
  234.         (cd $from && tar -cp -T $absmanifest -f - | gzip -9 >$WORKDIR/tar.out.gz)
  235.         # If what we want is a compressed tar archive, just copy to the destination.
  236.         echo "    (cd $WORKDIR && cp tar.out.gz $to/$archive)"
  237.         (cd $WORKDIR && cp tar.out.gz $to/$archive)
  238.     else
  239.         # If what we want is an lha archive, then we have to go through
  240.         # more contortions, including working around a couple bugs in lha
  241.         # where lha fails to preserve the case of directory names and also
  242.         # fails to archive files if more than a single arg is given to it.
  243.         echo "    (cd $from && tar -cp -T $absmanifest -f $WORKDIR/tar.out)"
  244.         (cd $from && tar -cp -T $absmanifest -f $WORKDIR/tar.out)
  245.         echo "    (cd $WORKDIR/ && tar -xpf tar.out && rm tar.out)"
  246.         (cd $WORKDIR/ && tar -xpf tar.out && rm tar.out)
  247.         echo "    (cd $WORKDIR && lha -mraxeq a $archive * && cp $archive $to/$archive)"
  248.         here=`pwd`
  249.         cd $WORKDIR
  250.         for file in *
  251.         do
  252.             lha -mraxeq a $archive $file
  253.         done
  254.         cp $archive $to/$archive
  255.         cd $here
  256.     fi
  257.     
  258.     # Install a copy of the product info file alongside the archive.
  259.  
  260.     echo "    cp $pifilein $pifileout"
  261.     cp $pifilein $pifileout
  262. done
  263.