import 'dart:io';

import 'package:scheduler_backup/backup.dart';

Future<void> main(List<String> arguments) async {
  final passphrase = _option(arguments, '--passphrase') ??
      Platform.environment['SCHEDULER_BACKUP_PASSPHRASE'];
  if (passphrase == null || passphrase.isEmpty) {
    stderr.writeln(
      'Usage: dart run scheduler_backup:backup --passphrase <passphrase>',
    );
    exitCode = 64;
    return;
  }

  try {
    final backup = await createEncryptedBackup(passphrase: passphrase);
    stdout.writeln(backup.path);
  } on Object catch (error) {
    stderr.writeln(error);
    exitCode = 1;
  }
}

String? _option(List<String> arguments, String name) {
  final index = arguments.indexOf(name);
  if (index == -1 || index + 1 >= arguments.length) {
    return null;
  }
  return arguments[index + 1];
}
