a9b777473aac403f4889860781e19d1d5313bd89
[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 feisty meow script alias files.  Alias files
12 #  contain a list of definitions for command aliases that are written in the
13 #  specified shell dialect (such as bash or perl) and which are additionally
14 #  tailored for the 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 "filename_helper.pl";
25
26 use Env qw(FEISTY_MEOW_BINARIES BUILD_TOP FEISTY_MEOW_APEX FEISTY_MEOW_LOADING_DOCK FEISTY_MEOW_SCRIPTS DEBUG_FEISTY_MEOW );
27
28 # given a possible aliasable filename, this will decide whether to create a perl
29 # or bash alias for it.  it needs the filename of the possible alias and the
30 # directory where that file resides.
31 sub make_alias {
32   local($file, $dir) = @_;
33   if ($file =~ /\.[pP][lL]$/) { 
34     local($aliasname) = $file; $aliasname =~ s/\.[Pp][lL]$//;
35     &make_perl_alias($aliasname, "$dir");
36   } elsif ($file =~ /\.[sS][hH]$/) { 
37     local($aliasname) = $file; $aliasname =~ s/\.[Ss][Hh]$//;
38     &make_bash_alias($aliasname, "$dir");
39   }
40 }
41
42 # makes an alias for a bash script given the alias name.
43 sub make_bash_alias {
44   local($aliasname) = shift(@_);
45   local($full_alias) = $aliasname;
46 #print "full alias is $full_alias\n";
47   $aliasname =~ s/^.*\/([^\/]*)/\1/;
48 #print "alias became $aliasname\n";
49   print she "define_yeti_alias $aliasname=\"bash $full_alias.sh\"\n";
50 }
51
52 # makes an alias for a perl script given the alias name.
53 sub make_perl_alias {
54   local($aliasname) = shift(@_);
55   local($full_alias) = $aliasname;
56   $aliasname =~ s/^.*\/([^\/]*)/\1/;
57 #print "alias became $aliasname\n";
58   print she "define_yeti_alias $aliasname=\"perl $full_alias.pl\"\n";
59 }
60
61 ##############
62
63 # The "common.alias" file is used in the generated aliases file as a base
64 # set of generally useful aliases.  We also add aliases for any script files
65 # (perl, bash, python, etc) that we find in the feisty meow script hierarchy.
66 # Any *.alias files found in the $FEISTY_MEOW_LOADING_DOCK/custom folder are
67 # loaded also.
68 sub rebuild_script_aliases {
69
70   if (length($DEBUG_FEISTY_MEOW)) {
71     print "rebuilding generated aliases file...\n";
72   }
73
74   # create our generated shells directory if it's not already.
75   if ( ! -d $FEISTY_MEOW_LOADING_DOCK ) {
76     mkdir $FEISTY_MEOW_LOADING_DOCK;
77     if (length($DEBUG_FEISTY_MEOW)) {
78       print "made FEISTY_MEOW_LOADING_DOCK at '$FEISTY_MEOW_LOADING_DOCK'\n";
79     }
80   }
81
82   # test if we can use color in ls...
83 #  $test_color=` ls --help 2>&1 | grep -i color `;
84
85   # this is an array of files from which to draw alias definitions.
86   @ALIAS_DEFINITION_FILES = ("$FEISTY_MEOW_SCRIPTS/core/common.alias");
87
88   # if custom aliases files exist, add them to the list.
89   foreach $i (&glob_list("$FEISTY_MEOW_LOADING_DOCK/custom/*.alias")) {
90     if (-f $i) { push(@ALIAS_DEFINITION_FILES, $i); }
91   }
92   if (length($DEBUG_FEISTY_MEOW)) {
93     print "using these alias files:\n";
94 print "HEY IS THIS PROBLEM CHILD?\n";
95     foreach $i (@ALIAS_DEFINITION_FILES) {
96       local $base_of_dir = &basename(&dirname($i));
97       local $basename = &basename($i);
98       print "  $base_of_dir/$basename\n";
99     }
100 print "WAS PROBLEM CHILD ABOVE HERE?\n";
101   }
102
103   # write the aliases for sh and bash scripts.
104
105   local $GENERATED_ALIAS_FILE = "$FEISTY_MEOW_LOADING_DOCK/fmc_core_and_custom_aliases.sh";
106   if (length($DEBUG_FEISTY_MEOW)) {
107     print "writing generated aliases in $GENERATED_ALIAS_FILE...\n";
108   }
109
110 #hmmm: perhaps a good place for a function to create the header,
111 #      given the appropriate comment code.
112
113   open GENOUT, ">$GENERATED_ALIAS_FILE" or die "cannot open $GENERATED_ALIAS_FILE";
114
115   print GENOUT "##\n";
116   print GENOUT "## generated file: $GENERATED_ALIAS_FILE\n";
117   print GENOUT "## please do not edit.\n";
118   print GENOUT "##\n";
119
120 #  if (length($test_color)) {
121 #    print GENOUT "export color_add='--color=auto'\n";
122 #  } else {
123 #    print GENOUT "export color_add=\n";
124 #  }
125
126   # plow in the full set of aliases into the file.
127   foreach $i (@ALIAS_DEFINITION_FILES) {
128     open CURR_ALIASER, "<$i" or die "cannot open current alias file $i";
129     foreach $line (<CURR_ALIASER>) {
130       print GENOUT "$line";
131     }
132   }
133
134   close GENOUT;
135
136   if (length($DEBUG_FEISTY_MEOW)) {
137     print("done rebuilding generated aliases file.\n");
138   }
139 }
140
141 ##############
142
143 # make sure we know where to store the files we're creating.
144 if ( ! length("$FEISTY_MEOW_LOADING_DOCK") ) {
145   print "\
146 The FEISTY_MEOW_LOADING_DOCK variable is not defined.  This must point to\
147 the location where the generated scripts are stored.  You may still need to\
148 configure the feisty meow script system with something like:\
149   bash /opt/feistymeow.org/feisty_meow/scripts/core/reconfigure_feisty_meow.sh\
150 Please see http://feistymeow.org for more details.\n";
151   exit 1;
152 #really need to use better exit codes.
153 }
154
155 ##############
156
157 $FEISTY_MEOW_LOADING_DOCK =~ s/\\/\//g;
158 $FEISTY_MEOW_SCRIPTS =~ s/\\/\//g;
159 $FEISTY_MEOW_APEX =~ s/\\/\//g;
160
161 ##############
162
163 # create our generated shells directory if it's not already there.
164 if (! -d $FEISTY_MEOW_LOADING_DOCK) {
165   mkdir $FEISTY_MEOW_LOADING_DOCK;
166 }
167
168 ##############
169
170 # set the executable bit for binaries for just this current user.
171 if (-d $FEISTY_MEOW_BINARIES) {
172   system("chmod -R u+x \"$FEISTY_MEOW_BINARIES\"/*");
173 }
174
175 ##############
176
177 # generate the first set of alias files that are defined in the core
178 # and custom scripts directories.
179 &rebuild_script_aliases;
180
181 ##############
182
183 # trash the old versions.
184 unlink("$FEISTY_MEOW_LOADING_DOCK/fmc_aliases_for_scripts.sh");
185
186 if (length($DEBUG_FEISTY_MEOW)) {
187   printf "writing $FEISTY_MEOW_LOADING_DOCK/fmc_aliases_for_scripts.sh...\n";
188 }
189
190 ##############
191
192 # open the alias files to be created.
193 open(she, ">> $FEISTY_MEOW_LOADING_DOCK/fmc_aliases_for_scripts.sh");
194
195 # find the list of files in the scripts directory.
196 @shell_files = (find_files(recursive_find_directories("$FEISTY_MEOW_SCRIPTS")),
197     find_files(recursive_find_directories("$FEISTY_MEOW_LOADING_DOCK/custom/scripts")));
198
199 # strip out the customization files, since they are added in on demand only.
200 #print "before filtering list: @shell_files\n";
201 @shell_files = grep ! /\/customize\//, @shell_files;
202 #print "after filtering list: @shell_files\n";
203
204 #printf "found all these files in main script dirs:\n";
205 #printf "  @shell_files\n";
206
207 # construct aliases for items in the scripts directory.
208 foreach $file (@shell_files) {
209   # test to see what type of item we got.
210   if ($file =~ '^\.$'
211       || $file =~ '^\.\.$'
212       || $file =~ '^.svn$'
213       || $file =~ '^.git$'
214       || $file =~ /\/\.$/
215       || $file =~ /\/\.\.$/
216       || $file =~ /\/\.svn$/
217       || $file =~ /\/\.git$/
218       || $file =~ /\/custom\/[a-zA-Z0-9_]+\/[a-zA-Z0-9_.]+$/
219 #hmmm: would be nice to have this name in a symbol somewhere instead of having "customize" everywhere.
220       ) {
221     # just skip this item; it's a special directory or a file we don't want to include.
222 #    print "skipping name: $file\n";
223   } else {
224      &make_alias($file, "");
225   }
226 }
227
228 close(she);
229
230 ##############
231
232 # prepare a finalizer chunk that is the last thing to load.
233
234 open(she, ">> $FEISTY_MEOW_LOADING_DOCK/fmc_ending_sentinel.sh");
235
236 # write in our sentinel alias that says alias loading was handled.
237 print she "define_yeti_alias CORE_ALIASES_LOADED=true\n";
238
239 close(she);
240
241 ##############
242
243 1;
244