home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / darwin / darwinx86.iso / usr / bin / dpkg-name < prev    next >
Encoding:
Text File  |  2001-09-18  |  4.0 KB  |  190 lines

  1. #!/bin/sh
  2.  
  3. set -e
  4.  
  5. # Time-stamp: <96/05/03 13:59:41 root>
  6. prog="`basename \"${0}\"`"
  7. version=""; # This line modified by Makefile
  8. purpose="rename Debian packages to full package names"
  9.  
  10. license () {
  11. echo "# ${prog} ${version} -- ${purpose}
  12. # Copyright (C) 1995,1996 Erick Branderhorst <branderh@debian.org>.
  13.  
  14. # This is free software; you can redistribute it and/or modify it
  15. # under the terms of the GNU General Public License as published by the
  16. # Free Software Foundation; either version 2, or (at your option) any
  17. # later version.
  18.  
  19. # This is distributed in the hope that it will be useful, but
  20. # WITHOUT ANY WARRANTY; without even the implied warranty of
  21. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the file
  22. # /usr/doc/copyright/GPL for more details."
  23. }
  24.  
  25. stderr () {
  26.     echo "${prog}: $@" >/dev/stderr;
  27. }
  28.  
  29. show_version () {
  30.     echo "${prog} version ${version} -- ${purpose}";
  31. }
  32.  
  33. usage () {
  34.     echo "Usage: ${prog} file[s]
  35.   ${purpose}
  36.   file.deb changes to <package>_<version>_<architecture>.deb 
  37.   according to the ``underscores convention''.
  38.   -a|--no-architecture  No architecture part in filename
  39.   -o|--overwrite        Overwrite if file exists
  40.   -s|--subdir [dir]     Move file into subdir (Use with care)
  41.   -c|--create-dir       Create target dir if not there (Use with care)
  42.   -h|--help|-v|--version|-l|--license  Show help/version/license"
  43. }
  44.  
  45. fileexists () {
  46.     if [ -f "$1" ];
  47.     then
  48.         return 0;
  49.     else
  50.         stderr "can't find \`"$1"'";
  51.         return 1;
  52.     fi
  53. }
  54.  
  55. getname () {
  56.     if p=`dpkg-deb -f -- "$1" package`;
  57.     then
  58.         v=`dpkg-deb -f -- "$1" version`;
  59.         r=`dpkg-deb -f -- "$1" revision`;
  60.         if [ -z "$r" ];
  61.         then
  62.             r=`dpkg-deb -f -- "$1" package_revision`;
  63.         fi
  64.  
  65.         if [ -n "$r" ];
  66.         then
  67.             v=$v-$r;
  68.         fi
  69.  
  70.         a=`dpkg-deb -f -- "$1" architecture`;
  71.         a=`echo $a|sed -e 's/ *//g'`;
  72.         if [ -z "$a" -a -n "$noarchitecture" ]; # arch field empty, or ignored
  73.         then
  74.             a=`dpkg --print-architecture`;
  75.             stderr "assuming architecture \`"$a"' for \`"$1"'";
  76.         fi
  77.         if [ -z "$noarchitecture" ];
  78.         then
  79.             tname=$p\_$v\_$a.deb;
  80.         else
  81.             tname=$p\_$v.deb
  82.         fi
  83.     
  84.         name=`echo $tname|sed -e 's/ //g'`
  85.         if [ "$tname" != "$name" ]; # control fields have spaces 
  86.         then
  87.             stderr "bad package control information for \`"$1"'"
  88.         fi
  89.         return 0;
  90.     fi
  91. }
  92.  
  93. getdir () {
  94.     if [ -z "$destinationdir" ];
  95.     then
  96.         dir=`dirname "$1"`;
  97.         if [ -n "$subdir" ];
  98.         then
  99.             s=`dpkg-deb -f -- "$1" section`;
  100.             if [ -z "$s" ];
  101.             then
  102.                 s="no-section";
  103.                 stderr "assuming section \`"no-section"' for \`"$1"'";
  104.             fi
  105.             if [ "$s" != "non-free" -a "$s" != "contrib" -a "$s" != "no-section" ];
  106.             then
  107.                 dir=`echo unstable/binary-$a/$s`;
  108.             else
  109.                 dir=`echo $s/binary-$a`;
  110.             fi
  111.         fi
  112.     else
  113.         dir=$destinationdir;
  114.     fi
  115. }
  116.  
  117. move () {
  118.     if fileexists "$arg"; 
  119.     then
  120.         getname "$arg";
  121.         getdir "$arg";
  122.         if [ ! -d "$dir" ];
  123.         then
  124.             if [ -n "$createdir" ];
  125.             then
  126.               if `mkdir -p $dir`;
  127.               then
  128.                   stderr "created directory \`$dir'";
  129.               else
  130.                   stderr "failed creating directory \`$dir'";
  131.                     exit 1;
  132.                 fi
  133.             else
  134.                 stderr "no such dir \`$dir'";
  135.                 stderr "try --create-dir (-c) option";
  136.                 exit 1;
  137.             fi
  138.         fi
  139.         newname=`echo $dir/$name`;
  140.         if [ $newname -ef "$1" ]; # same device and inode numbers
  141.         then
  142.             stderr "skipping \`"$1"'";
  143.         elif [ -f $newname -a -z "$overwrite" ];
  144.         then
  145.             stderr "can't move \`"$1"' to existing file";
  146.         elif `mv -- "$1" $newname`;
  147.         then
  148.             echo "moved \``basename "$1"`' to \`$newname'";
  149.         else
  150.             stderr "mkdir can be used to create directory";
  151.             exit 1;
  152.         fi
  153.   fi
  154. }
  155.  
  156. if [ $# = 0 ]; then usage; exit 0; fi    
  157. for arg
  158. do
  159.     if [ -n "$subdirset" ];
  160.     then
  161.         subdirset=0;
  162.         subdir=1;
  163.         if [ -d $arg ];
  164.         then
  165.             destinationdir=$arg;
  166.             continue
  167.         fi
  168.     fi
  169.     case "$arg" in
  170.         --version|-v) show_version; exit 0;;
  171.         --help|-[h?]) usage; exit 0;;
  172.         --licen[cs]e|-l) license; exit 0;;
  173.         --create-dir|-c) createdir=1;;
  174.         --subdir|-s) subdirset=1;;
  175.         --overwrite|-o) overwrite=1 ;;
  176.         --no-architecture|-a) noarchitecture=1 ;;
  177.         --) shift; 
  178.             for arg 
  179.             do 
  180.                 move "$arg";
  181.             done; exit 0;;
  182.         *) move "$arg";;
  183.     esac
  184. done
  185. exit 0;
  186.  
  187. # Local variables:
  188. # tab-width: 2
  189. # End:
  190.