Imported Upstream version 2.5.1
[debian/amanda] / contrib / set_prod_link.pl
1 #!/usr/local/bin/perl
2 # ========================================================================
3 # @(#) $Id: set_prod_link.pl,v 1.3 2006/05/25 01:47:13 johnfranks Exp $
4 # ------------------------------------------------------------------------
5 # $Source: /cvsroot/amanda/amanda/contrib/set_prod_link.pl,v $
6 # ------------------------------------------------------------------------
7 # Description:
8 #   
9 #   When installing AMANDA with the option --with-suffix you can use this
10 #   script to set the a symbolic link from the productive name of the
11 #   files to this special version.
12 #   This way you can switch on the fly, from one version to an other.
13 #
14 #   Actually I would advice to use the option --prefix and install the
15 #   the whole software in different paths.
16 #
17 #   But if you want for example install a new version in the same
18 #   directory you can use it.
19 #
20 # -----------------------------------------------------------------------
21 # Author: Ricardo Malta, rmalta@bigfoot.com
22 # -----------------------------------------------------------------------
23 # History:
24 #
25 # $Log: set_prod_link.pl,v $
26 # Revision 1.3  2006/05/25 01:47:13  johnfranks
27 # Allow spaces and arbitrary binary characters in file names
28 # and configuration files.
29 #
30 # 64-bit / type portability clean code.
31 #
32 # Add 'make lint' options to appropriate Makefiles.
33 #
34 # Fully lint clean code using Sun's lint, and splint code checkers.
35 #
36 # Various bug fixes that have not been pushed.
37 #
38 # Modified Files:
39 #       Modified most of the files...
40 #
41 # Revision 1.2  1999/11/02 21:30:10  oliva
42 # * contrib/set_prod_link.pl: Create the links for a configuration
43 # with --with-suffix.
44 #
45 #
46 # ========================================================================
47
48 $debug = 0;
49
50 if ($ARGV[0] ne "doit") {
51         print <<"EOD";
52 usage: $0 doit
53
54         Go to the directory where you have compiled AMANDA.
55         Call this programm with the parameter \"doit\".
56
57 EOD
58         exit 1;
59 }
60
61 # ------------------------------------------------------------------------
62 # Open the Makefile and search for the entries we need for doing the job.
63 # ------------------------------------------------------------------------
64 open(FD,"<Makefile") || die "Cannot open Makefile: $!\n";
65 while (<FD>) {
66         $suffix  = (split(/\s*,\s*/))[2] if /^\s*transform\s*=/;
67         $rootdir = (split(/=\s*/))[1] if /^\s*prefix\s*=/;
68         last if $suffix && $suffix;
69 }
70 close(FD);
71 chomp $rootdir;
72 die "Cannot find line containing \"transform =\" in Makefile.\n" if (!$suffix);
73 die "Cannot find line containing \"prefix =\" in Makefile.\n" if (!$rootdir);
74
75 # ------------------------------------------------------------------------
76 # Last chance ....
77 # ------------------------------------------------------------------------
78 print "Starting setting the links to productive version:
79     Directory: $rootdir
80     Suffix   : $suffix
81 Confirm with <yes>: ";
82 chomp($dummy = <STDIN>);
83 die "\nAborting ...\n" if ($dummy ne "yes");
84 print "\n";
85
86 # ------------------------------------------------------------------------
87 # Now do the job
88 # ------------------------------------------------------------------------
89 $CUR_DIR = "$rootdir";
90 Make_Prod_Link($rootdir,$suffix) || die "Cannot create links under $rootdir\n";
91
92 # ------------------------------------------------------------------------
93 # We are done ... get out of here
94 # ------------------------------------------------------------------------
95 exit 0;
96
97 # ************************************************************************
98 #                    F U N C T I O N S
99 # ************************************************************************
100
101 # ------------------------------------------------------------------------
102 # Scan the directory for AMANDA-Entries
103 # ------------------------------------------------------------------------
104 sub Make_Prod_Link {
105         my ($prefix,$suffix) = @_;
106
107         # --------------------------------------------------
108         # Just for info
109         # --------------------------------------------------
110         my $cur_dir = $CUR_DIR;
111         print "-> $CUR_DIR\n";
112
113         # --------------------------------------------------
114         # Change to given directory and read the inodes
115         # --------------------------------------------------
116         chdir $prefix or do { warn "$CUR_DIR: $!\n";
117                               return;
118                             };
119         opendir(DIR,".") or do { warn "$CUR_DIR: $!\n";
120                                  return;
121                                 };
122         my @inodes = grep(!/^\.$|^\.\.$/,readdir(DIR));
123
124         # --------------------------------------------------
125         # For each inode check if it is a directory or an
126         # amanda file
127         # --------------------------------------------------
128         foreach my $inode (@inodes) {
129                 if (-d $inode) {
130                         # ----------------------------------
131                         # For a directory -> recursion
132                         # ----------------------------------
133                         $CUR_DIR .= "/".$inode;
134                         Make_Prod_Link($inode,$suffix) or return;
135                         chdir ".." or do { warn "Cannot get back from $inode: $!\n";
136                                            return;
137                                          };
138                         $CUR_DIR = $cur_dir;
139                 }
140                 # -----------------------------------------------------
141                 # Create a symbolic link unless the file already exists
142                 # -----------------------------------------------------
143                 if (substr($inode,-length($suffix)) eq $suffix) {
144                         my $prog_name = substr($inode,0,-length($suffix));
145                         if (-e $prog_name && ! -l $prog_name) {
146                                 warn "Unexpected real file found: $CUR_DIR/$prog_name\n";
147                                 return;
148                         }
149                         unlink $prog_name;
150                         symlink($inode,$prog_name) or do { warn "Cannot create symbolical link for $prog_name -> $inode: $!\n";
151                                                            return;
152                                                          };
153                         print "    $prog_name -> $inode\n";
154                 } else {
155                         print "let it untouched: $inode\n" if $debug;
156                 }
157         }
158         1;
159 }