#!/usr/bin/perl -w

# $Id: cls-partition,v 1.3 2006/07/31 23:14:49 craig Exp $

#               Copyright (C) 2006, BroadOn Communications Corp.
#
#  These coded instructions, statements, and computer programs contain
#  unpublished  proprietary information of BroadOn Communications Corp.,
#  and  are protected by Federal copyright law. They may not be disclosed
#  to  third  parties or copied or duplicated in any form, in whole or in
#  part, without the prior written consent of BroadOn Communications Corp.


# This script generates an encrypted partition on a CLS machine.
# It puts the files generated on the CLS machine into the structure
# used by the rvl_prod_partition, calls the program, then cleans
# up.


use strict;
use Getopt::Long;
use Cwd;
use File::Basename;

my $eTicketExt       = ".eticket";
my $encryptedExt     = ".cry";

sub
usage()
{
    print "Usage: $0\n";
}

# Create an encrypted partition given the component files found
# on a CLS machine.

sub
generate_enc_partition($$)
{
    my ($dir, $keepFiles) = @_;
    my ($origDir, $mainDir);
    my (@subdirs, @allfiles);
    my ($file, $baseName, $eTicketFound, $contentFound, $tmpDirName);
    my ($eTicketName, $tmdName, $contentName, $hashName);
    my ($caName, $cpName, $xsName, @certList);
    my ($allCerts, $command);
    my $retVal = 1;


    $origDir = cwd();
    unless (chdir($dir)) {
        print "Can't change to directory $dir\n";
        return(0);
    }
    $tmpDirName = "part_files.$$";
    if (-e $tmpDirName) {
        print "$0: Error - file/directory $tmpDirName exists already";
        # Don't goto exit - don't want to remove existing file/dir
        return(0);
    }

    unless (mkdir($tmpDirName)) {
        print "$0: Can't make directory $tmpDirName ($!) - exit\n";
        $retVal = 0;
        goto end;
    }

    unless (opendir(DIR_HANDLE, ".")) {
        print "Can't open directory $dir\n";
        $retVal = 0;
        goto end;
    }
    # Grab all the file names except for . and ..
    @allfiles = grep !/^\.\.?$/, readdir DIR_HANDLE;
    closedir(DIR_HANDLE);

    # Look for a file with ending $eTicketExt
    # If there isn't exactly one such file, return error.
    # Use the base name of that file to find the tmd file.
    $eTicketFound = 0;
    foreach $file (@allfiles) {
        my ($name, $suffix, $extension);

        if (-f $file && -r $file) {

            ($name, $dir, $suffix) = fileparse($file, $eTicketExt);
            if ($suffix eq $eTicketExt) {
                if ($eTicketFound == 1) {
                    print "$0: Error - more than one file with extension";
                    print " $extension was found - exit\n";
                    $retVal = 0;
                    goto end;
                }
                $eTicketFound = 1;
                $baseName = $name;
                $eTicketName = $file;
            }
        }
    }

    if ($eTicketFound == 0) {
        print "$0: Error - no file with extension";
        print " $eTicketExt was found - exit\n";
        $retVal = 0;
        goto end;
    }

    $tmdName = "$baseName.tmd";
    if (! -r $tmdName) {
        print "$0: Error - can't read TMD file $baseName.tmd - exit";
        $retVal = 0;
        goto end;
    }

    $contentFound = 0;
    foreach $file (@allfiles) {
        my ($name, $suffix, $extension);

        if (-f $file && -r $file) {

            ($name, $dir, $suffix) = fileparse($file, $encryptedExt);
            if ($suffix eq $encryptedExt) {
                if ($contentFound == 1) {
                    print "$0: Error - more than one file with extension";
                    print " $encryptedExt was found - exit\n";
                    $retVal = 0;
                    goto end;
                }
                $contentFound = 1;
                $contentName = $file;
            }
        }
    }

    if ($contentFound == 0) {
        print "$0: Error - no file with extension";
        print " $encryptedExt was found - exit\n";
        $retVal = 0;
        goto end;
    }

    $hashName = "$baseName.hash";
    if (! -r $hashName) {
        print "$0: Error - can't read hash file $hashName\n";
        $retVal = 0;
        goto end;
    }

    $mainDir = cwd();
    unless (chdir($tmpDirName)) {
        print "$0: Can't change to directory $tmpDirName ($!)\n";
        $retVal = 0;
        goto end;
    }

    unless (symlink("../$eTicketName", "$baseName.tik")) {
        print "$0: Can't symlink '$eTicketName'\n";
        $retVal = 0;
        goto end;
    }
    unless (symlink("../$tmdName", "$baseName.tmd")) {
        print "$0: Can't symlink '$tmdName'\n";
        $retVal = 0;
        goto end;
    }
    unless (symlink("../$contentName", "$baseName.app")) {
        print "$0: Can't symlink '$contentName'\n";
        $retVal = 0;
        goto end;
    }
    unless (symlink("../$hashName", "$baseName.hash")) {
        print "$0: Can't symlink '$hashName'\n";
        $retVal = 0;
        goto end;
    }

    unless (chdir($mainDir)) {
        print "$0: Can't change to directory $mainDir ($!)\n";
        $retVal = 0;
        goto end;
    }

    $caName = "CA-cert.raw";
    $cpName = "CP-cert.raw";
    $xsName = "XS-cert.raw";

    @certList = ($caName, $cpName, $xsName);

    foreach $file (@certList) {
        if (! -r $file) {
            print "$0: File $file doesn't exist or isn't readable - exit\n";
            $retVal = 0;
            goto end;
        }
    }

    $allCerts = "$tmpDirName/allCerts";

    $command = "cat $caName $cpName $xsName > $allCerts"; 
    if (system($command) != 0) {
        print "$0: Command $command failed - exit\n";
        $retVal = 0;
        goto end;
    }

    $command = "rvl_prod_partition -c $tmpDirName -x $allCerts -o $baseName.enc_part"; 
    if ($keepFiles) {
        $command .= " -k";
    }
    if (system($command) != 0) {
        print "$0: Assembling the partition failed.\n";
        print "$0: Command was: $command\n";
        $retVal = 0;
        goto end;
    }

end:
    if (! $keepFiles && -e $tmpDirName) {
        if (system("rm -r $tmpDirName") != 0) {
            print "$0: Can't remove temporary directory $tmpDirName\n";
        }
    }
    unless (chdir($origDir)) {
        print "Can't change to directory $origDir\n";
        return;
    }
    return($retVal);
}

sub
main()
{
    my ($help, $verbose, $keep);
    my ($opt_return);
    my ($cur_dir);
    my ($name, $path, $suffix, $include_name);

    $opt_return = GetOptions (
                                 "help"    => \$help,
                                 "keep"    => \$keep,
                                 "verbose" => \$verbose,
                             );

    if (! defined ($keep)) {
        $keep = 0;
    }
    $cur_dir = cwd();
    if  (generate_enc_partition($cur_dir, $keep)) {
        if ($verbose) {
            print "$0: Generating partition succeeded\n";
        }
        exit(0);
    }
    else {
        print "$0: Generating partition failed\n";
        exit(1);
    }
}

main;
