#!/bin/sh

DBASE="stream.db"
WWW=~/WorkSpace/STREAM/MASTER/WebSite
export LC_ALL=C
AWK=awk

# This script splits the full database file "stream.db" into several
# subsets for subsequent processing.

# The processing includes:
#     calling the "Make_Tables" script
#     calling the "Memory_Types/Build_balance_graphs" script

# First, extract and process the "standard" set.
#     This excludes 4-byte results, "experimental/coded/simulated"
#     results and "obsolete/old/superceded" results.

echo "Extracting Standard dataset to standard.db"
$AWK -F, '$3 == 8 && $14 == "S" && $15 == "N" && $16 != "C" {print $0}' <$DBASE >standard.db
echo "Make_Tables...."
./Make_Tables standard.db

echo "Extracting Tuned dataset to tuned.db"
$AWK -F, '$3 == 8 && $14 == "T" && $15 == "N" {print $0}' <$DBASE >tuned.db
echo "Make_Tables...."
./Make_Tables tuned.db

#$AWK -F, '$3 == 8 && $14 == "S" && $15 == "N" {print $0}' <$DBASE | sort -t, --key=+6 -nr >std_sort.db
#./Make_Tables std_sort.db

# Second, extract and process the "experimental/coded/simulated" set.

echo "Extracting experimental dataset to experimental.db"
$AWK -F, '$14 == "E" && $15 == "N" {print $0}' <$DBASE >experimental.db
echo "Make_Tables...."
./Make_Tables experimental.db

#$AWK -F, '$3 == 8 && $14 == "E" && $15 == "N" {print $0}' <$DBASE | sort -t, --key=+6 -nr >exp_sort.db
#./Make_Tables exp_sort.db

# Third, extract and process the "obsolete/old/superceded" set.

echo "Extracting obsolete dataset to obsolete.db"
$AWK -F, '$15 == "Y" {print $0}' <$DBASE >obsolete.db
echo "Make_Tables...."
./Make_Tables obsolete.db

#$AWK -F, '$3 == 8 && $14 == "S" && $15 == "N" {print $0}' <$DBASE | sort -t, --key=+6 -nr >obs_sort.db
#./Make_Tables obs_sort.db

# Fourth, extract and process the 4-byte results.

echo "Extracting 4-byte dataset to thirty-two.db"
$AWK -F, '$3 == 4 {print $0}' <$DBASE >thirty-two.db
echo "Make_Tables...."
./Make_Tables thirty-two.db

#$AWK -F, '$3 == 8 && $14 == "S" && $15 == "N" {print $0}' <$DBASE | sort -t, --key=+6 -nr >32b_sort.db
#./Make_Tables 32b_sort.db


# Fifth, extract and process the peecee results
#     This excludes 4-byte results, "experimental/coded/simulated"
#     results and "obsolete/old/superceded" results.

echo "Extracting peecee dataset to peecee.db"
$AWK -F, '$3 == 8 && $14 == "S" && $15 == "N" && $16 == "P" {print $0}' <$DBASE >peecee.db
echo "Make_Tables...."
./Make_Tables peecee.db


# Sixth, extract and process the mac results
#     This excludes 4-byte results, "experimental/coded/simulated"
#     results and "obsolete/old/superceded" results.

echo "Extracting mac dataset to mac.db"
$AWK -F, '$3 == 8 && $14 == "S" && $15 == "N" && $16 == "M" {print $0}' <$DBASE >mac.db
echo "Make_Tables...."
./Make_Tables mac.db

# Seventh, extract and process the MPI results
#     This excludes 4-byte results, "experimental/coded/simulated"
#     results and "obsolete/old/superceded" results.
#     Field 16 uses "C" for "Cluster"

echo "Extracting MPI dataset to MPI.db"
$AWK -F, '$3 == 8 && $14 == "S" && $15 == "N" && $16 == "C" {print $0}' <$DBASE >MPI.db
echo "Make_Tables...."
./Make_Tables MPI.db


# Eighth, make the list of the "Top 20" Single address space results.
#    Extract only SMP results to "tmp.db", then
#    manually to limit this to one entry per system.

rm -f tmp.db
echo "Extracting Top20 to top20.db"
$AWK -F, '$11 == "N" {print $0}' <stream.db | sort -t , -nr --key=+6 >tmp.db
echo "Remember to manually create the Top20 database from tmp.db and rebuild"

echo ./Make_Tables top20.db


#     Now call the "Memory_Types/Build_balance_graphs" script
#
#cd Memory_Types
#Build_balance_graphs
#cd ..
#cp Memory_Types/All.gif $WWW/hpc/balance/mflops_vs_balance.gif


# Now list the most recent 30 results

echo "Making list of last 30 entries...."

echo "<html>" >$WWW/new_30.html
echo "<title>Newest 30 STREAM Results</title>"  >>$WWW/new_30.html
echo "<body BGCOLOR=#FFFFFF>"  >>$WWW/new_30.html
echo "<h1>Newest 30 STREAM Results</h1>"  >>$WWW/new_30.html
echo "<pre>"  >>$WWW/new_30.html
echo "  Date     NCPUS    System" >>$WWW/new_30.html
echo "--------------------------------------------" >>$WWW/new_30.html
sort -t, --key=+17 -r stream.db | awk -F, '{printf("%10.10s %5.5s    %s\n",$17,$2,$1)}' | head -30  >>$WWW/new_30.html
echo "--------------------------------------------" >>$WWW/new_30.html
echo "</body>"  >>$WWW/new_30.html
echo "</html>"  >>$WWW/new_30.html

# Next build the table with *all* results, sorted by date 
echo "Sorting all results for presentation by date of submission ...."
sort -t, --key=+17 -r stream.db >by_date.db
echo "Make_Tables...."
./Make_Tables by_date.db

# Next build the table with *all* results, sorted by name 
echo "Sorting all results alphabetically by Machine ID ...."
sort -f -t, --key=+1 stream.db >by_name.db
echo "Make_Tables...."
./Make_Tables by_name.db

