#!/bin/bash

installSQLiteDatabase()
{
	if [[ $CFG_REQUIREMENT_DATABASE == "true" ]]; then
        # Safeguard loading
        if [ ! -e "$docker_dir/$db_file" ]; then
            if command -v sqlite3 &> /dev/null; then
                isHeader "Setup SQLite Database"

                # Create SQLite database file
                if [ ! -e "$docker_dir/$db_file" ]; then
                    local result=$(runInstallOp touch $docker_dir/$db_file)
                    checkSuccess "Creating SQLite $db_file file"

                    local result=$(runInstallOp chmod 755 $docker_dir/$db_file)
                    checkSuccess "Changing permissions for SQLite $db_file file"
                fi

                databaseCreateTables;

                # Get list of table names from database
                sql_table_names=$(sqlite3 "$docker_dir/$db_file" ".tables")

                # Loop through table names and print the desired text
                for sql_table_name in $sql_table_names; do
                    isSuccessful "Table $sql_table_name found in database."
                done
            fi
        else
            # Make sure tables are always setup
            databaseCreateTables;
        fi
    fi
}
