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
//! This library provides a configuration interface for systemd-boot. It parses systemd-boot loader
//! configuration and systemd-boot entry configuration.
//!
//! **NOTE**: Not all fields in <https://www.freedesktop.org/software/systemd/man/systemd-boot.html>
//! are implemented, this library currently only provides interface for the fields listed on
//! <https://www.freedesktop.org/wiki/Software/systemd/systemd-boot/>.
//!
//! # Create and write a new systemd-boot configuration
//!
//! You can use the `SystemdBootConfig` struct to create a new systemd-boot configuration, or use
//! `SystemdBootConfigBuilder` to build a `SystemdBootConfig` from scratch.
//!
//! ```no_run
//! use libsdbootconf::{ConfigBuilder, EntryBuilder, SystemdBootConfBuilder};
//!
//! let systemd_boot_conf = SystemdBootConfBuilder::new("/efi/loader")
//!     .config(ConfigBuilder::new()
//!         .default("5.12.0-aosc-main")
//!         .timeout(5u32)
//!         .build())
//!     .entry(EntryBuilder::new("5.12.0-aosc-main")
//!         .title("AOSC OS x86_64 (5.12.0-aosc-main)")
//!         .version("5.12.0-aosc-main")
//!         .build())
//!     .build();
//!
//! // Or
//! use libsdbootconf::{Config, Entry, SystemdBootConf, Token};
//!
//! let systemd_boot_conf = SystemdBootConf::new(
//!     "/efi/loader",
//!     Config::new(Some("5.12.0-aosc-main"), Some(5u32)),
//!     vec![Entry::new(
//!         "5.12.0-aosc-main",
//!         vec![
//!             Token::Title("AOSC OS x86_64 (5.12.0-aosc-main)".to_owned()),
//!             Token::Version("5.12.0-aosc-main".to_owned()),
//!         ],
//!     )]
//! );
//!
//! systemd_boot_conf.write_all().unwrap();
//! ```
//!
//! # Create a new systemd-boot menu entry
//!
//! ```no_run
//! use libsdbootconf::entry::{Entry, EntryBuilder, Token};
//! use std::path::PathBuf;
//!
//! let entry = EntryBuilder::new("5.12.0-aosc-main")
//!     .title("AOSC OS x86_64 (5.12.0-aosc-main)")
//!     .linux("/EFI/linux/vmlinux-5.12.0-aosc-main")
//!     .initrd("/EFI/linux/initramfs-5.12.0-aosc-main.img")
//!     .options("root=/dev/sda1 rw")
//!     .build();
//!
//! // Or
//! let entry = Entry::new(
//!     "5.12.0-aosc-main",
//!     vec![
//!         Token::Title("AOSC OS x86_64 (5.12.0-aosc-main)".to_owned()),
//!         Token::Linux(PathBuf::from("/EFI/linux/vmlinux-5.12.0-aosc-main")),
//!         Token::Initrd(PathBuf::from("/EFI/linux/initramfs-5.12.0-aosc-main.img")),
//!         Token::Options("root=/dev/sda1 rw".to_owned()),
//!     ],
//! );
//!
//! entry.write("/efi/loader/entries/5.12.0-aosc-main.conf").unwrap();
//! ```

use std::{
    fs,
    path::{Path, PathBuf},
};
use thiserror::Error;

pub mod config;
pub mod entry;
mod macros;

use crate::macros::generate_builder_method;
pub use config::{Config, ConfigBuilder};
pub use entry::{Entry, EntryBuilder, Token};

#[derive(Error, Debug)]
pub enum LibSDBootConfError {
    #[error("invalid configuration")]
    ConfigParseError,
    #[error("invalid entry")]
    EntryParseError,
    #[error("invalid entry filename {0}")]
    InvalidEntryFilename(PathBuf),
    #[error(transparent)]
    IOError(#[from] std::io::Error),
    #[error("invalid token {0}")]
    InvalidToken(String),
}

/// An abstraction over the basic structure of systemd-boot configurations.
#[derive(Default, Debug)]
pub struct SystemdBootConf {
    pub working_dir: PathBuf,
    pub config: Config,
    pub entries: Vec<Entry>,
}

impl SystemdBootConf {
    /// Create a new `SystemdBootConf` with a working directory, a configuration, and a list of
    /// entries.
    ///
    /// # Examples
    ///
    /// ```
    /// use libsdbootconf::SystemdBootConf;
    ///
    /// let systemd_boot_conf = SystemdBootConf::init("/efi/loader");
    ///
    /// assert_eq!(systemd_boot_conf.working_dir, std::path::PathBuf::from("/efi/loader"));
    /// ```
    pub fn new<P, C, E>(working_dir: P, config: C, entries: E) -> Self
    where
        P: Into<PathBuf>,
        C: Into<Config>,
        E: Into<Vec<Entry>>, 
    {
        Self {
            working_dir: working_dir.into(),
            config: config.into(),
            entries: entries.into(),
        }
    }

