summaryrefslogtreecommitdiffstats
path: root/dselect/methods/media/install.sh
blob: 625ca22e64dfd7ecf3e322dd5a13a15949e57b5d (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/bin/sh
#
# Copyright © 1995-1998 Ian Jackson <ijackson@chiark.greenend.org.uk>
# Copyright © 1998 Heiko Schlittermann <hs@schlittermann.de>
#
# 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 of the License, or
# (at your option) any later version.
#
# This program 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
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

set -e
vardir="$1"
method=$2
option=$3

cd "$vardir/methods/$method"

. ./shvar.$option

#debug() { echo "DEBUG: $@"; }
debug() {
  true
}
iarch=$(dpkg --print-architecture)
ismulti() {
  test -e "$1/.disk/info" || test -e "$1$2/.disk/info"
}

# 1/ mountpoint
# 2/ hierarchy
getdisklabel () {
  debug "$1" "$2"
  if [ -f $1/.disk/info ]; then
   echo -n $(head -1 "$1/.disk/info")
  elif [ -f $1$2/.disk/info ]; then
    echo -n $(head -1 "$1$2/.disk/info")
  else
    echo -n 'Non-Debian disc'
  fi
}

xit=1

do_umount() {
  if [ -n "$umount" ]; then
    echo umount "$umount"
    #">/dev/null" "2>&1"
  fi
}

do_mount() {
  if [ ! -b "$p_blockdev" ]; then
    loop=",loop"
  fi

  if [ -n "$p_blockdev" ]; then
    umount="$p_mountpoint"
    echo mount -rt iso9660 -o nosuid,nodev${loop} "$p_blockdev" "$p_mountpoint"\; umount="$p_mountpoint"
  fi
}

trap 'eval $(do_umount); exit $xit' 0

eval $(do_mount)

predep="$vardir/predep-package"
while true; do
  thisdisk="$(getdisklabel ${p_mountpoint} ${p_hierbase})"
  set +e
  dpkg --predep-package >"$predep"
  rc=$?
  set -e
  if test $rc = 1; then
    break
  fi
  test $rc = 0

  perl -e '
	($binaryprefix,$predep,$thisdisk) = @ARGV;
	open(P, "< $predep") or die "cannot open $predep: $!\n";
	while (<P>) {
		s/\s*\n$//;
		$package = $_ if s/^Package: //i;
		/^X-Medium:\s+(.*)\s*/ and $medium = $1;
		@filename = split(/ /,$_) if s/^Filename: //i;
		@msdosfilename = split(/ /,$_) if s/^MSDOS-Filename: //i;
	}
	die "internal error - no package" if length($package) == 0;
	die "internal error - no filename" if not @filename;
	die "internal error - mismatch >@filename< >@msdosfilename<"
		if @filename && @msdosfilename &&
		   @filename != @msdosfilename;
	if ($medium && ($medium ne $thisdisk)) {
		print "

This is
    $thisdisk
However, $package is expected on disc:
    $medium
Please change the discs and press <RETURN>.

";
		exit(1);
	}
	@invoke = (); $| =1;
	for ($i = 0; $i <= $#filename; $i++) {
		$ppart = $i+1;
		print "Looking for part $ppart of $package ... ";
		if (-f "$binaryprefix$filename[$i]") {
			$print = $filename[$i];
			$invoke = "$binaryprefix$filename[$i]";
		} elsif (-f "$binaryprefix$msdosfilename[$i]") {
			$print = $msdosfilename[$i];
			$invoke = "$binaryprefix$msdosfilename[$i]";
		} else {
			$base = $filename[$i]; $base =~ s,.*/,,;
			$msdosbase = $msdosfilename[$i]; $msdosbase =~ s,.*/,,;
			$c = open(X, "-|"));
			if (not defined $c) {
				die "failed to fork for find: $!\n";
			}
			if (!$c) {
				exec("find", "-L",
				     length($binaryprefix) ? $binaryprefix : ".",
				     "-name",$base,"-o","-name",$msdosbase);
				die "failed to exec find: $!\n";
			}
			while (chop($invoke = <X>)) { last if -f $invoke; }
			$print = $invoke;
			if (substr($print,0,length($binaryprefix)+1) eq
			    "$binaryprefix/") {
				$print = substr($print,length($binaryprefix));
			}
		}
		if (!length($invoke)) {
			warn "

Cannot find the appropriate file(s) anywhere needed to install or upgrade
package $package. Expecting version $version or later, as listed in the
Packages file.

Perhaps the package was downloaded with an unexpected name? In any case,
you must find the file(s) and then either place it with the correct
filename(s) (as listed in the Packages file or in $vardir/available)
and rerun the installation, or upgrade the package by using
\"dpkg --install --auto-deconfigure" by hand.

";
			exit(1);
		}
		print "$print\n";
		push(@invoke,$invoke);
	}
	print "Running dpkg -iB for $package ...\n";
	exec("dpkg","-iB","--",@invoke);
	die "failed to exec dpkg: $!\n";
  ' -- "$p_mountpoint$p_hierbase" "$predep" "$thisdisk"
