# sql2tbl.sh	20250510



todos=`sqlite3 eidogo.db3 "select * from tourney"`

htmlfile="tourney.html"

printf "<html>\n" >$htmlfile
printf "<head>\n" >$htmlfile
printf "<title>tourney.html $dttm</title>\n" >$htmlfile
printf "	</head>\n" >$htmlfile
printf "<body bgcolor=linen>\n" >> $htmlfile
printf "<div id=eidogo_tourney>\n" >> $htmlfile
printf "<h1>eidogo_tourney</h1>\n" >> $htmlfile
printf "<hr color=goldenrod>\n" >> $htmlfile
printf "<div id=div_sgf>\n" >> $htmlfile
printf "	<h2>div_sgf</h2>\n" >> $htmlfile
printf "	<div id=div_result>\n" >> $htmlfile
printf "		result\n" >> $htmlfile
printf "		</div id=div_result_end>\n" >> $htmlfile
printf "	</div id=div_sgf_end>\n" >> $htmlfile
printf "<hr color=goldenrod>\n" >> $htmlfile



printf "<table id='table_tourney'>\n" >> $htmlfile
printf "<hr color=goldenrod>\n" >> $htmlfile
# printf "<tr><th>idx</th><th>id</th><th>date</th><th>event</th><th>white</th><th>black</th><th>result</th><th>id2</th><th>sgf</th></tr>\n" >> $htmlfile
	printf "<tr class='oddeven'>\n" >> $htmlfile
	printf "<td width=10>idx</td>\n" >> $htmlfile
	printf "<td width=50>id</td>\n" >> $htmlfile
	printf "<td width=100>dt</td>\n" >> $htmlfile
	printf "<td width=600>event</td>\n" >> $htmlfile
	printf "<td width=200>white</td>\n" >> $htmlfile
	printf "<td width=200>black</td>\n" >> $htmlfile
	printf "<td width=50>result</td>\n" >> $htmlfile
	printf "<td width=100>id2</td>\n" >> $htmlfile
	printf "<td width=200>sgf</td>\n" >> $htmlfile
	printf "</tr>\n" >> $htmlfile


j=0;k=0;
OLDIFS=$IFS;
IFS=$'\n';
for todo in $todos; do
	let j=j+1;
	idx=`echo $todo | awk -F"|" '{ printf $1 }'`
	id=`echo $todo | awk -F"|" '{ printf $2 }'`
	dt=`echo $todo | awk -F"|" '{ printf $3 }'`
	event=`echo $todo | awk -F"|" '{ printf $4 }'`
	white=`echo $todo | awk -F"|" '{ printf $5 }'`
	black=`echo $todo | awk -F"|" '{ printf $6 }'`
	result=`echo $todo | awk -F"|" '{ printf $7 }'`
	id2=`echo $todo | awk -F"|" '{ printf $8 }'`
	sgf=`echo $todo | awk -F"|" '{ printf $9 }'`

#	if [ -f sgf/$sgf ]; then sgf=`cat sgf/$sgf`; fi
	if [ -f $id2 ]; then 
		sgf=`cat $id2`; 
#		printf "___FOUND: idx=$idx id=$id id2=$id2 sgf=$sgf\n"
		sqlite3 eidogo.db3 "update tourney set sgf='$sgf' where id='$id' and id2='$id2'"
#	else
#		printf "NOTFOUND: idx=$idx id=$id id2=$id2\n"
		fi



	if (( j % 2 == 0 )); then
#		echo "$n is even"
		oddeven="even";
	else
#		echo "$n is odd"
		oddeven="odd";
		fi


#	if [ $((j%2)) -eq 0 ]; then
#		echo "even";
#		oddeven="even";
#	else
#		echo "odd";
#		oddeven="odd";
#		fi

	printf "$j $oddeven $idx id=$id dt=$dt event=$event black=$black white=$white result=$result id2=$ids sgf=sgf..\n"
	printf "$j $ids id=$id id2=$id2 sgf=sgf..\n";

	printf "<tr class='$oddeven'>\n" >> $htmlfile
	printf "<td width=10><a href='./#titles/$id'>$idx</a></td>\n" >> $htmlfile
	printf "<td width=50><a href='./#titles/$id'>$id</a></td>'\n" >> $htmlfile
	printf "<td width=100><a href='./#titles/$id'>$dt</a></td>\n" >> $htmlfile
	printf "<td width=600><a href='./#titles/$id'>$event</a></td>\n" >> $htmlfile
	printf "<td width=200><a href='./#titles/$id'>$white</a></td>\n" >> $htmlfile
	printf "<td width=200><a href='./#titles/$id'>$black</a></td>\n" >> $htmlfile
	printf "<td width=50><a href='./#titles/$id'>$result</a></td>\n" >> $htmlfile
	printf "<td width=100><a href='$id2'>$id2</a></td>\n" >> $htmlfile
	if [ $sgf == "sgf" ]; then
#		printf "<td width=200><a href='./#titles/$id'>$sgf</a></td>\n" >> $htmlfile
		printf "<td width=200>$sgf</td>\n" >> $htmlfile
	else
#		sgftxt=`echo $sgf | cut -c1-10`
#		printf "<td width=200><a href='$id2'>id2</a></td>\n" >> $htmlfile
		printf "<td width=200><a href=# onclick=\"readfile('$id2'); return false;\">$id2</a></td>\n" >> $htmlfile
		fi
	printf "</tr>\n" >> $htmlfile

	done

	IFS=$OLDIFS;
	
			
printf "	</table>\n" >> $htmlfile

printf "<script id=script_readfile>\n" >> $htmlfile
printf "       function readfile(filename) {\n" >> $htmlfile
printf "                if(( filename == 'id2' ) || ( filename == '' )) {\n" >> $htmlfile
printf "                        document.getElementById('div_result').innerHTML='';\n" >> $htmlfile
printf "                        return;\n" >> $htmlfile
printf "                        }\n" >> $htmlfile
printf "                var client = new XMLHttpRequest();\n" >> $htmlfile
printf "                client.open('GET', filename);\n" >> $htmlfile
printf "                client.onreadystatechange = function() {\n" >> $htmlfile
printf "                        document.getElementById('div_result').innerHTML=client.responseText;\n" >> $htmlfile
printf "//                      alert(client.responseText);\n" >> $htmlfile
printf "                        }\n" >> $htmlfile
printf "                client.send();\n" >> $htmlfile
printf "                }\n" >> $htmlfile
printf "        <script id=script_readfile_end>\n" >> $htmlfile




# printf "<script id=script_readfile1>\n" >> $htmlfile
# printf "        function readfile1(filename) {\n" >> $htmlfile
# printf "                var reader = new FileReader();\n" >> $htmlfile
# printf "                reader.onload = function(event) {\n" >> $htmlfile
# printf "                        const result = event.target.result;\n" >> $htmlfile
# printf "                        document.body.innerHTML += '<p>' + result + '</p>';\n" >> $htmlfile
# printf "			document.getElementById('div_result').innerHTML=result;\n" >> $htmlfile
# printf "                        };\n" >> $htmlfile
# printf "                reader.readAsText(new File([filename], 'text/plain'));\n" >> $htmlfile
# printf "                }\n" >> $htmlfile
# printf "        </script id=script_readfile1_end>\n" >> $htmlfile



printf "	</body>\n" >> $htmlfile
printf "	</html>\n" >> $htmlfile
