// deployer IntegrationTests main
// Copyright (C) 2024 Jean-Cloud
// GNU General Public License v3

#include <iostream>
#include <fstream>
#include "BashManager.h"

using namespace std;

void Help()
{
    cout << "blabla" << endl;
}

//tests
void bashTest()
{
    //test of the execution of deploy.sh
    int res1=-1;
    ifstream deployResult("deployResult");
    if (!deployResult){
        cout << "The deployResult file cannot be opened. The Bash module must have failed at executing deploy.sh." << endl;
    } else {
        string user; //deploy.sh writes in deployResult the result of the whoami command
        getline(deployResult,user);
        if (user!="root"){
            cout << "The deploy.sh has not been executed with the right user." << endl;
        } else {
            res1=0;
        }
    }
    //test of the execution of deploy_user.sh
    //deploy_user.sh of test.sh8s.sh creates deployAsResult in the data_dir as the user associated with the service
    int res2=-1;
    //extract the data_dir path from the .env file 
    ifstream env ("services/test.sh8s.sh/.env");
    if (!env){
        cout << ".env file non-existent. The deployer must have failed to create it." << endl;
    } else {
        string line;
        string data_dir;
        size_t pos = string::npos;
        while (getline (env, line)){
            pos=line.find("data_dir=");
            if (pos!=string::npos){
                data_dir=line.substr(pos+9);
                break;
            }
        }
        if (data_dir.empty()){
            cout << "http_dir variable not defined in the .env. The deployer must have failed to initialize it." << endl;
        } else {
            //read the deployAsResult file
            string path=data_dir+"/deployAsResult";
            ifstream deployAsResult(path);
            if (!deployAsResult){
                cout << "The deployAsResult file cannot be opened. The Bash module must have failed at executing deploy_user.sh." << endl;
            } else {
                string user; //deploy.sh writes in deployAsResult the result of the whoami command
                getline(deployAsResult,user);
                if (user!="test.sh8s.sh"){
                    cout << "The deploy.sh has not been executed with the right user." << endl;
                } else {
                    res2=0;
                }
            }
        }
    }
    int res=res1+res2;
    if (res==0){
        cout << "Test of BashModule: OK" << endl;
    } else {
        cout << "Test of BashModule: FAIL" << endl;
    }
}

void dockerTest()
{
    cout << "Test of DockerModule" << endl;
}

void nginxTest()
{
    cout << "Test of Nginx module" << endl;
}

void wireguardTest()
{
    cout << "Test of WireguardModule" << endl;
}

void encryptionTest()
{
    cout << "Test of EncryptionModule" << endl;
}

int main(int argc, char **argv)
{
    if(argc!=2){
        cerr << "Invalid number of arguments." << endl;
    } else{
        BashManager::Execute("./deployer deploy test.sh8s.sh");
        string action=argv[1];
        if (action=="bash"){
            bashTest();
        } else if  (action=="docker"){
            dockerTest();
        } else if  (action=="nginx"){
            nginxTest();
        } else if  (action=="wireguard"){
            wireguardTest();
        } else if  (action=="encryption"){
            encryptionTest();
        } else if  (action=="all"){
            bashTest();
            dockerTest();
            nginxTest();
            wireguardTest();
            encryptionTest();
        } else {
            cerr << "Unknown argument." << endl;
            Help();
        }
    }
}