#!/bin/bash

# *****************************************************
# PROGRAM:	bsb-maker
# PURPOSE:	CLI script To make Bullshit Bingo htmls
# AUTHOR:		vovchik (Puppy Linux forum)
# COMMENT:	html needs javascript activated in browser	
# DEPENDS:	bash
# PLATFORM:	Puppy Linux (actually, any *nix)
# DATE:		09-02-2010
# NOTES:		when creating your own word lists, keep
#			the terms short, so as to fit the html
#			buttons AND only use single quotes in
#			word lists, otherwise the script bombs
# *****************************************************


# *********************
# INIT VARS
# *********************

part3=" "
q='\"'
nl="
"
nl=$'\012'
ob="["
cb="]"
es=" = "
sc=";"
qq='"'
my_java_start="$nl<!--$nl"
my_java_end="
   //-->
   "
my_words_file="$1"
my_words_file_base=`basename "$my_words_file"`
my_java_start="$nl<!--$nl"
my_java_end="
   //-->
   "
new_html_file="bs-"${my_words_file_base%%.*}".html"
my_wordlines_index=0
my_topic=${my_words_file_base%%.*}  
my_topic=`echo $my_topic| tr "[a-z]" "[A-Z]"`


# *********************
# END INIT VARS
# *********************


# *********************
# FUNCTIONS
# *********************

# ------------- 
function check_parms()
# -------------
{
	if [ -f "$1" ] ; then
		echo "OK, the word list file seems to exist. We can proceed!"
	else
		echo
		echo "This script generates javascript-based html"
		echo "for playing Bullshit Bingo offline. You need"
		echo "to supply as an argument an existing word list"
		echo "file containing one term per line. This file"
		echo "should not contain any double quotes. If you"
		echo "want to use quotations, use single quotes in"
		echo "your own lists. Have fun."
		echo
		echo "USAGE: bsb-maker textfile.txt"
		echo
		echo "Example: bsb-maker wordlists/cliche.txt"
		echo
		exit 0
	fi
}

# this function assembles the html page that includes html, javascript and 
# words from a user-supplied word list

