setup 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #!/usr/bin/perl -w
  2. # -*- Mode: Perl -*-
  3. # setup.pl ---
  4. # Author : Manoj Srivastava ( srivasta@tiamat.datasync.com )
  5. # Created On : Wed Mar 4 15:11:47 1998
  6. # Created On Node : tiamat.datasync.com
  7. # Last Modified By : Manoj Srivastava
  8. # Last Modified On : Tue May 19 11:25:32 1998
  9. # Last Machine Used: tiamat.datasync.com
  10. # Update Count : 87
  11. # Status : Unknown, Use with caution!
  12. # HISTORY :
  13. # Description :
  14. # This file is designed to go into /usr/lib/apt/methods/setup
  15. #
  16. #use strict;
  17. #use diagnostics;
  18. #printf STDERR "DEBUG: Arguments $ARGV[0];$ARGV[1];$ARGV[2];\n";
  19. # Handle the arguments
  20. my $vardir=$ARGV[0];
  21. my $method=$ARGV[1];
  22. my $option=$ARGV[2];
  23. my $config_file = '/etc/apt/sources.list';
  24. my $boldon=`setterm -bold on`;
  25. my $boldoff=`setterm -bold off`;
  26. my @known_types = ('deb');
  27. my @known_access = ('http', 'ftp', 'file');
  28. my @typical_distributions = ('stable', 'unstable', 'testing', 'non-US');
  29. my @typical_components = ('main', 'contrib', 'non-free');
  30. my %known_access = map {($_,$_)} @known_access;
  31. my %typical_distributions = map {($_,$_)} @typical_distributions;
  32. # Read the config file, creating source records
  33. sub read_config {
  34. my %params = @_;
  35. my @Config = ();
  36. die "Required parameter Filename Missing" unless
  37. $params{'Filename'};
  38. open (CONFIG, "$params{'Filename'}") ||
  39. die "Could not open $params{'Filename'}: $!";
  40. while (<CONFIG>) {
  41. chomp;
  42. my $rec = {};
  43. my ($type, $urn, $distribution, $components) =
  44. m/^\s*(\S+)\s+(\S+)\s+(\S+)\s*(?:\s+(\S.*))?$/o;
  45. $rec->{'Type'} = $type;
  46. $rec->{'URN'} = $urn;
  47. $rec->{'Distribution'} = $distribution;
  48. $rec->{'Components'} = $components;
  49. push @Config, $rec;
  50. }
  51. close(CONFIG);
  52. return @Config;
  53. }
  54. # write the config file; writing out the current set of source records
  55. sub write_config {
  56. my %params = @_;
  57. my $rec;
  58. my %Seen = ();
  59. die "Required parameter Filename Missing" unless
  60. $params{'Filename'};
  61. die "Required parameter Config Missing" unless
  62. $params{'Config'};
  63. open (CONFIG, ">$params{'Filename'}") ||
  64. die "Could not open $params{'Filename'} for writing: $!";
  65. for $rec (@{$params{'Config'}}) {
  66. my $line = "$rec->{'Type'} $rec->{'URN'} $rec->{'Distribution'} ";
  67. $line .= "$rec->{'Components'}" if $rec->{'Components'};
  68. $line .= "\n";
  69. print CONFIG $line unless $Seen{$line}++;
  70. }
  71. close(CONFIG);
  72. }
  73. # write the config file; writing out the current set of source records
  74. sub print_config {
  75. my %params = @_;
  76. my $rec;
  77. my %Seen = ();
  78. die "Required parameter Config Missing" unless
  79. $params{'Config'};
  80. for $rec (@{$params{'Config'}}) {
  81. next unless $rec;
  82. my $line = "$rec->{'Type'} " if $rec->{'Type'};
  83. $line .= "$rec->{'URN'} " if $rec->{'URN'};
  84. $line .= "$rec->{'Distribution'} " if $rec->{'Distribution'};
  85. $line .= "$rec->{'Components'}" if $rec->{'Components'};
  86. $line .= "\n";
  87. print $line unless $Seen{$line}++;
  88. }
  89. }
  90. # Ask for and add a source record
  91. sub get_source {
  92. my %params = @_;
  93. my $rec = {};
  94. my $answer;
  95. my ($type, $urn, $distribution, $components);
  96. if ($params{'Default'}) {
  97. ($type, $urn, $distribution, $components) =
  98. $params{'Default'} =~ m/^\s*(\S+)\s+(\S+)\s+(\S+)\s+(\S.*)$/o;
  99. }
  100. $type = 'deb';
  101. $urn = "http://http.us.debian.org/debian" unless $urn;
  102. $distribution = "stable" unless $distribution;
  103. $components = "main contrib non-free" unless $components;
  104. $rec->{'Type'} = 'deb';
  105. $| = 1;
  106. my $done = 0;
  107. while (!$done) {
  108. print "\n";
  109. print "$boldon URL [$urn]: $boldoff";
  110. $answer=<STDIN>;
  111. chomp ($answer);
  112. $answer =~ s/\s*//og;
  113. if ($answer =~ /^\s*$/o) {
  114. $rec->{'URN'} = $urn;
  115. last;
  116. }
  117. else {
  118. my ($scheme) = $answer =~ /^\s*([^:]+):/o;
  119. if (! defined $known_access{$scheme}) {
  120. print "Unknown access scheme $scheme in $answer\n";
  121. print " The available access methods known to me are\n";
  122. print join (' ', @known_access), "\n";
  123. print "\n";
  124. }
  125. else {
  126. $rec->{'URN'} = $answer;
  127. last;
  128. }
  129. }
  130. }
  131. print "\n";
  132. print " Please give the distribution tag to get or a path to the\n";
  133. print " package file ending in a /. The distribution\n";
  134. print " tags are typically something like:$boldon ";
  135. print join(' ', @typical_distributions), "$boldoff\n";
  136. print "\n";
  137. print "$boldon Distribution [$distribution]:$boldoff ";
  138. $answer=<STDIN>;
  139. chomp ($answer);
  140. $answer =~ s/\s*//og;
  141. if ($answer =~ /^\s*$/o) {
  142. $rec->{'Distribution'} = $distribution;
  143. $rec->{'Components'} = &get_components($components);
  144. }
  145. elsif ($answer =~ m|/$|o) {
  146. $rec->{'Distribution'} = "$answer";
  147. $rec->{'Components'} = "";
  148. }
  149. else {
  150. # A distribution tag, eh?
  151. warn "$answer does not seem to be a typical distribution tag\n"
  152. unless defined $typical_distributions{$answer};
  153. $rec->{'Distribution'} = "$answer";
  154. $rec->{'Components'} = &get_components($components);
  155. }
  156. return $rec;
  157. }
  158. sub get_components {
  159. my $default = shift;
  160. my $answer;
  161. print "\n";
  162. print " Please give the components to get\n";
  163. print " The components are typically something like:$boldon ";
  164. print join(' ', @typical_components), "$boldoff\n";
  165. print "\n";
  166. print "$boldon Components [$default]:$boldoff ";
  167. $answer=<STDIN>;
  168. chomp ($answer);
  169. $answer =~ s/\s+/ /og;
  170. if ($answer =~ /^\s*$/o) {
  171. return $default;
  172. }
  173. else {
  174. return $answer;
  175. }
  176. }
  177. sub get_sources {
  178. my @Config = ();
  179. my $done = 0;
  180. my @Oldconfig = ();
  181. if (-e $config_file) {
  182. @Oldconfig = &read_config('Filename' => $config_file)
  183. }
  184. print "\t$boldon Set up a list of distribution source locations $boldoff \n";
  185. print "\n";
  186. print " Please give the base URL of the debian distribution.\n";
  187. print " The access schemes I know about are:$boldon ";
  188. print join (' ', @known_access), "$boldoff\n";
  189. # print " The mirror scheme is special that it does not specify the\n";
  190. # print " location of a debian archive but specifies the location\n";
  191. # print " of a list of mirrors to use to access the archive.\n";
  192. print "\n";
  193. print " For example:\n";
  194. print " file:/mnt/debian,\n";
  195. print " ftp://ftp.debian.org/debian,\n";
  196. print " http://ftp.de.debian.org/debian,\n";
  197. # print " and the special mirror scheme,\n";
  198. # print " mirror:http://www.debian.org/archivemirrors \n";
  199. print "\n";
  200. my $index = 0;
  201. while (!$done) {
  202. if ($Oldconfig[$index]) {
  203. push (@Config, &get_source('Default' => $Oldconfig[$index++]));
  204. }
  205. else {
  206. push (@Config, &get_source());
  207. }
  208. print "\n";
  209. print "$boldon Would you like to add another source?[y/N]$boldoff ";
  210. my $answer = <STDIN>;
  211. chomp ($answer);
  212. $answer =~ s/\s+/ /og;
  213. if ($answer =~ /^\s*$/o) {
  214. last;
  215. }
  216. elsif ($answer !~ m/\s*y/io) {
  217. last;
  218. }
  219. }
  220. return @Config;
  221. }
  222. sub main {
  223. if (-e $config_file) {
  224. my @Oldconfig = &read_config('Filename' => $config_file);
  225. print "$boldon I see you already have a source list.$boldoff\n";
  226. print "-" x 72, "\n";
  227. &print_config('Config' => \@Oldconfig);
  228. print "-" x 72, "\n";
  229. print "$boldon Do you wish to overwrite it? [y/N]$boldoff ";
  230. my $answer = <STDIN>;
  231. chomp ($answer);
  232. $answer =~ s/\s+/ /og;
  233. exit 0 unless $answer =~ m/\s*y/io;
  234. }
  235. # OK. They want to be here.
  236. my @Config = &get_sources();
  237. #&print_config('Config' => \@Config);
  238. &write_config('Config' => \@Config, 'Filename' => $config_file);
  239. }
  240. &main();