summaryrefslogtreecommitdiff
path: root/scripts/dpkg-name.sh
blob: fc4c2371a2e78b2c5e34041b413db44933d2d0c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/bin/sh

set -e

# Time-stamp: <96/05/03 13:59:41 root>
prog="`basename \"${0}\"`"
version="1.2.3"; # This line modified by Makefile
purpose="rename Debian packages to full package names"

license () {
echo "# ${prog} ${version} -- ${purpose}
# Copyright (C) 1995,1996 Erick Branderhorst <branderh@debian.org>.

# This is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any
# later version.

# This is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the file
# /usr/doc/copyright/GPL for more details."
}

stderr () {
	echo "${prog}: $@" >/dev/stderr;
}

show_version () {
	echo "${prog} version ${version} -- ${purpose}";
}

usage () {
	echo "Usage: ${prog} file[s]
  ${purpose}
  file.deb changes to <package>_<version>_<architecture>.deb 
  according to the ``underscores convention''.
  -a|--no-architecture  No architecture part in filename
  -o|--overwrite        Overwrite if file exists
  -s|--subdir [dir]     Move file into subdir (Use with care)
  -c|--create-dir       Create target dir if not there (Use with care)
  -h|--help|-v|--version|-l|--license  Show help/version/license"
}

fileexists () {
	if [ -f "$1" ];
	then
		return 0;
	else
		stderr "can't find \`"$1"'";
		return 1;
	fi
}

getname () {
	if p=`dpkg-deb -f -- "$1" package`;
	then
		v=`dpkg-deb -f -- "$1" version | sed s,.*:,,`;
		r=`dpkg-deb -f -- "$1" revision`;
		if [ -z "$r" ];
		then
			r=`dpkg-deb -f -- "$1" package_revision`;
		fi

		if [ -n "$r" ];
		then
			v=$v-$r;
		fi

		a=`dpkg-deb -f -- "$1" architecture`;
		a=`echo $a|sed -e 's/ *//g'`;
		if [ -z "$a" -a -n "$noarchitecture" ]; # arch field empty, or ignored
		then
			a=`dpkg --print-architecture`;
			stderr "assuming architecture \`"$a"' for \`"$1"'";
		fi
		if [ -z "$noarchitecture" ];
		then
			tname=$p\_$v\_$a.deb;
		else
			tname=$p\_$v.deb
		fi
	
		name=`echo $tname|sed -e 's/ //g'`
		if [ "$tname" != "$name" ]; # control fields have spaces 
		then
			stderr "bad package control information for \`"$1"'"
		fi
		return 0;
	fi
}

getdir () {
	if [ -z "$destinationdir" ];
	then
		dir=`dirname "$1"`;
		if [ -n "$subdir" ];
		then
			s=`dpkg-deb -f -- "$1" section`;
			if [ -z "$s" ];
			then
				s="no-section";
				stderr "assuming section \`"no-section"' for \`"$1"'";
			fi
			if [ "$s" != "non-free" -a "$s" != "contrib" -a "$s" != "no-section" ];
			then
				dir=`echo unstable/binary-$a/$s`;
			else
				dir=`echo $s/binary-$a`;
			fi
		fi
	else
		dir=$destinationdir;
	fi
}

move () {
	if fileexists "$arg"; 
	then
		getname "$arg";
		getdir "$arg";
		if [ ! -d "$dir" ];
		then
			if [ -n "$createdir" ];
			then
  			if `mkdir -p $dir`;
	  		then
		  		stderr "created directory \`$dir'";
			  else
		  		stderr "failed creating directory \`$dir'";
					exit 1;
				fi
			else
				stderr "no such dir \`$dir'";
				stderr "try --create-dir (-c) option";
				exit 1;
			fi
		fi
		newname=`echo $dir/$name`;
		if [ $newname -ef "$1" ]; # same device and inode numbers
		then
			stderr "skipping \`"$1"'";
		elif [ -f $newname -a -z "$overwrite" ];
		then
			stderr "can't move \`"$1"' to existing file";
		elif `mv -- "$1" $newname`;
		then
			echo "moved \``basename "$1"`' to \`$newname'";
		else
			stderr "mkdir can be used to create directory";
			exit 1;
		fi
  fi
}

if [ $# = 0 ]; then usage; exit 0; fi	
for arg
do
	if [ -n "$subdirset" ];
	then
		subdirset=0;
		subdir=1;
		if [ -d $arg ];
		then
			destinationdir=$arg;
			continue
		fi
	fi
	case "$arg" in
		--version|-v) show_version; exit 0;;
		--help|-[h?]) usage; exit 0;;
		--licen[cs]e|-l) license; exit 0;;
		--create-dir|-c) createdir=1;;
		--subdir|-s) subdirset=1;;
		--overwrite|-o) overwrite=1 ;;
		--no-architecture|-a) noarchitecture=1 ;;
		--) shift; 
			for arg 
			do 
				move "$arg";
			done; exit 0;;
		*) move "$arg";;
	esac
done
exit 0;

# Local variables:
# tab-width: 2
# End: