// used to compile            
//      g++ -o startup startup.cpp `mysql_config --cflags --libs`

// used to test memory
//      valgrind --track-origins=yes ./startup

#include <dirent.h>
#include <iostream>
#include </usr/include/mariadb/mysql.h>
#include </var/www/html/cpp/mysql.cpp>
#include "license.cpp"

// startup.cpp adds to an existing file apache2 unique Pi code, install date and license key
// program won't start without above data
// if trial expires temporary license key won't work
// when person registers license they connect to VPS, send the Pi serial number, create record in VPS database and generate
//      license key to send back to save to file

// how verification works
// startup reads serial number from Pi and reads file with license key then runs formula that created the license key to verify it matches
// if no match program won't start

// get pi serial number = cat /sys/firmware/devicetree/base/serial-number

// don't give out mysql.cpp file and have it silently authenticate license key every startup

using namespace std;


// void startup()
// {
// }

int main(void)//(int argc, char **argv)
{
    DIR *dir;
    struct dirent *ent;
    if ((dir = opendir("/var/www/html/updates/")) != NULL) {
        /* print all the files and directories within directory */
        while ((ent = readdir(dir)) != NULL) 
        {
            std::string name = ent->d_name;
            // std::cout << "name            = " << name << std::endl;
            // printf("%s\n", ent->d_name);
            // std::cout << "directories     = " << directories << std::endl;
            if (name == "robotank") 
            {
	            // std::cout <<"Found File ---------------------------- " << endl;
                
                std::string comm = "sudo mv /var/www/html/updates/robotank /var/www/html/cpp/";  // delete update folder and contents
                system(comm.c_str());

                comm = "sudo chown pi:www-data /var/www/html/updates/robotank";
                system(comm.c_str());

                comm = "sudo chmod 755 robotank";
                system(comm.c_str());
                
                connectMysql();
                std::string query = "UPDATE systemSettings SET updateRan = '2' WHERE userID = 1";
                mysql_query(conn, query.c_str()); // run query
                mysql_disconnect();
                break;
            }
        }
        closedir(dir);
    } 
    else {
    /* could not open directory */
        // perror("");
    }

    // std::cout << "directories     = " << directories << std::endl;




    // update.updateUpdateAvailable();
    // std::cout <<" -------------------------------------------------------- " << endl;
    // std::cout <<" -------------------------------------------------------- " << endl;
    // std::cout <<" -------------------------------------------------------- " << endl;
    // std::cout <<" -------------------------------------------------------- " << endl;
    // std::cout <<"START FINISHED ---------------------------- " << endl;
    // std::cout <<" -------------------------------------------------------- " << endl;
    // std::cout <<" -------------------------------------------------------- " << endl;
    // std::cout <<" -------------------------------------------------------- " << endl;
    // std::cout <<" -------------------------------------------------------- " << endl;
    // std::cout <<"" << endl;
    
    std::string comm = "sudo ./robotank";  // delete update folder and contents
    system(comm.c_str());
    return 0;

	// while(1){
    //     sleep(1);
	// }
}

