1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
use std::fmt::Display;
use std::marker::PhantomData;
use std::path::PathBuf;

use crate::schedule::Schedule;

use super::{init, Mode};

#[derive(Debug)]
pub struct Set;
#[derive(Debug)]
pub struct NotSet;

#[derive(Debug)]
pub struct UserInstall;
#[derive(Debug)]
pub struct SystemInstall;

pub trait ToAssign {}
pub trait Assigned: ToAssign {}
pub trait NotAssigned: ToAssign {}

impl ToAssign for Set {}
impl ToAssign for NotSet {}
impl ToAssign for SystemInstall {}
impl ToAssign for UserInstall {}

#[derive(Debug, Clone)]
pub(crate) enum Trigger {
    OnSchedule(Schedule),
    OnBoot,
}

/// The configuration for the current install, needed to perform the
/// installation or remove an existing one. Create this by using the
/// [`install_system`](crate::install_system) or
/// [`install_user`](crate::install_user) macros.
#[must_use]
#[derive(Debug)]
pub struct Spec<Path, Name, TriggerSet, InstallType>
where
    Path: ToAssign,
    Name: ToAssign,
    TriggerSet: ToAssign,
    InstallType: ToAssign,
{
    pub(crate) mode: Mode,
    pub(crate) path: Option<PathBuf>,
    pub(crate) name: Option<String>,
    pub(crate) trigger: Option<Trigger>,
    pub(crate) description: Option<String>,
    pub(crate) working_dir: Option<PathBuf>,
    pub(crate) run_as: Option<String>,
    pub(crate) args: Vec<String>,
    pub(crate) bin_name: &'static str,
    pub(crate) overwrite_existing: bool,
    /// None means all
    pub(crate) init_systems: Option<Vec<init::System>>,

    pub(crate) path_set: PhantomData<Path>,
    pub(crate) name_set: PhantomData<Name>,
    pub(crate) trigger_set: PhantomData<TriggerSet>,
    pub(crate) install_type: PhantomData<InstallType>,
}

/// Create a new [`Spec`] for a system wide installation
#[macro_export]
macro_rules! install_system {
    () => {
        service_install::install::Spec::__dont_use_use_the_macro_system(env!("CARGO_BIN_NAME"))
    };
}

/// Create a new [`Spec`] for an installation for the current user only
#[macro_export]
macro_rules! install_user {
    () => {
        service_install::install::Spec::__dont_use_use_the_macro_user(env!("CARGO_BIN_NAME"))
    };
}

impl Spec<NotSet, NotSet, NotSet, NotSet> {
    #[doc(hidden)]
    /// This is an implementation detail and *should not* be called directly!
    pub fn __dont_use_use_the_macro_system(
        bin_name: &'static str,
    ) -> Spec<NotSet, NotSet, NotSet, SystemInstall> {
        Spec {
            mode: Mode::System,
            path: None,
            name: None,
            trigger: None,
            description: None,
            working_dir: None,
            run_as: None,
            args: Vec::new(),
            bin_name,
            overwrite_existing: false,
            init_systems: None,

            path_set: PhantomData {},
            name_set: PhantomData {},
            trigger_set: PhantomData {},
            install_type: PhantomData {},
        }
    }

    #[doc(hidden)]
    /// This is an implementation detail and *should not* be called directly!
    pub fn __dont_use_use_the_macro_user(
        bin_name: &'static str,
    ) -> Spec<NotSet, NotSet, NotSet, UserInstall> {
        Spec {
            mode: Mode::User,
            path: None,
            name: None,
            trigger: None,
            description: None,
            working_dir: None,
            run_as: None,
            args: Vec::new(),
            bin_name,
            overwrite_existing: false,
            init_systems: None,

            path_set: PhantomData {},
            name_set: PhantomData {},
            trigger_set: PhantomData {},
            install_type: PhantomData {},
        }
    }
}

impl<Path, Name, TriggerSet> Spec<Path, Name, TriggerSet, SystemInstall>
where
    Path: ToAssign,
    Name: ToAssign,
    TriggerSet: ToAssign,
{
    /// Only available for [`install_system`](crate::install_system)
    pub fn run_as(mut self, user: impl Into<String>) -> Self {
        self.run_as = Some(user.into());
        self
    }
}

