/*
    Copyright 2010 by BoscoWitch

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using GLib;
using Sqlite;


public class Test.HelloObject : GLib.Object {
    
    public static string res;
    
    public static int callback (int n_columns, string[] values,
                                string[] column_names)
    {
        counter++;
        string tmp = values[1]; 
	string tmp2 = values[2]; 
        
	if(tmp2.contains("["))
        	tmp2 = tmp2.slice(0,tmp2.pointer_to_offset(tmp2.rchr(-1,'['))-1);
	if(tmp2 == "")
		tmp2 = values[2];
        stdout.printf( "update entries set reading_stripped=\"%s\" where entryid=\"%s\";\n".printf(tmp2,values[0])); 

	if(tmp.contains(";"))
        	tmp = tmp.slice(0,tmp.pointer_to_offset(tmp.chr(-1,';')));
	if(tmp.contains(" "))
        	tmp = tmp.slice(0,tmp.pointer_to_offset(tmp.chr(-1,' ')));
	if(tmp.contains("("))
        	tmp = tmp.slice(0,tmp.pointer_to_offset(tmp.chr(-1,'(')));
	if(tmp == "")
		tmp = values[1];

        stdout.printf( "update entries set japanese_stripped=\"%s\" where entryid=\"%s\";\n".printf(tmp,values[0]));

        return 0;
    }


  public static string execute;
    public static int counter;
    
    
    public static int main(string[] args) {
    	

string exelink;
char exepath[1024];
exelink = "/proc/%d/exe".printf(Posix.getpid());

Posix.readlink(exelink,exepath);
string Path = (string)exepath;

Path = Path.slice(0,Path.pointer_to_offset(Path.rchr(-1,'/')));
//stderr.printf(Path);
  counter = 0;
    	Database db;
        int rc;
 
        if (!FileUtils.test ("%s/wadoku_Database.sqlite3".printf(Path), FileTest.IS_REGULAR)) {
            stderr.printf ("Database %s does not exist or is directory\n", args[1]);
            return -1;
        }
 
        rc = Database.open ("%s/wadoku_Database.sqlite3".printf(Path), out db);
         
        if (rc != Sqlite.OK) {
	    stderr.printf ("Can't open database: %d, %s\n", rc, db.errmsg ());
            return -1;
        }

    	stdout.printf( "CREATE TEMPORARY TABLE TEMP_TABLE(entryid, german, japanese, reading, romanjireading, wordtype);\nINSERT INTO TEMP_TABLE SELECT entryid, german, japanese, reading, romanjireading, wordtype FROM entries;\nDROP TABLE entries;\nCREATE TABLE entries (entryid INTEGER PRIMARY KEY, german TEXT, japanese TEXT, reading TEXT, reading_stripped TEXT, japanese_stripped TEXT, romanjireading TEXT, wordtype TEXT);\nINSERT INTO entries(entryid, german, japanese, reading, romanjireading, wordtype) SELECT entryid, german, japanese, reading, romanjireading, wordtype FROM TEMP_TABLE;\nDROP TABLE TEMP_TABLE;\n"); 
 
      rc = db.exec ("""select entryid,japanese,reading from entries order by LENGTH(entryid) asc;""", callback, null);
          
          if (rc != Sqlite.OK) {
         		stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
        	}
          

        return 0;
    }
    
}

