// =============================================================================
// Zboard Config Utility
// Copyright (C) 2007  Matt Razza

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
// -------------------------------------------------------------------------------
// 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, bool bVerbose = false); //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(bool bVerbose = false); //Prints the running processes
		static int getPID(std::string procName, bool bVerbose = false); //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, bool bVerbose = false)
		{
			return S_ISDIR(getMode(strPath, bVerbose));
		}

		// Check if the path is a regular file
		static bool isRegularFile(const std::string& strPath, bool bVerbose = false)
		{
			return S_ISREG(getMode(strPath, bVerbose));
		}

		// 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, bool bVerbose = false)
		{
			struct stat output;
			if (0 > lstat(strPath.c_str(), &output))
			{
                if (bVerbose)
                {
                    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;
		}
};