done

perl -e '
	$SIG{INT} = sub { cd $vardir; unlink <tmp/*>; exit 1; };
	$| = 1;
	my ($vardir, $mountpoint, $hierbase, $mount, $umount) = @ARGV;
	my $line;
	my $AVAIL = "$vardir/methods/media/available";
	my $STATUS = "$vardir/status";
	my %Installed, %Filename, %Medium;
	print "Get currently installed package versions...";
	open(IN, "$STATUS") or die "cannot open $STATUS: $!\n";
	$line = 0;
	{ local $/ = "";
	while (<IN>) {
		my %status;
		my @pstat;
		$line++ % 20 or print ".";
		s/\n\s+/ /g;
		%status =  ("", split /^(\S*?):\s*/m, $_);
		map { chomp $status{$_}; $status{$_} =~ s/^\s*(.*?)\s*$/$1/;} keys %status;
		@pstat = split(/ /, $status{Status});
		next unless ($pstat[0] eq "install");
		if ($pstat[2] eq "config-files" || $pstat[2] eq "not-installed") {
		    $Installed{$status{Package}} = "0.0";
		} else {
		    $Installed{$status{Package}} = $status{Version} || "" ;
		}
	}; }
	print "\nGot ", scalar keys %Installed, " installed/pending packages\n";
	print "Scanning available packages...";
	$line = 0;
	open(IN, "$AVAIL") or die("Cannot open $AVAIL: $!\n");
	{ local $/ = "";
	while (<IN>) {
		my $updated;
		 $line++ % 20 or print ".";

		 s/\n\s+/ /g;
		 %avail   =  ("", split /^(\S*?):\s*/m, $_);
		 map { chomp $avail{$_}; $avail{$_} =~ s/^\s*(.*?)\s*$/$1/;} keys %avail;

		 next unless defined $Installed{$avail{Package}};

		 system "dpkg", "--compare-versions", $avail{Version}, "gt", $Installed{$avail{Package}};
		 $updated = ($? == 0);
		 #print "$avail{Package}(" . ($updated ? "+" : "=") . ") ";
		 $updated or next;

		 $Filename{$avail{Package}} = $avail{Filename};

		 next unless defined $avail{"X-Medium"};
		 ${Medium{$avail{"X-Medium"}}} or ${Medium{$avail{"X-Medium"}}} = [];
		 push @{${Medium{$avail{"X-Medium"}}}}, $avail{Package};
	}; };
	print "\n";

	if (@_ = keys(%Medium)) {
		    print "You will need the following distribution disc(s):\n",
			join (", ", @_), "\n";
	}

	foreach $need (sort @_) {
		if (-r "$mountpoint/.disk/info") {
			open(IN, "$mountpoint/.disk/info") or die("$mountpoint/.disk/info: $!\n");
		} else {
			open(IN, "$mountpoint/$hierbase/.disk/info") or die("$mountpoint/$hierbase/.disk/info: $!\n");
		}
		$disk = <IN>; chomp $disk; close(IN);

		print "Processing disc\n   $need\n";

		while ($disk ne $need) {
			print "Wrong disc.  This is disc\n    $disk\n";
			print "However, the needed disc is\n    $need\n";
			print "Please change the discs and press <RETURN>\n";
			system($umount);
			<STDIN>;
			system($mount);
			$? and warn("cannot mount $mount\n");
		} continue {
			if (-r "$mountpoint/.disk/info") {
			    open(IN, "$mountpoint/.disk/info") or die("$mountpoint/.disk/info: $!\n");
			} else {
			    open(IN, "$mountpoint/$hierbase/.disk/info") or die("$mountpoint/$hierbase/.disk/info: $!\n");
			}
			$disk = <IN>; chomp $disk; close(IN);
		}

		-d "tmp" || mkdir "tmp", 0755 or die("Cannot mkdir tmp: $!\n");
		unlink <tmp/*>;

		print "creating symlinks...\n";
		foreach (@{$Medium{$need}}) {
			($basename = $Filename{$_}) =~ s/.*\///;
			symlink "$mountpoint/$hierbase/$Filename{$_}",
				"tmp/$basename";
		}
		chdir "tmp" or die "cannot chdir to tmp: $!\n";
		system "dpkg", "-iGROEB", ".";
		unlink <*>;
		chdir "..";

		if ($?) {
			print "\nThe dpkg run produced errors. Please state whether to\n",
			      "continue with the next media disc. [Y/n]: ";
			$answer = <STDIN>;
			exit 1 if $answer =~ /^n/i;
			$ouch = $?;
		}
	}

	exit $ouch;

' "$vardir" "$p_mountpoint" "$p_hierbase" "$(do_mount)" "$(do_umount)"

echo -n 'Installation OK.  Hit RETURN.  '
read response

xit=0