added a recustom macro for copying my personal aliases into the
[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("$FEISTY_MEOW_GENERATED") ) {
74   print "\
75 The FEISTY_MEOW_GENERATED 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 $FEISTY_MEOW_GENERATED =~ s/\\/\//g;
84 $FEISTY_MEOW_SCRIPTS =~ s/\\/\//g;
85 $FEISTY_MEOW_DIR =~ s/\\/\//g;
86
87 # create our generated shells directory if it's not already there.
88 if (! -d $FEISTY_MEOW_GENERATED) {
89   mkdir $FEISTY_MEOW_GENERATED;
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 \"$FEISTY_MEOW_SCRIPTS\"/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 FEISTY_MEOW_SCRIPTS
105   # (see below).
106
107 # trash the old versions.
108 unlink("$FEISTY_MEOW_GENERATED/p_alias.sh");
109
110 printf "writing $FEISTY_MEOW_GENERATED/p_alias.sh...\n";
111
112 # open the alias files to be created.
113 open(she, ">> $FEISTY_MEOW_GENERATED/p_alias.sh");
114
115 #print "os is $OS\n";
116
117 # find the list of files in the scripts directory.
118 #opendir(scripts, "$FEISTY_MEOW_SCRIPTS");
119 #@shell_files = sort(readdir(scripts));
120 #print "yeti scripts: @shell_files\n";
121
122 @shell_files = &load_file_names("$FEISTY_MEOW_SCRIPTS");
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 =~ '^.git$'
131       || $file =~ /\/\.$/
132       || $file =~ /\/\.\.$/
133       || $file =~ /\/\.svn$/
134       || $file =~ /\/\.git$/
135       ) {
136     # just skip this item; it's a special directory.
137   } elsif (-d "$FEISTY_MEOW_SCRIPTS/$file") {
138     # if we see a subdirectory in the scripts folder, we add all the
139     # scripts in it as aliases.  we recurse only one level.
140     opendir(subdir, "$FEISTY_MEOW_SCRIPTS/$file");
141     @subdir_files = sort(readdir(subdir));
142     foreach $subfile (@subdir_files) {
143       push(@shell_files, "$file/$subfile");
144     }
145   } else {
146     # if it's a regular file, we'll try to make an alias for it.  the function
147     # will only fire if the ending is appropriate for the script languages we use.
148     &make_alias($file, "$FEISTY_MEOW_SCRIPTS");
149   }
150 }
151
152 # open the source repository's script directory to find scripts in there.
153 local($build_shell_path) = "$BUILD_TOP/scripts/generator";
154 @build_shells = &load_file_names("$build_shell_path");
155 #opendir(build_shells_dir, $build_shell_path);
156 #@build_shell_files = sort(readdir(build_shells_dir));
157 #if (scalar(@build_shell_files) > 0) {
158 #  print "build shell folders: @build_shell_files\n";
159 #}
160 foreach $file (@build_shells) {
161   &make_alias($file, "$build_shell_path");
162 }
163
164 close(she);
165