#!/usr/local/bin/python
import sys
import sqlite3
conn = sqlite3.connect("/home/TEMP/Databases/NED.db")
conn.text_factory = str
c = conn.cursor()
if len(sys.argv) < 3:
    print "\n*****************************************"
    print "Not enough options were passed."     
    print "NED requires 2 arguments. the first -H , -R , -I , -D or -S .\nThe second can be a period."
    print "If printing the database -T also add a filename of your choice ( no quotes required ):"
    print " Example: NED -T Data2Text.txt"   
    print "If wanting to read all entries use -R . (use the period)" 
    print "even use the period with help.  -H .   must be entered."
    print "*****************************************\n"
    sys.exit()
mod = sys.argv[1]
def create():
    import sqlite3
    conn = sqlite3.connect("/home/TEMP/Databases/NED.db")
    conn.text_factory = str
    c = conn.cursor()
    c.execute("CREATE VIRTUAL TABLE PROJECT using FTS4 (input)")
    conn.commit()
    text = "Database Created"
    return text

def insert(data,conn=conn, c=c):
    c.execute("INSERT into PROJECT values (?)", (data,))
    for row in c.execute("SELECT ROWID,* FROM PROJECT ORDER BY ROWID DESC LIMIT 1"):
        print "\nPOST VERIFIED:\n",row[0],row[1]
    conn.commit()
    conn.close()
    return data

def search(data,conn=conn, c=c):
    for row in c.execute("SELECT ROWID,* FROM PROJECT WHERE input MATCH ?",(data,)):
        print "\nINFO Found Here:\n",row[0],row[1]
    conn.commit()
    conn.close()
    return data

def delete(rowid,conn=conn, c=c):
    c.execute("DELETE FROM PROJECT WHERE rowid = ?", (rowid,))
    conn.commit()
    conn.close()
    text = "ROWID "+rowid+" Deleted"
    return text

def main():
    conn = sqlite3.connect("/home/TEMP/Databases/NED.db")
    conn.text_factory = str
    c = conn.cursor()
    for row in c.execute("SELECT rowid, * FROM PROJECT"):
        print row[0],": ",row[1]
        
def Dsearch():
    conn = sqlite3.connect("/home/TEMP/Databases/NED.db")
    conn.text_factory = str
    search = raw_input("Detail Search: ")
    c = conn.cursor()
    for row in c.execute("SELECT rowid, * FROM PROJECT"):
        if search in row[1]:
            print row[0],": ",row[1] 

def prtmain(filename):
    fn = open(filename, "w")
    conn = sqlite3.connect("/home/TEMP/Databases/NED.db")
    conn.text_factory = str
    c = conn.cursor()
    for row in c.execute("SELECT rowid, * FROM PROJECT"):
        TEXT = "id:"+str(row[0])+"\n"+str(row[1])
        TEXT = str(TEXT)
        TEXT = TEXT.replace('\\n','\n')
        TEXT = "".join(TEXT)
        fn.write(TEXT+'\n----\n')

def HELP():
    TXT = """
    USE: NED argv[1] argv[2]
    argv[1] sets the mod:
    -I insert / -D delete / -R read / -H help
    examples:
    Notice the entry is in parenthese.
    -I  to insert "STUFF to be inserted"
    NED -I "STUFF to be inserted"
    -D to delete where rowid is 3
    NED -D 3
    Notice the period after -R . 
    -R . read all
    To search for the term "current project"
    NED -S current project
    -S "current project"
    To 'EXACT DETAIL' search" Notice the period after -DS
    NED -DS .
    -H help on options
    NED -H .
    """
    print TXT

if mod == "-H" or mod == "h":
    HELP()        
if mod == "-R":
    main()
if mod == "-I":
    data = sys.argv[2]
    insert(data)
if mod == "-D":
    rowid = sys.argv[2]
    delete(rowid) 
if mod == "-S":
    data = sys.argv[2]
    search(data)
if mod == "-DS":
    print ("Exact Text SEARCH.")
    Dsearch()
if mod == "-T":
    filename = sys.argv[2]
    prtmain(filename)
if mod == "-CREATE":
    create()
    print create
else:
    print "_________________\n"
    print sys.argv[2],"Command Completed"
    