/*-
 * ~~~~~~licensing~~~~~~
 * keystore-shell
 * ==========
 * Copyright (C) 2020 - 2025 EmDev LLC
 * ==========
 * You may not use this file except in accordance with the License Terms of the Copyright
 * Holder located at: https://entaxy.ru/eula . All copyrights, all intellectual property
 * rights to the Software and any copies are the property of the Copyright Holder. Unless
 * it is explicitly allowed the Copyright Holder, the User is prohibited from using the
 * Software for commercial purposes to provide services to third parties.
 * 
 * The Copyright Holder hereby declares that the Software is provided on an "AS IS".
 * Under no circumstances does the Copyright Holder guarantee or promise that the
 * Software provided by him will be suitable or not suitable for the specific purposes
 * of the User, that the Software will meet all commercial and personal subjective
 * expectations of the User, that the Software will work properly, without technical
 * errors, quickly and uninterruptedly.
 * 
 * Under no circumstances shall the Copyright Holder or its Affiliates is not liable
 * to the User for any direct or indirect losses of the User, his expenses or actual
 * damage, including, downtime; loss of bussines; lost profit; lost earnings; loss
 * or damage to data, property, etc.
 * ~~~~~~/licensing~~~~~~
 */
package ru.entaxy.security.keystore.shell;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.karaf.shell.api.action.Action;
import org.apache.karaf.shell.api.action.Command;
import org.apache.karaf.shell.api.action.Option;
import org.apache.karaf.shell.api.action.lifecycle.Service;

import ru.entaxy.platform.base.support.CommonUtils;

@Service
@Command(scope = KeystoreShell.COMMAND_SCOPE, name = "keystore-key-add")
public class KeystoreKeyAddCommand extends KeystoreAwareCommand implements Action {

    @Option(name = "-a", aliases = {"--new-alias"})
    String alias;

    @Option(name = "-f", aliases = {"--from-file"})
    String contentFile;

    @Option(name = "-c", aliases = {"--content"})
    String content;


    @Override
    public Object execute() throws Exception {
        String contentToAdd = null;
        if (CommonUtils.isValid(content)) {
            contentToAdd = content;
        } else {
            if (CommonUtils.isValid(contentFile)) {
                File f = new File(contentFile);
                if (!f.exists()) {
                    // print error
                    System.err.println(String.format("ERROR: file [%s] not found", contentFile));
                    return null;
                }

                try {
                    contentToAdd = FileUtils.readFileToString(f);
                } catch (IOException e) {
                    // print error
                    System.err.println(
                            String.format("ERROR: failed reading file [%s]: [%s]", contentFile, e.getMessage()));
                    return null;
                }

            }
        }

        if (!CommonUtils.isValid(contentToAdd)) {
            // print error
            System.err.println("ERROR: content not specified: use -f or -c option");
            return null;
        }

        try {
            keystoreManager.addKey(keystoreId, alias, contentToAdd);
        } catch (Exception e) {
            System.err.println(
                    String.format("ERROR: failed adding key: [%s]", e.getMessage()));
            return null;
        }

        // output
        System.out.println("Done");

        return null;
    }

}
