// ==============================================
// Zboard Config Utility
// Copyright (C) 2007 Matt Razza
// ----------------------------------------------
// This file manages and interfaces with the
// processes.
// ==============================================

#pragma once
#include "zboard-inc.h"

// Linux Process Info
class linuxProcInfo
{
	public:
		static bool isRunning(std::string procName); //Check if a given application is running
		static std::string getProcName(unsigned int pid); //Gets a proc name from it's pid
		static void printRunning(); //Prints the running processes
		static int getPID(std::string procName); //Gets the PID for a given program
		static bool killPID(unsigned int pid); //Kills a PID
	
	private:
		static std::string itos(unsigned int iVar); //Convers an int to a str
		static int stoi(std::string sVar); //Converts a std::string to an int
};

// Linux File Info
class linuxFileInfo
{
	public:
		// Check if the path is a directory
		static bool isDirectory(const std::string& strPath)
		{
			return S_ISDIR(getMode(strPath));
		}
		
		// Check if the path is a regular file
		static bool isRegularFile(const std::string& strPath)
		{
			return S_ISREG(getMode(strPath));
		}
		
		// Checks if a file exists
		static bool fileExists(const std::string& strFile)
		{
			std::ifstream fin;
			fin.open(strFile.c_str()); //Attempt to open the file for reading
			
			if (fin.is_open()) //If it opened, it's there
			{
				fin.close();
				return true;
			}

			return false; //It didn't open, it's not there
		}
	
	private:
		// Get the type of a path
		static mode_t getMode (const std::string& strPath)
		{
			struct stat output;
			if (0 > lstat(strPath.c_str(), &output))
			{
				printf("\n\033[1;31m[Internal Error]\033[0m\n");
				printf("Please send the following output to the developer...\n\n");
				printf("vr:     %i:%i:%i\n", verMajor, verMinor, verRevision);
				printf("spat:   %s\n", strPath.c_str());
				printf("oxp:    lfiGM\n");
			}
			
			return output.st_mode;
		}
};