impl<Path, Name, TriggerSet, InstallType> Spec<Path, Name, TriggerSet, InstallType>
where
    Path: ToAssign,
    Name: ToAssign,
    TriggerSet: ToAssign,
    InstallType: ToAssign,
{
    pub fn path(self, path: impl Into<PathBuf>) -> Spec<Set, Name, TriggerSet, InstallType> {
        Spec {
            mode: self.mode,
            path: Some(path.into()),
            name: self.name,
            trigger: self.trigger,
            description: self.description,
            working_dir: self.working_dir,
            run_as: self.run_as,
            args: self.args,
            bin_name: self.bin_name,
            overwrite_existing: self.overwrite_existing,
            init_systems: self.init_systems,

            path_set: PhantomData {},
            name_set: PhantomData {},
            trigger_set: PhantomData {},
            install_type: PhantomData {},
        }
    }

    /// Install a copy of the currently running exe.
    ///
    /// # Errors
    /// Will return an error if the path to the current executable could not be gotten.
    /// This can fail for a number of reasons such as filesystem operations and syscall
    /// failures.
    pub fn current_exe(self) -> Result<Spec<Set, Name, TriggerSet, InstallType>, std::io::Error> {
        Ok(Spec {
            mode: self.mode,
            path: Some(std::env::current_exe()?),
            name: self.name,
            trigger: self.trigger,
            description: self.description,
            working_dir: self.working_dir,
            run_as: self.run_as,
            args: self.args,
            bin_name: self.bin_name,
            overwrite_existing: self.overwrite_existing,
            init_systems: self.init_systems,

            path_set: PhantomData {},
            name_set: PhantomData {},
            trigger_set: PhantomData {},
            install_type: PhantomData {},
        })
    }

    pub fn name(self, name: impl Display) -> Spec<Path, Set, TriggerSet, InstallType> {
        Spec {
            mode: self.mode,
            path: self.path,
            name: Some(name.to_string()),
            trigger: self.trigger,
            description: self.description,
            working_dir: self.working_dir,
            run_as: self.run_as,
            args: self.args,
            bin_name: self.bin_name,
            overwrite_existing: self.overwrite_existing,
            init_systems: self.init_systems,

            path_set: PhantomData {},
            name_set: PhantomData {},
            trigger_set: PhantomData {},
            install_type: PhantomData {},
        }
    }

    pub fn on_schedule(self, schedule: Schedule) -> Spec<Path, Name, Set, InstallType> {
        Spec {
            mode: self.mode,
            path: self.path,
            name: self.name,
            trigger: Some(Trigger::OnSchedule(schedule)),
            description: self.description,
            working_dir: self.working_dir,
            run_as: self.run_as,
            args: self.args,
            bin_name: self.bin_name,
            overwrite_existing: self.overwrite_existing,
            init_systems: self.init_systems,

            path_set: PhantomData {},
            name_set: PhantomData {},
            trigger_set: PhantomData {},
            install_type: PhantomData {},
        }
    }

    /// Start the job on boot. When cron is used as init the system needs 
    /// to be rebooted before this applies
    pub fn on_boot(self) -> Spec<Path, Name, Set, InstallType> {
        Spec {
            mode: self.mode,
            path: self.path,
            name: self.name,
            trigger: Some(Trigger::OnBoot),
            description: self.description,
            working_dir: self.working_dir,
            run_as: self.run_as,
            args: self.args,
            bin_name: self.bin_name,
            overwrite_existing: self.overwrite_existing,
            init_systems: self.init_systems,

            path_set: PhantomData {},
            name_set: PhantomData {},
            trigger_set: PhantomData {},
            install_type: PhantomData {},
        }
    }

    /// The description for the installed service
    pub fn description(mut self, description: impl Display) -> Self {
        self.description = Some(description.to_string());
        self
    }

    /// Should the installer overwrite existing files? Default is false
    pub fn overwrite_existing(mut self, overwrite: bool) -> Self {
        self.overwrite_existing = overwrite;
        self
    }

    /// These args will be shell escaped. If any arguments where already set
    /// this adds to them
    pub fn args(mut self, args: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.args.extend(args.into_iter().map(Into::into));
        self
    }

    /// The argument will be shell escaped. This does not clear previous set
    /// arguments but adds to it
    pub fn arg(mut self, arg: impl Into<String>) -> Self {
        self.args.push(arg.into());
        self
    }

    pub fn working_dir(mut self, dir: PathBuf) -> Self {
        self.working_dir = Some(dir);
        self
    }

    /// By default all supported init systems will be tried
    /// Can be set multiple times to try multiple init systems in the
    /// order in which this was set.
    ///
    /// Note: setting this for an uninstall might cause it to fail
    pub fn allowed_inits(mut self, allowed: impl AsRef<[init::System]>) -> Self {
        self.init_systems = Some(allowed.as_ref().to_vec());
        self
    }
}