#!/usr/bin/perl
#
# Copyright 1992, A. Brossard
#
# upgrade the libc's
#
sub upgrade_libc {
    local( $root, $usr, $src_files, $arch ) = @_;
    local( $libc, @tmp, @tmp2, $ending, $libc_dst );

    if( $crypt ) {
	$ending = '';
	if( ! -e "$root$crypt_file" ) {
	    open( CRYPT, ">>$root$crypt_file" ) || die $lang ? "Impossible de crer '$root$crypt_file'\n" : "Couldn't create '$root$crypt_file'\n";
	    print CRYPT "This machine uses the special crypt for Sun's at the EPFL\n";
	    close CRYPT;
	}
    } else {
	$ending = '.no.crypt';
    }
    if( ! chdir "$src_files/$arch" ) {
	&print( "  Unable to get the new libraries in $src_files/$arch.\n",
		"   Impossible de trouver les nouvelles libraries dans $src_files/$arch.\n");
	return;
    }
    @tmp = <libc.so.*>;
    foreach $libc ( @tmp ) {
	if ( $crypt && $libc =~ /.no.crypt$/ ) { next; }
	if ( ! $crypt && $libc !~ /.no.crypt$/ ) { next; }
	$libc_dst = $libc;
	$libc_dst =~ s/.no.crypt$//;
	if( $libc =~ /^libc.so/ ) { $libc_dst = "$usr/lib/$libc_dst"; }
	elsif( $libc =~ /^lib5c.so/ ) { $libc_dst = "$usr/5lib/$libc_dst"; }
	if( -e $libc_dst ) {
	    &print( "Warning you already had a $libc_dst\n",
	     "Avertissement: vous sembliez dja avoir cette libc: $libc_dst\n");
	    unlink $libc_dst;
	}
	system "cp -p $libc $libc_dst";
	chmod 0644, $libc_dst;
    }
    chdir "$usr/lib";
    rename( 'libc.a', 'libc.a.FCS' ) if ! -e 'libc.a.FCS';
    system "cp -p $src_files/$arch/libc.a$ending libc.a; ranlib libc.a"
			if -e "$src_files/$arch/libc.a$ending";
    chdir "$usr/5lib";
    system "cp -p $src_files/$arch/lib5c.a$ending libc.a; ranlib libc.a"
			if -e "$src_files/$arch/lib5c.a$ending";
    system "cp -p $src_files/$arch/lib5c_p.a$ending libc_p.a; ranlib libc_p.a"
			if -e "$src_files/$arch/lib5c_p.a$ending";
    chdir "$usr/lib";
    @tmp = <libc.sa.*>;
    @tmp2 = <libc.so.*>;
    ($libc = $tmp2[$#tmp2]) =~ s/\.so\./\.sa\./;
    link( $tmp[$#tmp], $libc ) if $libc ne $tmp[$#tmp];
    system "ranlib $libc";
    chdir "$usr/5lib";
    @tmp = <libc.sa.*>;
    @tmp2 = <libc.so.*>;
    ($libc = $tmp2[$#tmp2]) =~ s/\.so\./\.sa\./;
    link( $tmp[$#tmp], $libc ) if $libc ne $tmp[$#tmp];
    system "ranlib $libc";
    &print( "  All the libc's have been installed, running ldconfig\n",
	"  Toutes les libc ont t installes, faisons un ldconfig\n");
    system 'ldconfig';
}


if( !$src_files ) {	# standalone
    local( $mount ) = 0;
    $src_files = "/tmp/4.1.2";
    chop( $arch=`arch` );
    $rhost='sasun1';
    $crypt_file='/etc/install/crypt';
    $mount_prog = "/export/mnt/4.1.2";

    $ENV{PATH}="/usr/etc:$ENV{PATH}";

    if( ! -d "$src_files/$arch" ) {
	$mount = 1;
	mkdir( $src_files, 02755);
	system "mount $rhost:$mount_prog $src_files";
    }
    if( ! -d "$src_files/$arch" ) {
	die  $lang ? "mount de $rhost:$mount_prog sur $src_files a chou, aborting\n" : "mount of $rhost:$mount_prog on $src_files failed, aborting\n";
    }
    push( @INC, "$src_files/perl-lib");
    require 'print.pl';

    if( -e $crypt_file ) {
	$crypt = 1;	# Use special encryption library
    }
    &upgrade_libc( '/', '/usr', $src_files, $arch );

    system "umount $src_files" if $mount;
    rmdir $src_files if $mount;
}
1;