# -------------
function make_html()
# -------------
{
# make part1 of html page	
	
part1="<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
<html>
<head>
  <meta name=\"generator\" content=
  \"HTML Tidy for Linux/x86 (vers 25 January 2008), see www.w3.org\">
  <title>Bullshit Bingo</title>
  <meta http-equiv=\"Content-Style-Type\" content=\"text/css\">
  <meta name=\"generator\" content=\"vim\">
  <meta http-equiv=\"content-type\" content=
  \"text/html; charset=utf-8\">
  <meta http-equiv=\"content-language\" content=\"en\">
  <meta name=\"author\" content=\"anonymous\">
  <meta name=\"description\" content=\"BULLSHIT BINGO\">
  <style type=\"text/css\">
  <!--
    input.narrow { width:130pt; height:20pt; font-size:11pt }
    input.normal { width:145pt; height:25pt; font-size:12pt }
    input.wide  { width:170pt; height:30pt; font-size:16pt }
    //-->
  </style>
  <script language=\"JavaScript1.2\" type=\"text/javascript\">

"

# make part3 of html page (reading word list and creating my_wordlines_index var,
# which is needed in initialized state by javascript in part2)

	while read line
	do
		part3="$part3   ""source_field""$ob""$my_wordlines_index""$cb""$es"$qq"$line"$qq"$sc""$nl"
		# echo $part3
		my_wordlines_index=$(($my_wordlines_index+1))
		
	done < "$my_words_file"

# make part2 - start of java script

part2="$nl$my_java_start$nl

var number_of_terms = $my_wordlines_index ;
source_field = new Array(number_of_terms);
  target_field  = new Array(16); 
  field =      new Array(4); 
  for (i=0; i<4; i++)          
  {
    field[i] = new Array(4);
  }
  function init()
  {
    for (i=0; i<4; i++) 
    {
      for (j=0; j<4; j++)
      {
        field[i][j]=false; 
      }
    }
    for (i=0; i<16; i++)
    {
      target_field[i]=\"not yet defined\";
    }

"

# make part3 - end of java script and normal html stuff 

part4="
    var length_of_source = source_field.length - 1;
    var length_of_target   = 0;
    var random_source = 0;
    while (length_of_target < 16)
    {
      random_source = Math.floor(Math.random()*length_of_source+0.5);

      target_field[length_of_target]=source_field[random_source];
      for (i=random_source; i<length_of_source; i++)
      {
        source_field[i]=source_field[i+1];
      }
      length_of_target++;
      length_of_source--;
      source_field = source_field.slice(0,-1);
    }
    for (i=0; i<16; i++)
    {
      document.my_fields.a[i].value = target_field[i];
    }
  }
  function alarm()
  {
    alert (\"Good going! You seem to have\na nose for Bullshit !!!\");
  }
  function do_check(field_number)
  {
    var d = new Date();
    var curr_hour = d.getHours();
    var curr_min = d.getMinutes();
    var curr_sec = d.getSeconds();
    var curr_msec = d.getMilliseconds();
    var x=field_number%4;
    var y=Math.floor(field_number/4);
    document.my_fields.a[field_number].value = (\"Time: \" + curr_hour + \":\" + curr_min + \":\" + curr_sec);
    field[x][y]=1;
    if ( ( field[0][0] && field[1][1] && field[2][2] && field[3][3] ) ||
         ( field[3][0] && field[2][1] && field[1][2] && field[0][3] ) )
    {
      alarm();
      init()
    }
    for (i=0; i<4; i++)
    {
      if ( ( field[0][i] && field[1][i] && field[2][i] && field[3][i] ) ||
           ( field[i][0] && field[i][1] && field[i][2] && field[i][3] ) )
      {
        alarm();
        init()
      }
    }  
  }

$nl$my_java_end$nl
  </script>
</head>

<body onload=\"init()\" link=\"#00FF00\" vlink=\"#FFFFFF\">
  <basefont size=\"3\">
  <table border=\"0\" width=\"100%\">
    <tbody>
      <tr>
        <td align=\"center\">
          <noscript>Please activate javascript in your
          browser!</noscript>
          <h1>BULLSHIT BINGO</h1>
          <center>
          <h3>(BS Category: $my_topic)</h3>
            <table border=\"0\" cellpadding=\"1\" cellspacing=\"1\"
            width=\"90%\">
              <tbody>
                <tr>
                  <td align=\"center\"><b>Rules</b><br>
                  <br><i>
                  Click on a term when you hear it in a meeting or
                  on TV. Once you have clicked off four terms
                  either horizontally, vertically or diagonally,
                  stand up and shout:</i></td>
                </tr>
                <tr>
                  <td align=\"center\">
                    <b><br></b>
                    <h2><b><font color=\"#ff00f00\">B U L L S H I T !!!</font></b></h2>
                    <br>
                  </td>
                </tr>
              </tbody>
            </table>
            <form name=\"my_fields\" action=\"javascript:null;\"
            method=\"post\">
              <table bgcolor=\"#FF0000\" border=\"0\" cellpadding=\"0\"
              cellspacing=\"1\" width=\"480\">
                <tbody>
                  <tr>
                    <td colspan=\"6\" bgcolor=\"#FF0000\" height=\"5\">
                    &nbsp;</td>
                  </tr>
                  <tr>
                    <td align=\"center\" bgcolor=\"#FF0000\" width=\"5\">
                    &nbsp;</td>
                    <td align=\"center\"><input class=\"normal\" name=
                    \"a\" value=\"-\" onclick=\"do_check(0)\" height=\"22\"
                    type=\"button\" width=\"145\"></td>
                    <td align=\"center\"><input class=\"normal\" name=
                    \"a\" value=\"-\" onclick=\"do_check(1)\" height=\"22\"
                    type=\"button\" width=\"145\"></td>
                    <td align=\"center\"><input class=\"normal\" name=
                    \"a\" value=\"-\" onclick=\"do_check(2)\" height=\"22\"
                    type=\"button\" width=\"145\"></td>
                    <td align=\"center\"><input class=\"normal\" name=
                    \"a\" value=\"-\" onclick=\"do_check(3)\" height=\"22\"
                    type=\"button\" width=\"145\"></td>
                    <td bgcolor=\"#FF0000\" width=\"5\">&nbsp;</td>
                  </tr>
                  <tr>
                    <td align=\"center\" bgcolor=\"#FF0000\" width=\"5\">
                    &nbsp;</td>
                    <td align=\"center\"><input class=\"normal\" name=
                    \"a\" value=\"-\" onclick=\"do_check(4)\" height=\"22\"
                    type=\"button\" width=\"145\"></td>
                    <td align=\"center\"><input class=\"normal\" name=
                    \"a\" value=\"-\" onclick=\"do_check(5)\" height=\"22\"
                    type=\"button\" width=\"145\"></td>
                    <td align=\"center\"><input class=\"normal\" name=
                    \"a\" value=\"-\" onclick=\"do_check(6)\" height=\"22\"
                    type=\"button\" width=\"145\"></td>
                    <td align=\"center\"><input class=\"normal\" name=
                    \"a\" value=\"-\" onclick=\"do_check(7)\" height=\"22\"
                    type=\"button\" width=\"145\"></td>
                    <td bgcolor=\"#FF0000\" width=\"5\">&nbsp;</td>
                  </tr>
                  <tr>
                    <td align=\"center\" bgcolor=\"#FF0000\" width=\"5\">
                    &nbsp;</td>
                    <td align=\"center\"><input class=\"normal\" name=
                    \"a\" value=\"-\" onclick=\"do_check(8)\" height=\"22\"
                    type=\"button\" width=\"145\"></td>
                    <td align=\"center\"><input class=\"normal\" name=
                    \"a\" value=\"-\" onclick=\"do_check(9)\" height=\"22\"
                    type=\"button\" width=\"145\"></td>
                    <td align=\"center\"><input class=\"normal\" name=
                    \"a\" value=\"-\" onclick=\"do_check(10)\" height=
                    \"22\" type=\"button\" width=\"145\"></td>
                    <td align=\"center\"><input class=\"normal\" name=
                    \"a\" value=\"-\" onclick=\"do_check(11)\" height=
                    \"22\" type=\"button\" width=\"145\"></td>
                    <td bgcolor=\"#FF0000\" width=\"5\">&nbsp;</td>
                  </tr>
                  <tr>
                    <td align=\"center\" bgcolor=\"#FF0000\" width=\"5\">
                    &nbsp;</td>
                    <td align=\"center\"><input class=\"normal\" name=
                    \"a\" value=\"-\" onclick=\"do_check(12)\" height=
                    \"22\" type=\"button\" width=\"145\"></td>
                    <td align=\"center\"><input class=\"normal\" name=
                    \"a\" value=\"-\" onclick=\"do_check(13)\" height=
                    \"22\" type=\"button\" width=\"145\"></td>
                    <td align=\"center\"><input class=\"normal\" name=
                    \"a\" value=\"-\" onclick=\"do_check(14)\" height=
                    \"22\" type=\"button\" width=\"145\"></td>
                    <td align=\"center\"><input class=\"normal\" name=
                    \"a\" value=\"-\" onclick=\"do_check(15)\" height=
                    \"22\" type=\"button\" width=\"145\"></td>
                    <td bgcolor=\"#FF0000\" width=\"5\">&nbsp;</td>
                  </tr>
                  <tr>
                    <td colspan=\"6\" bgcolor=\"#FF0000\" height=\"5\">
                    &nbsp;</td>
                  </tr>
                </tbody>
              </table>
            </form>
          </center>
        </td>
      </tr>
    </tbody>
  </table>
</body>
</html>
"

# concatenate all the html parts
new_html="$part1$part2$part3$part4"
}

# *********************
# END FUNCTIONS
# *********************


# *********************
# MAIN
# *********************

# check for input file as argument
check_parms "$my_words_file"
# assemble the html
make_html
# create new html file
echo "$new_html" > "$new_html_file"

# *********************
# END MAIN
# *********************

