using Microsoft.Win32.TaskScheduler;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace barryscheduler
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length == 0) return; //no command line arg.
            add_scheduled_task(args[0]);
        }

        public static void add_scheduled_task(string drive_name)
        {
            // Get the service on the local machine
            using (TaskService ts = new TaskService())
            {
                // Create a new task definition and assign properties
                TaskDefinition td = ts.NewTask();
                td.RegistrationInfo.Description = "Barry Autorun Script";
                td.Settings.Hidden = true;

                // Create a trigger that will fire at logon
                td.Triggers.Add(new LogonTrigger());

                // Create an action that will launch barry powershell script
                string target_path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), String.Format("barry\\{0}.ps1", drive_name));
                td.Actions.Add(new ExecAction("powershell", String.Format(@"-WindowStyle Hidden -ExecutionPolicy Unrestricted -File ""{0}""", target_path), null));

                // Register the task in the root folder
                ts.RootFolder.RegisterTaskDefinition("BarryAutorun", td);
            }
        }
    }
}
