Code: Select all
#include <dirent.h>
#include <sys/stat.h>
static void
InstallFromDir (char *dirName, char *protocol, char *settingsFile)
{ // scan system for new plugin specs in given directory
DIR *dir;
struct dirent *dp;
struct stat statBuf;
time_t lastSaved = 0;
char buf[1024], *p;
if(!stat(settingsFile, &statBuf)) lastSaved = statBuf.st_mtime;
snprintf(buf, 1024, "%s/%s", dirName, protocol);
if(!(dir = opendir(buf))) return;
while( (dp = readdir(dir))) {
time_t installed = 0;
if(!strstr(dp->d_name, ".eng")) continue; // to suppress . and ..
snprintf(buf, 1024, "%s/%s/%s", dirName, protocol, dp->d_name);
if(!stat(buf, &statBuf)) installed = statBuf.st_mtime;
if(lastSaved == 0 || (int) (installed - lastSaved) > 0) { // first time we see it
FILE *f = fopen(buf, "r");
if(f) { // read the plugin-specs
char engineCommand[1024], engineDir[1024], variants[1024];
char bad=0, dummy, *engineCom = engineCommand;
int major, minor;
if(fscanf(f, "plugin spec %d.%d%c", &major, &minor, &dummy) != 3 ||
fscanf(f, "%[^\n]%c", engineCommand, &dummy) != 2 ||
fscanf(f, "%[^\n]%c", variants, &dummy) != 2) bad = 1;
fclose(f);
if(bad) continue;
// uncomment following two lines for chess-only installs
// if(!(p = strstr(variants, "chess")) ||
// p != variants && p[-1] != ',' || p[5] && p[5] != ',') continue;
// split off engine working directory (if any)
strcpy(engineDir, "");
if(sscanf(engineCommand, "cd %[^;];%c", engineDir, &dummy) == 2)
engineCom = engineCommand + strlen(engineDir) + 4;
InstallNewEngine(engineCom, engineDir, variants, protocol);
}
}
}
closedir(dir);
}
static void
AutoInstallProtocol (char *settingsFile, char *protocol)
{ // install new engines for given protocol (both from package and source)
InstallFromDir("/usr/local/share/games/plugins", protocol, settingsFile);
InstallFromDir("/usr/share/games/plugins", protocol, settingsFile);
}
void
AutoInstall (char *settingsFile)
{ // install all new XBoard and UCI engines
AutoInstallProtocol(settingsFile, "xboard");
AutoInstallProtocol(settingsFile, "uci");
}