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