first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / scripts / core / generate_aliases.pl
1 #!/usr/bin/perl
2
3 ###############################################################################
4 #                                                                             #
5 #  Name   : generate_aliases                                                  #
6 #  Author : Chris Koeritz                                                     #
7 #  Rights : Copyright (C) 1996-$now by Author                                 #
8 #                                                                             #
9 #  Purpose:                                                                   #
10 #                                                                             #
11 #    This script generates YETI alias files.  Alias files contain a list of   #
12 #  definitions for command aliases that are written in the specified shell    #
13 #  dialect (such as bash or perl) and which are additionally tailored for the #
14 #  operating system to be used.                                               #
15 #                                                                             #
16 ###############################################################################
17 #  This program is free software; you can redistribute it and/or modify it    #
18 #  under the terms of the GNU General Public License as published by the Free #
19 #  Software Foundation; either version 2 of the License or (at your option)   #
20 #  any later version.  See: "http://www.gruntose.com/Info/GNU/GPL.html" for a #
21 #  version of the License.  Please send any updates to "fred@gruntose.com".   #
22 ###############################################################################
23
24 require "importenv.pl";
25
26 # given a possible aliasable filename, this will decide whether to create a perl
27 # or bash alias for it.  it needs the filename of the possible alias and the
28 # directory where that file resides.
29 sub make_alias {
30   local($file, $dir) = @_;
31   if ($file =~ /\.[pP][lL]$/) { 
32     local($aliasname) = $file; $aliasname =~ s/\.[Pp][lL]$//;
33     &make_perl_alias($aliasname, "$dir");
34   } elsif ($file =~ /\.[sS][hH]$/) { 
35     local($aliasname) = $file; $aliasname =~ s/\.[Ss][Hh]$//;
36     &make_bash_alias($aliasname, "$dir");
37   }
38 }
39
40 # makes an alias for a bash script given the alias name.
41 sub make_bash_alias {
42   local($aliasname) = shift(@_);
43   local($full_alias) = $aliasname;
44   $aliasname =~ s/^.*\/([^\/]*)/\1/;
45 #print "alias became $aliasname\n";
46   local($source_dir) = shift(@_);
47 #print "bash alias is $aliasname, dir is $source_dir\n";
48   print she "alias $aliasname=\"bash $source_dir/$full_alias.sh\"\n";
49 }
50
51 # makes an alias for a perl script given the alias name.
52 sub make_perl_alias {
53   local($aliasname) = shift(@_);
54   local($full_alias) = $aliasname;
55   $aliasname =~ s/^.*\/([^\/]*)/\1/;
56 #print "alias became $aliasname\n";
57   local($source_dir) = shift(@_);
58 #print "perl alias is $aliasname, dir is $source_dir\n";
59   print she "alias $aliasname=\"perl $source_dir/$full_alias.pl\"\n";
60 }
61
62 # given a directory, this returns an array of all the filenames found therein.
63 sub load_file_names {
64   local($path) = shift(@_);
65   opendir(that_dir, $path);
66   local(@those_files) = sort(readdir(that_dir));
67   return @those_files;
68 }
69
70 ##############
71
72 # make sure we know where to store the files we're creating.
73 if ( ! length("$GENERADIR") ) {
74   print "\
75 The GENERADIR variable is not defined.  This must point to the location where\n\
76 the generated scripts are stored.  Perhaps you still need to run\n\
77 bootstrap_shells.sh and set up some environment variables.  Please see\n\
78 http://yeticode.org for more details.\n";
79   exit 1;
80 #really need to use better exit codes.
81 }
82
83 $GENERADIR =~ s/\\/\//g;
84 $SHELLDIR =~ s/\\/\//g;
85 $YETI_DIR =~ s/\\/\//g;
86
87 # create our generated shells directory if it's not already there.
88 if (! -d $GENERADIR) {
89   mkdir $GENERADIR;
90 }
91
92 ##############
93
94 # set the executable bit for yeti binaries for just this current user.
95 if (-d $BINDIR) {
96   system("chmod -R u+x \"$BINDIR\"/*");
97 }
98
99 ##############
100
101 system("bash \"$SHELLDIR\"/core/unter_alia.sh");
102   # generate the first set of alias files; these are the root files used
103   # by the shell.  each of them will be written to in turn invoke the
104   # p_alias files which are made from the set of scripts in YETI_SCRIPTS
105   # (see below).
106
107 # trash the old versions.
108 unlink("$GENERADIR/p_alias.sh");
109
110 printf "writing $GENERADIR/p_alias.sh...\n";
111
112 # open the alias files to be created.
113 open(she, ">> $GENERADIR/p_alias.sh");
114
115 #print "os is $OS\n";
116
117 # find the list of files in the scripts directory.
118 #opendir(scripts, "$SHELLDIR");
119 #@shell_files = sort(readdir(scripts));
120 #print "yeti scripts: @shell_files\n";
121
122 @shell_files = &load_file_names("$SHELLDIR");
123
124 # construct aliases for items in the scripts directory.
125 foreach $file (@shell_files) {
126   # test to see what type of item we got.
127   if ($file =~ '^\.$'
128       || $file =~ '^\.\.$'
129       || $file =~ '^.svn$'
130       || $file =~ /\/\.$/
131       || $file =~ /\/\.\.$/
132       || $file =~ /\/\.svn$/) {
133     # just skip this item; it's a special directory.
134   } elsif (-d "$SHELLDIR/$file") {
135     # if we see a subdirectory in the scripts folder, we add all the
136     # scripts in it as aliases.  we recurse only one level.
137     opendir(subdir, "$SHELLDIR/$file");
138     @subdir_files = sort(readdir(subdir));
139     foreach $subfile (@subdir_files) {
140       push(@shell_files, "$file/$subfile");
141     }
142   } else {
143     # if it's a regular file, we'll try to make an alias for it.  the function
144     # will only fire if the ending is appropriate for the script languages we use.
145     &make_alias($file, "$SHELLDIR");
146   }
147 }
148
149 # open the source repository's script directory to find scripts in there.
150 local($build_shell_path) = "$BUILD_TOP/scripts/generator";
151 @build_shells = &load_file_names("$build_shell_path");
152 #opendir(build_shells_dir, $build_shell_path);
153 #@build_shell_files = sort(readdir(build_shells_dir));
154 #if (scalar(@build_shell_files) > 0) {
155 #  print "build shell folders: @build_shell_files\n";
156 #}
157 foreach $file (@build_shells) {
158   &make_alias($file, "$build_shell_path");
159 }
160
161 close(she);
162