#!/usr/bin/perl


use Getopt::Long qw(:config no_ignore_case);

$base = "/usr/local/crops";

unless ( GetOptions (

	"base=s" => \$base,
	"bin=s" => \$bin,
	"man1=s" => \$man1,
	"doc=s" => \$doc,
	"config=s" => \$config,

	"u" => \$info_only,
	"install|i" => \$install

)) { Usage(); exit(1); };

$bin = "$base/bin" unless $bin;
$man1 = "$base/man1" unless $man1;
$doc = "$base/doc" unless $doc;
$config = "/etc/crops/crops.conf" unless $config;

if ($info_only) { Usage(); die "\n"; };


print<<__ENDD__;

Installation will look like this:
-----------------------------------------------------
        Base dir: $base
         Bin dir: $bin
        Man1 dir: $man1
         Doc dir: $doc
       Conf file: $config

__ENDD__

die "Use ./install.pl -i to install, or -u for Usage instructions\n" 
	unless ($install);

for my $p ( qw/base bin doc man1/ ) {
	if  (!( ${$p} =~ /^[Nn]$/ )) {
		$n = "${p}/*";
		`mkdir -p "${$p}"`;
		`cp -a $n "${$p}/"`;
	}
}

if (!( "$config" =~ /^[Nn]$/ )) {
	$config =~ /(.*\/)/; $dir = $1;
	`mkdir -p "$dir"`;
	`cp -a crops.conf "$config"`;
}
`perl -p -i -e 's#__BASEDIR__#$base#' "$config"`;

chdir("./inst-data/") or die "Can't chdir to ./inst-data/ ($!)\n";

# Now make encodings and fonts
print<<__ENDD__;

Adding encodings
-----------------------------------------------------
__ENDD__

system ("./addencodings.sh 'encodings' '${bin}/' '$config'");

print<<__ENDD__;

Adding standard fonts
-----------------------------------------------------
__ENDD__
system ("./addfonts.sh 'fonts' '${bin}/' '$config'");


exit (0); ###### END


sub Usage {
print <<__ENDF__;

perl install.pl --base <basedir> --bin <bindir>
             --man1 <mandir> --doc <html files>
	     --config <config path> --install

     Defaults are
             base: /usr/local/crops/
              bin: /usr/local/crops/bin/
             man1: /usr/local/crops/man1/
           doc /usr/local/crops/doc/
           config: /etc/crops/crops.conf

     Keyword "N" disables particular category:
           perl install --doc N


__ENDF__

}

