dpkg (1.3.1) experimental; urgency=LOW
[dpkg.git] / scripts / dpkg-name.sh
blob545789659a9f25e137aeb97e375add353f53446c
1 #!/bin/sh
3 set -e
5 # Time-stamp: <96/05/03 13:59:41 root>
6 prog="`basename \"${0}\"`"
7 version="1.2.3"; # This line modified by Makefile
8 purpose="rename Debian packages to full package names"
10 license () {
11 echo "# ${prog} ${version} -- ${purpose}
12 # Copyright (C) 1995,1996 Erick Branderhorst <branderh@debian.org>.
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.
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."
25 stderr () {
26 echo "${prog}: $@" >/dev/stderr;
29 show_version () {
30 echo "${prog} version ${version} -- ${purpose}";
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"
45 fileexists () {
46 if [ -f "$1" ];
47 then
48 return 0;
49 else
50 stderr "can't find \`"$1"'";
51 return 1;
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`;
65 if [ -n "$r" ];
66 then
67 v=$v-$r;
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"'";
77 if [ -z "$noarchitecture" ];
78 then
79 tname=$p\_$v\_$a.deb;
80 else
81 tname=$p\_$v.deb
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"'"
89 return 0;
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"'";
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`;
112 else
113 dir=$destinationdir;
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;
133 else
134 stderr "no such dir \`$dir'";
135 stderr "try --create-dir (-c) option";
136 exit 1;
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;
156 if [ $# = 0 ]; then usage; exit 0; fi
157 for arg
159 if [ -n "$subdirset" ];
160 then
161 subdirset=0;
162 subdir=1;
163 if [ -d $arg ];
164 then
165 destinationdir=$arg;
166 continue
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
180 move "$arg";
181 done; exit 0;;
182 *) move "$arg";;
183 esac
184 done
185 exit 0;
187 # Local variables:
188 # tab-width: 2
189 # End: