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