Files
itgmania212121/stepmania/PBProject/Installer/BuildInstaller.cpp
T

166 lines
3.8 KiB
C++
Raw Normal View History

2003-09-08 20:20:57 +00:00
/*
* BuildInstaller.cpp
* stepmania
*
* Created by Steve Checkoway on Sun Sep 07 2003.
* Copyright (c) 2003 Steve Checkoway. All rights reserved.
*
*/
using namespace std;
2003-09-10 02:42:49 +00:00
#include <unistd.h>
#include <sys/param.h>
#include <sys/wait.h>
2003-09-08 20:20:57 +00:00
#include <cstdio>
#include <cstdlib>
#include <cstring>
2003-09-08 20:20:57 +00:00
#include <stack>
#include <map>
#include <sys/errno.h>
#include "StdString.h"
#include "InstallerFile.h"
#include "Processor.h"
#include "Util.h"
2003-09-08 20:20:57 +00:00
2003-09-10 02:42:49 +00:00
void HandleFile(const CString& file, const CString& dir, const CString& archivePath, bool overwrite)
2003-09-08 20:20:57 +00:00
{
static bool archiveMade = false;
char path[] = "/usr/bin/tar";
2003-12-24 20:31:58 +00:00
char arg1[] = "-rvf";
char arg2[archivePath.length() + 1];
char arg3[] = "-C";
char arg4[dir.length() + 1];
char arg5[file.length() + 1];
if (!archiveMade)
2003-09-08 20:20:57 +00:00
{
arg1[1] = 'c';
2003-09-08 20:20:57 +00:00
archiveMade = true;
}
strcpy(arg2, archivePath);
strcpy(arg4, dir);
strcpy(arg5, file);
2003-12-24 20:31:58 +00:00
/*if (CallTool(path, arg1, arg2, arg3, arg4, arg5, NULL))
exit(-10);*/
int r;
if ((r = CallTool2(true, -1, -1, -1, path, arg1, arg2, arg3, arg4,
arg5, NULL)) != 0)
{
fprintf(stderr, "%s %s %s %s %s %s returned %d\n",
path, arg1, arg2, arg3, arg4, arg5, r);
exit(-10);
2003-12-24 20:31:58 +00:00
}
2003-09-08 20:20:57 +00:00
}
2003-09-10 02:42:49 +00:00
const CString GetPath(const CString& ID)
{
if (ID == "install path")
{
char cwd[MAXPATHLEN];
char path[MAXPATHLEN];
char *ptr;
size_t temp;
2003-09-10 02:42:49 +00:00
getwd(cwd);
printf("Enter a path (relative or absolute) to the install files\n"
"The current working directory is: %s\n"
"> ", cwd);
ptr = fgets(path, MAXPATHLEN, stdin);
if ((temp = strlen(path)))
{
if (path[temp - 1] == '\n')
2003-09-30 17:34:23 +00:00
path[temp - 1] = '\000';
}
2003-09-10 02:42:49 +00:00
ASSERT(ptr);
while (*ptr != '\000' && (*ptr == ' ' || *ptr == '\t'))
++ptr;
if (*ptr == '/')
return ptr;
return CString(cwd) + "/" + CString(ptr);
}
fprintf(stderr, "Unknown path command, return `.'\n");
return ".";
}
2003-09-08 20:20:57 +00:00
void PrintUsage(int err)
{
printf("usage: BuildInstaller config [dir]\n\n"
"config: The configuration file for the installer.\n"
"dir: The output directory. It is created if it doesn't exist\n");
exit(err);
}
int main(int argc, char *argv[])
{
CString inFile, outDir;
switch (argc)
{
case 1:
printf("BuildInstaller needs to be run with at least one argument.\n");
PrintUsage(1);
case 2:
inFile = argv[1];
outDir = ".";
break;
case 3:
inFile = argv[1];
outDir = argv[2];
break;
default:
printf("BuildInstaller takes at most 2 arguments.\n");
PrintUsage(2);
}
if (inFile == "help" || inFile == "-h" || inFile == "--help")
PrintUsage(0);
2003-09-09 01:00:48 +00:00
outDir += "/files";
2003-09-08 20:20:57 +00:00
InstallerFile config(inFile);
unsigned nextLine = 0;
if (!config.ReadFile())
{
printf("Couldn't read config file, \"%s\".\n", inFile.c_str());
return 3;
}
if (mkdir_p(outDir))
2003-09-08 20:20:57 +00:00
{
printf("The mkdir_p call failed.\n");
2003-09-08 20:20:57 +00:00
return 4;
}
CString archivePath = outDir + "/archive.tar";
2003-09-10 02:42:49 +00:00
Processor p(archivePath, HandleFile, GetPath, NULL, false);
2003-09-08 20:20:57 +00:00
while (nextLine < config.GetNumLines())
p.ProcessLine(config.GetLine(nextLine), nextLine);
2003-09-10 03:25:58 +00:00
printf("Compressing the archive.\n");
char toolPath[] = "/usr/bin/gzip";
if (CallTool(toolPath, archivePath.c_str(), NULL))
{
printf("Gzip failed.\n");
return 6;
}
2003-09-09 01:00:48 +00:00
2003-09-08 20:20:57 +00:00
if (!config.WriteFile(outDir + "/config"))
{
printf("%s\n", strerror(errno));
return 7;
2003-09-08 20:20:57 +00:00
}
return 0;
}