#!/usr/bin/perl

#****************************************************************************#
#- Table of contents
##TOC
#****************************************************************************#
#- 1 Scope
#- 2 Identity shape model files

#****************************************************************************#
#- 1
## Scope
#****************************************************************************#
#  In an email from 8 Nov 2017, Joel Parker noted that some of the 3dtool
#  scenarios use an old shape model with flat southern hemisphere. As an
#  example he gave
#  VSTP/escort_2015-02-05T20-30-01_RATT_DV_079_01_01____00135/VSTP_076/
#
#  In that directory, the file index.html loads
#  CSHP_DV_042_01_______00077_v31486_f62968_reordered.js
#  from the directory common/models.
#
#  The file hd_index.html loads
#  CG6725K_10percent.js
#  from the same directory.
#
#  Both js files provide a function cometgeo().
#
#  So this is the plan:
#
#     1. Identity all files in the model directory which provide a function
#        cometgeo.
#
#     2. Find all files in VSTP_??? directories which contain the name of any
#        of the files found in step 1.
#
#     3. Replace the filenames appropriately, or mv the unwanted shape models
#        out of the way and replace them by links of the favoured one.

#****************************************************************************#
#- 2
## Identity shape model files
#****************************************************************************#
@tmp = `cd /home/rossim/html/3dtool/common/models/; grep cometgeo *.js | grep function`;
foreach( @tmp ) {
    if ( /(\S+):/ ) {
	$shape_file[$i] = $1;
	print "$shape_file[$i]\n";
	$i++;
    }
}

## Get list of html files in VSTP directories
@tmp = `cd /home/rossim/html/3dtool/VSTP/; ls */VSTP_*/*.html`;
foreach( @tmp ) {
    if ( /(DV_\d*).*(VSTP_\S*)/ ) {
	$html_file[$i] = "$1 $2";
	print "$html_file[$i]\n";

### Search through html file
	open html, "$_";
	while( <html> ) {
	    $record = $_;

#### Look for shape file
	    $j = 0;
	    foreach( @shape_file ) {
		if ( $record =~ /src\s*=\s*"(.*$_)/ ) {
		    print "$1\n";
		    $nuse[$j]++
		}
		$j++;
	    }

	}
	close html;

	$i++;
    }
}

## Pring shape file usage
$j = 0;
foreach( @shape_file ) {
    print "$nuse[$j] $_\n";
    $j++;
}
