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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
use strict;
use Fcntl;
use constant CF_CACHE => '.cf.cache';
use constant BLOCKSIZE => 8192;
use DBIShell::UTIL qw(get_param);
use constant BUNDLED_LIB_DIRS => ('DBIShell' ,
'DBIShell/dr' ,
'DBIShell/help',
);
use constant EXTRA_DATA_DIRS => ('icons');
use vars qw(*SH *TH *CF);
#use vars qw($DDIR $BDIR);
#use vars qw($PERL $IDIR $LDIR);
sub interpolate_and_copy ($$$);
my(%RH);
my($dirent);
# this sould all get defaults from the makefile,
# but some people don't read the docs [or run the Makefile]
# so for them we add defaults here:
$CF{PERL} = $ARGV[0] || '/usr/bin/perl';
$CF{IDIR} = $ARGV[1] || '/usr/local/dbishell';
$CF{LDIR} = $ARGV[2];
$CF{PFIX} = $ARGV[3] || '';
# load defaults from config cache:
if(sysopen(*CF, CF_CACHE, O_RDONLY))
{
while(<CF>) { /^(\w+)=(.*)/ && ($CF{$1} = $2) }
close(*CF);
}
# couple of basic questions, default to answers from cache:
$CF{PERL} = get_param("Where does your perl live? [$CF{PERL}] : ")
|| $CF{PERL};
$CF{IDIR} = get_param("Where should I install dbishell? [$CF{IDIR}] : ")
|| $CF{IDIR};
# construct sensible defaults for anything still missing:
$CF{BDIR} ||= join('/',$CF{IDIR},'bin');
$CF{LDIR} ||= join('/',$CF{IDIR},'lib');
$CF{DDIR} ||= join('/',$CF{IDIR},'share/doc/dbishell');
$CF{SDIR} ||= join('/',$CF{IDIR},'share/dbishell');
# Ok - now the user gets a chance to override:
$CF{BDIR}
= get_param("Where should I install the dbishell script? [$CF{BDIR}] : ")
|| $CF{BDIR};
$CF{LDIR}
= get_param("Where should I install the dbishell libs? [$CF{LDIR}] : ")
|| $CF{LDIR};
$CF{DDIR}
= get_param("Where should I install the dbishell docs? [$CF{DDIR}] : ")
|| $CF{DDIR};
$CF{SDIR}
= get_param("Where should I install extra data files? [$CF{SDIR}] : ")
|| $CF{SDIR};
# save state to cache:
if(sysopen(*CF, CF_CACHE, O_WRONLY|O_CREAT|O_TRUNC))
{
foreach (keys(%CF)) { print(CF "$_=$CF{$_}\n") }
close(*CF);
}
# make install paths or die:
mkdirhier($CF{IDIR}, 0755) || exit(0);
mkdirhier($CF{BDIR}, 0755) || exit(0);
mkdirhier($CF{DDIR}, 0755) || exit(0);
mkdirhier($CF{LDIR}, 0755) || exit(0);
mkdirhier($CF{SDIR}, 0755) || exit(0);
foreach my $dir (BUNDLED_LIB_DIRS)
{
mkdirhier(join('/', $CF{LDIR}, $dir), 0755) || exit(0);
}
foreach my $dir (EXTRA_DATA_DIRS)
{
mkdirhier(join('/', $CF{SDIR}, $dir), 0755) || exit(0);
}
#-d($IDIR) || mkdir($IDIR, 0755);
#-d($LDIR) || mkdir($LDIR, 0755);
#-d("$LDIR/DBIShell") || mkdir("$LDIR/DBIShell", 0755);
$RH{PERL} = $CF{PERL};
$RH{LDIR} = $CF{LDIR};
$RH{SDIR} = $CF{SDIR};
interpolate_and_copy('LICENSE' , "$CF{DDIR}/LICENSE" , 0444);
interpolate_and_copy('LICENSE' , "$CF{DDIR}/README" , 0444);
interpolate_and_copy('dbishell' , "$CF{IDIR}/bin/dbishell" , 0555);
interpolate_and_copy('DBIShell.pm' , "$CF{LDIR}/DBIShell.pm" , 0444);
#unnecessary?
#interpolate_and_copy('DBIShell/Gtk.pm', "$CF{LDIR}/DBIShell/Gtk.pm", 0444);
foreach my $dir (BUNDLED_LIB_DIRS)
{
opendir(DH, $dir);
while ($dirent = readdir(DH))
{
if($dirent =~ /^[A-Z_]+.pm$/i)
{
interpolate_and_copy(join('/', $dir, $dirent),
join('/', $CF{LDIR}, $dir, $dirent),
0444
);
}
}
closedir(DH);
}
foreach my $dir (EXTRA_DATA_DIRS)
{
opendir(DH, $dir);
DATAFILE:
while($dirent = readdir(DH))
{
if( $dirent =~ /^\./ ) { next DATAFILE }
copy_file( join('/', $dir, $dirent),
join('/', $CF{SDIR}, $dir, $dirent),
0444
);
}
closedir(DH);
}
sub mkdirhier ($)
{
my($target,@target,$T,$mode);
$target = $CF{PFIX} . $_[0];
$mode = $_[1];
($T,@target) =
($target =~ /^\//) ?
(undef(), split(/\//,$target)) :
split(/\//,$target);
#warn("mkdirhier($target)\n");
DIRECTORY:
foreach (@target)
{
$T = join('/', $T, $_);
#warn("$T\n");
-d($T) ?
next DIRECTORY :
(mkdir($T, $mode)
|| (warn("mkdir($T, $mode): $!\n"),return 0));
}
return 1;
}
sub interpolate_and_copy ($$$)
{
my $sh = local(*SH);
my $th = local(*TH);
my ($src,$dst,$mode) = @_;
# warn("i_a_c(@_)\n");
sysopen($sh, $src, O_RDONLY)
|| die("sysopen($src) failed: $!\n");
unlink($CF{PFIX}.$dst);
sysopen($th, $CF{PFIX}.$dst, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, $mode)
|| die("sysopen($dst) failed: $!\n");
while (<$sh>)
{
if(m/^#::(.*)/s)
{
$_ = $1; s/<%(\w+)%>/$RH{$1}/g;
}
print($th $_);
}
close($th);
close($sh);
}
sub sysprnt ($$$)
{
my $done = 0;
my($fh,$data,$len) = @_;
while( $done < $len )
{
my $d = syswrite($fh, $data, $len, $done);
$done += defined($d) ? $d : die("syswrite() failed: $!\n");
}
}
sub copy_file ($$$)
{
my $buf = ' ' x BLOCKSIZE;
my $sh = local(*SH);
my $th = local(*TH);
my ($src,$dst,$mode) = @_;
sysopen($sh, $src, O_RDONLY)
|| die("sysopen($src) failed: $!\n");
unlink($dst);
sysopen($th, $CF{PFIX}.$dst, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, $mode)
|| die("sysopen($dst) failed: $!\n");
while(sysread($sh, $buf, BLOCKSIZE) > 0)
{
sysprnt($th, $buf, length($buf));
}
close($th);
close($sh);
}
__END__
|