import os
import sqlite3
import subprocess

RPI_IMDB_DATABASE = "/var/lib/rpiWebApp/"
RPI_TSV_DIRECTORY = "/var/lib/imdb-rename/"
RPI_CSV_DIRECTORY = "/home/matt/"

MC_IMDB_DATABASE = "***REMOVED***"
MC_TSV_DIRECTORY = "***REMOVED***"
MC_CSV_DIRECTORY = "***REMOVED***"


IMDB_DATABASE = RPI_IMDB_DATABASE if os.path.exists(RPI_IMDB_DATABASE) else MC_IMDB_DATABASE
TSV_DIRECTORY = RPI_TSV_DIRECTORY if os.path.exists(RPI_TSV_DIRECTORY) else MC_TSV_DIRECTORY
CSV_DIRECTORY = RPI_CSV_DIRECTORY if os.path.exists(RPI_CSV_DIRECTORY) else MC_CSV_DIRECTORY


def create_csv_files():
    print("start create csv")
    subprocess.run(
        ["xsv", "input", "-d", "\t", "--no-quoting", "{}title.akas.tsv".format(TSV_DIRECTORY), "-o", "{}title_akas.csv".format(CSV_DIRECTORY)]
    )
    subprocess.run(
        ["xsv", "input", "-d", "\t", "--no-quoting", "{}title.basics.tsv".format(TSV_DIRECTORY), "-o", "{}title_basics.csv".format(CSV_DIRECTORY)]
    )
    subprocess.run(
        ["xsv", "input", "-d", "\t", "--no-quoting", "{}title.episode.tsv".format(TSV_DIRECTORY), "-o", "{}title_episode.csv".format(CSV_DIRECTORY)]
    )
    print("end create csv")


def import_csv_files():
    print("start import csv")
    f = open("import_csv.sql").read()
    sql_script = f.format(CSV_DIRECTORY)
    subprocess.run(["sudo", "-u", "http", "sqlite3", IMDB_DATABASE + "imdb.db"], input=sql_script.encode("utf8"))
    print("end import csv")


create_csv_files()
import_csv_files()
