-
Notifications
You must be signed in to change notification settings - Fork 49
/
configure
executable file
·156 lines (144 loc) · 5.02 KB
/
configure
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env perl
# the above line will use any perl that is in your PATH. You might want to
# replace it by #!/usr/bin/perl or similar if this does not work for you.
use strict;
use warnings;
use File::Copy;
use Getopt::Long;
# find sxw2txt and other scripts in current dir, if scripts not installed yet
$ENV{PATH} .= ':.';
use vars qw($opt_help $opt_prefix $opt_nomake $opt_shell $opt_all_completions);
Getopt::Long::Configure("prefix_pattern=--");
my $result = GetOptions('help+', 'prefix=s', 'shell=s', 'nomake+', 'all-completions+');
if ( $ARGV[0] or ! $result or $opt_help) {
print << 'EOF';
Usage: configure [options]
Options:
--help print this message
--shell=<filename> specify an alternative shell path (zsh/bash) to use
--nomake do not generate a Makefile
--all-completions always install all completions
Directory and file names:
--prefix=PREFIX install lesspipe.sh in PREFIX/bin (/usr/local)
configure generates by default a Makefile
EOF
exit ! $opt_help ? 1 : 0;
}
my $bash_complete_dir = `pkg-config --variable=completionsdir bash-completion`;
chomp $bash_complete_dir;
my $prefix = $opt_prefix || $ENV{PREFIX} || '/usr/local';
# remove trailing slash and trailing bin directory
print "removed trailing /bin dir from prefix\n" if $prefix =~ m|/bin/?$|;
$prefix =~ s|(/bin)?/?$||;
if ( $opt_shell and -f $opt_shell and $opt_shell =~ /^\// ) {
# do nothing
} elsif ( $opt_shell) {
print "unuseable shell $opt_shell: not executable or no absolute path\n";
exit 1;
}
# gererate Makefile
my @bad = ();
my $shell = check_shell_vers();
if ( ! $opt_nomake ) {
my $no_bash = (grep {/bash/} @bad and ! $opt_all_completions);
my $no_zsh = (grep {/zsh/} @bad and ! $opt_all_completions);
open OUT, ">Makefile";
while (<DATA>) {
next if /bash_complete_dir/ and $no_bash;
next if /zsh\/site-functions/ and $no_zsh;
s/opt_prefix/$prefix/;
if ($bash_complete_dir and ! $opt_prefix) {
s/mkdir.*bash-completion.*/mkdir -p $bash_complete_dir/;
s/cp.*bash-completion.*/cp .\/less_completion $bash_complete_dir/;
}
print OUT;
}
close OUT;
}
$bash_complete_dir = "$prefix/share/bash-completion";
print "installing bash completion in $bash_complete_dir\nIn bash, please preload the completion, dynamic invocation does not work\n. $bash_complete_dir/less_completion\nOr consider installing the file less_completion in /etc/bashcompletion.d\n";
open F, "lesspipe.sh";
{
my $line1 = <F>;
exit if $line1 =~ /^$shell$/;
local $/;
my $in = <F>;
print "generating new lesspipe.sh using '$shell' as first line\n";
copy "lesspipe.sh", "lesspipe.sh.old";
open OUT, ">lesspipe.sh";
print OUT "$shell\n";
print OUT $in;
close OUT;
}
exit 0;
sub check_shell_vers {
# define useable shells, the order is important !
my @shells = qw( /bin/bash /bin/zsh /bin/sh);
@shells = ( $opt_shell ) if $opt_shell;
my $selected_shell = '';
my $shellcmd = '';
for my $shell ( @shells ) {
# get the basename of the shell and shell options
my ($path, $name, $opt);
if ( $shell =~ /(.*)\/([^\/]+)(\s.*)$/ ) {
($path, $name, $opt) = ($1, $2, $3);
} else {
($path, $name, $opt) = ($1, $2, "") if $shell =~ /(.*)\/([^\/]+)\s*$/;
}
# do we have the shell in the PATH
my $versstr = uc $name.'_VERSION';
$versstr = 'BASH_VERSION' if $name eq 'sh';
my @where = grep { -x $_."/$name" } split ':', $ENV{PATH};
$where[0] = $path if -x $path.'/'.$name;
my $file = $where[0].'/'.$name if $where[0];
if ( ! $where[0] or ! -x $file ) {
print "$name not found in the PATH, continuing\n";
push @bad, $shell;
next;
}
# get the shell version
my $v = `$file -c \'echo \$$versstr\'`;
chomp $v;
### print "$file $v found in the PATH\n";
if ( $name eq 'bash' or $name eq 'sh' ) {
my $tst = `$file -c \'if [[ -n 1 && -n 2 ]];then true;fi 2>&1\'`;
if ( $tst ) {
push @bad, $shell;
next;
}
}
$selected_shell = $name if ! $selected_shell;
$shellcmd = "$file$opt" if ! $shellcmd;
}
if ( !$selected_shell ) {
print "Sorry, no useable shell found, the following were tried:\n@bad\n";
print "You could run configure --shell=<abs_filename> to retry\n";
exit 1;
}
return "#!/usr/bin/env $selected_shell";
}
__END__
# This is a generated file, do not edit it. Use configure to modify it
PREFIX = opt_prefix
.PHONY: install
all:
./configure --prefix=$(PREFIX)
test:
./test.pl
install:
mkdir -p $(DESTDIR)$(PREFIX)/bin
mkdir -p $(DESTDIR)$(PREFIX)/share/man/man1
mkdir -p $(DESTDIR)$(PREFIX)/share/zsh/site-functions
mkdir -p $(DESTDIR)$(PREFIX)/share/bash-completion/
cp ./code2color ./sxw2txt ./archive_color ./lesspipe.sh ./vimcolor ./lesscomplete $(DESTDIR)$(PREFIX)/bin
cp ./lesspipe.1 $(DESTDIR)$(PREFIX)/share/man/man1
cp ./less_completion $(DESTDIR)$(PREFIX)/share/bash-completion/
cp ./_less $(DESTDIR)$(PREFIX)/share/zsh/site-functions
chmod 0755 $(DESTDIR)$(PREFIX)/bin/lesspipe.sh
chmod 0755 $(DESTDIR)$(PREFIX)/bin/sxw2txt
chmod 0755 $(DESTDIR)$(PREFIX)/bin/code2color
chmod 0755 $(DESTDIR)$(PREFIX)/bin/archive_color
chmod 0755 $(DESTDIR)$(PREFIX)/bin/vimcolor
chmod 0755 $(DESTDIR)$(PREFIX)/bin/lesscomplete
clean:
mv Makefile Makefile.old