//  
//   Rebex Sample Code License
// 
//   Copyright 2025, REBEX CR s.r.o.
//   All rights reserved.
//   https://www.rebex.net/
// 
//   Permission to use, copy, modify, and/or distribute this software for any
//   purpose with or without fee is hereby granted.
// 
//   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
//   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
//   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
//   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
//   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
//   OTHER DEALINGS IN THE SOFTWARE.
// 

using System;

using Rebex.Net;
using Rebex.TerminalEmulation;

namespace Rebex.Samples
{
    /// <summary>
    /// The RExec class executes one command on the remote host.
    /// </summary>
    public class RExec
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        /// <param name="args">Program arguments.</param>
        /// <returns>Exit code.</returns>
        public static int Main(string[] args)
        {
            // set the trial license key
            Rebex.Licensing.Key = Rebex.Samples.TrialKey.Key;


            // if bad number of arguments was specified, show help and return
            if (args.Length != 4)
            {
                ShowHelp();
                return 1;
            }

            // initialize variables from the arguments
            string host = args[0];
            string login = args[1];
            string password = args[2];
            string command = args[3];

            // create an Ssh object
            Ssh ssh = new Ssh();

            try
            {
                // connect to the server
                ssh.Connect(host);

                // login to the server
                ssh.Login(login, password);

                // run the command
                string response = ssh.RunCommand(command);

                // write the response
                Console.Write(response);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception occured: {0}", e);
                return 1;
            }
            finally
            {
                // disconnect from the server
                if( ssh != null)
                    ssh.Disconnect();
            }

            return 0;
        }

        /// <summary>
        /// Shows syntax and example.
        /// </summary>
        private static void ShowHelp()
        {
            Console.WriteLine("--------------------------------------------------------------------");
            Console.WriteLine("Rebex RExec (Remote Exec) - executes a command on the remote host.");
            Console.WriteLine("--------------------------------------------------------------------");
            Console.WriteLine("Note: If the command requires user interaction, RExec will time out.");
            Console.WriteLine("Syntax: RExec host login password command");
            Console.WriteLine("Example: RExec sample.host.com test test123 \"ls -l\"");
        }
    }
}
