// Spectre Samba Mounter // // Douglas Thrift // // Spectre.cpp #include "Spectre.h" #include "Maker.h" #include "Mounter.h" string program; string programName = "Spectre Samba Mounter"; string programVersion = "1.0alpha"; bool debug = false; Config config; int main(int argc, char* argv[]) { program = argv[0]; #include "configure.h" bool make = false; bool mount = false; set makes; set mounts; for (int index = 1; index < argc; index++) { string arg = argv[index]; if (arg == "-help") { usage(); return 0; } else if (arg == "-version") { version(); return 0; } else if (arg == "-make") { if (!make) make = true; if (++index < argc) { makes.insert(argv[index]); } else { cerr << program << ": Bad arguments\n"; usage(); return 1; } } else if (arg == "-mount") { if (!mount) mount = true; if (++index < argc) { mounts.insert(argv[index]); } else { cerr << program << ": Bad arguments\n"; usage(); return 1; } } else if (arg == "-D") { if (!debug) { debug = true; cerr.setf(ios_base::boolalpha); } } } if (!make && !mount) { usage(); return 0; } if (debug) { cerr << "make = " << make << "\n" << (make ? "makes = {\n" : ""); for (set::iterator itor = makes.begin(); itor != makes.end(); itor++) { cerr << " " << *itor << "\n"; } cerr << (make ? "}\n" : "") << "mount = " << mount << "\n" << (mount ? "mounts = {\n" : ""); for (set::iterator itor = mounts.begin(); itor != mounts.end(); itor++) { cerr << " " << *itor << "\n"; } cerr << (mount ? "}\n" : "") << "config.install = " << config.install << "\n"; } configure(); if (debug) { cerr << "config.smbclient = " << config.smbclient << "\n" << "config.mount = " << config.mount << "\n" << "config.root = " << config.root << "\n" << (!config.hosts.empty() ? "config.hosts = {\n" : ""); for (multimap::iterator itor = config.hosts.begin(); itor != config.hosts.end(); itor++) { cerr << " " << itor->first << " = " << itor->second << "\n"; } cerr << (!config.hosts.empty() ? "}\n" : ""); } if (make) { for (set::iterator itor = makes.begin(); itor != makes.end(); itor++) { Maker maker(*itor); maker.make(); } } if (mount) { for (set::iterator itor = mounts.begin(); itor != mounts.end(); itor++) { Mounter mounter(*itor); mounter.mount(); } } return 0; } string platform() { utsname* computer = new utsname; uname(computer); string os = computer->sysname; string version = computer->release; string architecture = computer->machine; delete computer; string platform = "(" + os + " " + version + " " + architecture + ")"; return platform; } void usage() { cout << "Usage: " << program << " [-make host ...] [-mount host ...] [-D] " << "[-version] [-help]\n" << "Options:\n" << " -make host Make the mount tree for host\n" << " -mount host Mount the shares on host to its tree\n" << " -D Display debug information\n" << " -version Display version information and exit\n" << " -help Display this message and exit\n"; } void version() { cout << programName << " " << programVersion << " "<< platform() << "\n\n" << " Copyright (C) 2003, Douglas Thrift. All Rights Reserved.\n\n" << " This product includes software developed by Douglas Thrift\n" << " (http://computers.douglasthrift.net/).\n"; } void configure() { string conf = config.install + "/conf/spectre.conf"; ifstream fin(conf.c_str()); if (!fin.is_open()) { cerr << program << ": Could not open " << conf << "\n"; exit(1); } do { string line; getline(fin, line); if (line.find('#') == 0 || line == "") { // ignore } else if (line.find("smbclient=") == 0) { config.smbclient = line.substr(10); } else if (line.find("mount=") == 0) { config.mount = line.substr(6); } else if (line.find("root=") == 0) { config.root = line.substr(5); } else if (line.find("host=") == 0) { string host = line.substr(5); getline(fin, line); if (line != "{") { cerr << program << ": Munged configuration for " << host << "\n"; exit(1); } do { getline(fin, line); if (line.find("\t#") == 0) { // ignore } else if (line.find('\t') == 0) { config.hosts.insert(pair(host, line.substr(1))); } else if (line == "}") { break; } } while (fin.good()); } } while (fin.good()); fin.close(); }