CommandLine cmd;
try {
cmd = parser.parse(options, args);
} catch (ParseException e) {
throw new UsageException(e.getMessage());
}
type = null;
path = null;
if ((path = cmd.getOptionValue(SyncType.BACKUP.getName())) != null) {
type = SyncType.BACKUP;
} else if ((path = cmd.getOptionValue(SyncType.RESTORE.getName())) != null) {
type = SyncType.RESTORE;
} else if ((path = cmd.getOptionValue(SyncType.CLEAN.getName())) != null) {
type = SyncType.CLEAN;
} else if (cmd.hasOption(SyncType.LIST.getName())) {
type = SyncType.LIST;
}
String config = cmd.getOptionValue("config", "." + Item.SEPARATOR + "config" + Item.SEPARATOR + "cloudsync.config");
if (config.startsWith("." + Item.SEPARATOR)) {
config = System.getProperty("user.dir") + Item.SEPARATOR + config;
}
boolean configValid = config != null && new File(config).isFile();
prop = new Properties();
try {
prop.load(new FileInputStream(config));
} catch (final IOException e) {
configValid = false;
}
name = getOptionValue(cmd, "name", null);
remoteConnector = prop.getProperty("REMOTE_CONNECTOR");
passphrase = prop.getProperty("PASSPHRASE");
if (StringUtils.isEmpty(passphrase)) {
throw new CloudsyncException("'PASSPHRASE' is not configured");
}
String value = getOptionValue(cmd, "followlinks", LinkType.EXTERNAL.getName());
followlinks = LinkType.fromName(value);
value = getOptionValue(cmd, "duplicate", SyncType.CLEAN.equals(type) ? DuplicateType.RENAME.getName() : DuplicateType.STOP.getName());
duplicate = DuplicateType.fromName(value);
value = getOptionValue(cmd, "permissions", PermissionType.SET.getName());
permissions = PermissionType.fromName(value);
history = (type != null && type.equals("backup")) ? Integer.parseInt(getOptionValue(cmd, "history", "0")) : 0;
nocache = cmd.hasOption("nocache") || SyncType.CLEAN.equals(type);
forcestart = cmd.hasOption("forcestart");
testrun = cmd.hasOption("test");
String pattern = getOptionValue(cmd, "include", null);
if (pattern != null)
includePatterns = pattern.contains("|") ? pattern.split("\\|") : new String[] { pattern };
pattern = getOptionValue(cmd, "exclude", null);
if (pattern != null)
excludePatterns = pattern.contains("|") ? pattern.split("\\|") : new String[] { pattern };
if (!StringUtils.isEmpty(name)) {
logfilePath = Helper.preparePath(getOptionValue(cmd, "logfile", null), name);
cachefilePath = Helper.preparePath(getOptionValue(cmd, "cachefile", null), name);
pidfilePath = cachefilePath.substring(0, cachefilePath.lastIndexOf(".")) + ".pid";
lockfilePath = cachefilePath.substring(0, cachefilePath.lastIndexOf(".")) + ".lock";
}
final boolean baseValid = SyncType.LIST.equals(type) || (path != null && new File(path).isDirectory());
boolean logfileValid = logfilePath == null || new File(logfilePath).getParentFile().isDirectory();
boolean cachefileValid = cachefilePath == null || new File(cachefilePath).getParentFile().isDirectory();
if (cmd.hasOption("help") || type == null || name == null || followlinks == null || duplicate == null || permissions == null || !baseValid || config == null || !configValid || !logfileValid
|| !cachefileValid) {
List<String> messages = new ArrayList<String>();
if (cmd.getOptions().length > 0) {
messages.add("error: missing or wrong options");
if (type == null) {
messages.add(" You must specifiy --backup, --restore, --list or --clean");
} else if (!baseValid) {
messages.add(" --" + type.getName() + " <path> not valid");
}
if (name == null) {
messages.add(" Missing --name <name>");
}
if (followlinks == null) {
messages.add(" Wrong --followlinks behavior set");
}
if (duplicate == null) {
messages.add(" Wrong --duplicate behavior set");
}
if (permissions == null) {
messages.add(" Wrong --permissions behavior set");
}
if (config == null) {
messages.add(" Missing --config <path>");
} else if (!configValid) {
messages.add(" --config <path> not valid");
}
if (!logfileValid) {
messages.add(" --logfile <path> not valid");
}
if (!cachefileValid) {
messages.add(" --cachefile <path> not valid");
}
}
throw new UsageException(StringUtils.join(messages, '\n'));
}
}