    /// Initialize a new `SystemdBootConf` with a working directory.
    ///
    /// # Examples
    ///
    /// ```
    /// use libsdbootconf::SystemdBootConf;
    ///
    /// let systemd_boot_conf = SystemdBootConf::init("/efi/loader");
    ///
    /// assert_eq!(systemd_boot_conf.working_dir, std::path::PathBuf::from("/efi/loader"));
    /// ```
    pub fn init<P: Into<PathBuf>>(working_dir: P) -> Self {
        Self {
            working_dir: working_dir.into(),
            ..Default::default()
        }
    }

    /// Read from an existing systemd-boot installation.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use libsdbootconf::SystemdBootConf;
    ///
    /// let systemd_boot_conf = SystemdBootConf::load("/efi/loader").unwrap();
    /// ```
    pub fn load<P: AsRef<Path>>(working_dir: P) -> Result<Self, LibSDBootConfError> {
        let mut systemd_boot_conf = Self::init(working_dir.as_ref());

        systemd_boot_conf.load_current()?;

        Ok(systemd_boot_conf)
    }

    /// Read from the current systemd-boot working directory.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use libsdbootconf::SystemdBootConf;
    ///
    /// let mut systemd_boot_conf = SystemdBootConf::init("/efi/loader");
    ///
    /// systemd_boot_conf.load_current().unwrap();
    /// ```
    pub fn load_current(&mut self) -> Result<(), LibSDBootConfError> {
        let config = Config::load(self.working_dir.join("loader.conf"))?;
        let mut entries = Vec::new();

        for file in fs::read_dir(self.working_dir.join("entries"))? {
            let path = file?.path();
            if path.is_file() {
                let entry = Entry::load(&path)?;
                entries.push(entry);
            }
        }

        self.config = config;
        self.entries = entries;

        Ok(())
    }

    /// Write systemd-boot configuration file to the system.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use libsdbootconf::SystemdBootConf;
    ///
    /// let systemd_boot_conf = SystemdBootConf::init("/efi/loader");
    ///
    /// systemd_boot_conf.write_config().unwrap();
    /// ```
    pub fn write_config(&self) -> Result<(), LibSDBootConfError> {
        self.config.write(self.working_dir.join("loader.conf"))?;
        
        Ok(())
    }

    /// Write all entries to the system.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use libsdbootconf::SystemdBootConf;
    ///
    /// let systemd_boot_conf = SystemdBootConf::init("/efi/loader");
    ///
    /// systemd_boot_conf.write_entries().unwrap();
    /// ```
    pub fn write_entries(&self) -> Result<(), LibSDBootConfError> {
        for entry in self.entries.iter() {
            entry.write(
                self.working_dir
                    .join("entries")
                    .join(format!("{}.conf", entry.id)),
            )?;
        }

        Ok(())
    }
 
    /// Write all configurations and entries to the system.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use libsdbootconf::SystemdBootConf;
    ///
    /// let systemd_boot_conf = SystemdBootConf::init("/efi/loader");
    ///
    /// systemd_boot_conf.write_all().unwrap();
    /// ```
    pub fn write_all(&self) -> Result<(), LibSDBootConfError> {
        self.write_config()?;
        self.write_entries()?;

        Ok(())
    }
}

/// Builder for `SystemdBootConf`.
#[derive(Default, Debug)]
pub struct SystemdBootConfBuilder {
    inner: SystemdBootConf,
}

impl SystemdBootConfBuilder {
    /// Create an empty `SystemdBootConfBuilder` with a working directory.
    pub fn new<P: Into<PathBuf>>(working_dir: P) -> Self {
        Self {
            inner: SystemdBootConf::init(working_dir),
        }
    }

    generate_builder_method!(
        /// Add a systemd-boot loader `Config`.
        plain INNER(inner) config(Config)
    );
    generate_builder_method!(
        /// Add a list of `Entry`.
        into INNER(inner) entries(E: Vec<Entry>)
    );

    /// Add an `Entry`
    pub fn entry(mut self, entry: Entry) -> Self {
        self.inner.entries.push(entry);

        self
    }

    /// Build the `SystemdBootConf`.
    pub fn build(self) -> SystemdBootConf {
        self.inner
    }
}