defter 0.5.0

Self-hosted Zotero sync and file storage server, with metadata in SQLite and attachments in S3.
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
//! defter: a self-hosted Zotero sync and file storage server.
//!
//! Library metadata lives in SQLite, attachment files in S3-compatible object
//! storage. Stock Zotero clients sync against it, and commonmeta sync can
//! enrich the library through the same API.

use std::net::SocketAddr;
use std::path::PathBuf;
use std::time::Duration;

use clap::{Args, Parser, Subcommand};

#[cfg(test)]
mod api_tests;
mod app;
mod auth;
mod backup;
mod countries;
mod db;
mod error;
mod i18n;
mod oidc;
mod profile;
mod query;
mod serve;
mod storage;
mod store;
mod ui;
mod zotero;

#[derive(Parser)]
#[command(
    name = "defter",
    version,
    about = "Self-hosted Zotero sync and file storage server"
)]
struct Cli {
    #[command(subcommand)]
    command: Command,
}

#[derive(Subcommand)]
enum Command {
    /// Start the server (runs in the foreground).
    Start(Box<StartArgs>),
    /// Print the read/write API key the login session hands out, generating
    /// one in the data directory when none is configured.
    Key(KeyCommandArgs),
    /// Back up the library database to object storage now, or list backups.
    /// Safe while the server runs.
    Backup(Box<BackupCommandArgs>),
    /// Restore the library database from a backup. The server must be stopped.
    Restore(Box<RestoreCommandArgs>),
}

#[derive(Args)]
struct DataArgs {
    /// Directory for the database, the generated API key and (without S3)
    /// attachment files. Defaults to $STATE_DIRECTORY under systemd; as root,
    /// /var/lib/defter (macOS: /Library/Application Support/defter);
    /// otherwise ~/.local/share/defter or $XDG_DATA_HOME/defter (macOS:
    /// ~/Library/Application Support/defter).
    #[arg(long, env = "DEFTER_DATA_DIR")]
    data_dir: Option<PathBuf>,
}

#[derive(Args)]
struct KeyArgs {
    /// API keys as a comma-separated list of `rw:<file>` / `ro:<file>`
    /// entries, each file holding one token. Read-only keys can read
    /// everything but change nothing.
    #[arg(long, env = "DEFTER_KEYS")]
    keys: Option<String>,

    /// A file holding a single read/write API key.
    #[arg(long, env = "DEFTER_API_KEY_FILE")]
    api_key_file: Option<PathBuf>,

    /// A single read/write API key, e.g. from an environment file only root
    /// and the defter group can read. Approves Zotero logins on the login
    /// page. Generate it with `openssl rand -hex 32`.
    #[arg(long, env = "DEFTER_API_KEY", hide_env_values = true)]
    api_key: Option<String>,
}

#[derive(Args)]
struct KeyCommandArgs {
    #[command(flatten)]
    data: DataArgs,
    #[command(flatten)]
    keys: KeyArgs,
}

/// Where attachment files are, which backups also use.
#[derive(Args)]
struct StorageArgs {
    /// S3 bucket for attachment files. Without it files are kept in
    /// --files-dir, which is meant for development.
    #[arg(long, env = "DEFTER_S3_BUCKET")]
    s3_bucket: Option<String>,

    /// S3 endpoint, e.g. https://fsn1.your-objectstorage.com (Hetzner),
    /// https://<account>.r2.cloudflarestorage.com (Cloudflare R2) or
    /// http://127.0.0.1:9000 (RustFS); defaults to AWS. Clients download from
    /// pre-signed URLs on it, so they must be able to reach it.
    #[arg(long, env = "DEFTER_S3_ENDPOINT")]
    s3_endpoint: Option<String>,

    /// S3 region: the location for Hetzner (fsn1, nbg1, hel1), `auto` for R2.
    /// Falls back to AWS_REGION.
    #[arg(long, env = "DEFTER_S3_REGION")]
    s3_region: Option<String>,

    /// S3 access key ID. Falls back to AWS_ACCESS_KEY_ID.
    #[arg(long, env = "DEFTER_S3_ACCESS_KEY_ID")]
    s3_access_key_id: Option<String>,

    /// File holding the S3 access key ID.
    #[arg(long, env = "DEFTER_S3_ACCESS_KEY_ID_FILE")]
    s3_access_key_id_file: Option<PathBuf>,

    /// S3 secret access key (prefer the _FILE variant). Falls back to
    /// AWS_SECRET_ACCESS_KEY.
    #[arg(long, env = "DEFTER_S3_SECRET_ACCESS_KEY", hide_env_values = true)]
    s3_secret_access_key: Option<String>,

    /// File holding the S3 secret access key.
    #[arg(long, env = "DEFTER_S3_SECRET_ACCESS_KEY_FILE")]
    s3_secret_access_key_file: Option<PathBuf>,

    /// Address the bucket as <bucket>.<endpoint> instead of <endpoint>/<bucket>.
    /// Always on for Hetzner Object Storage.
    #[arg(long, env = "DEFTER_S3_VIRTUAL_HOSTED_STYLE")]
    s3_virtual_hosted_style: bool,

    /// Key prefix for every object, so a bucket can be shared.
    #[arg(long, env = "DEFTER_S3_PREFIX", default_value = "")]
    s3_prefix: String,

    /// Seconds a pre-signed download URL stays valid.
    #[arg(long, env = "DEFTER_PRESIGN_TTL", default_value_t = 300)]
    presign_ttl: u64,

    /// Stream downloads through the server instead of redirecting clients
    /// to the bucket (for a bucket clients cannot reach).
    #[arg(long, env = "DEFTER_PROXY_DOWNLOADS")]
    proxy_downloads: bool,

    /// Directory for attachment files when no S3 bucket is configured.
    /// Defaults to <data-dir>/files.
    #[arg(long, env = "DEFTER_FILES_DIR")]
    files_dir: Option<PathBuf>,
}

/// The library database.
#[derive(Args)]
struct LibraryArgs {
    /// SQLite database file. Defaults to <data-dir>/defter.sqlite3.
    #[arg(long, env = "DEFTER_DB")]
    db: Option<PathBuf>,

    /// The library's Zotero user ID.
    #[arg(long, env = "DEFTER_USER_ID", default_value_t = 1)]
    user_id: u64,
}

#[derive(Args)]
struct BackupArgs {
    /// Hours between backups of the library database to object storage, under
    /// backup/. 0 turns scheduled backups off.
    #[arg(long, env = "DEFTER_BACKUP_INTERVAL_HOURS", default_value_t = 6)]
    backup_interval_hours: u64,

    /// Days backups are kept. Deleted attachment files are kept as long, so a
    /// restored library finds them.
    #[arg(long, env = "DEFTER_BACKUP_KEEP_DAYS", default_value_t = 30)]
    backup_keep_days: u64,

    /// A separate S3 bucket for backups, with the same endpoint and
    /// credentials, so losing the files bucket does not lose them too.
    /// Defaults to the files bucket.
    #[arg(long, env = "DEFTER_BACKUP_S3_BUCKET")]
    backup_s3_bucket: Option<String>,
}

#[derive(Args)]
struct BackupCommandArgs {
    #[command(flatten)]
    data: DataArgs,
    #[command(flatten)]
    library: LibraryArgs,
    #[command(flatten)]
    storage: StorageArgs,
    #[command(flatten)]
    backup: BackupArgs,

    /// List the backups instead of making one.
    #[arg(long)]
    list: bool,
}

#[derive(Args)]
struct RestoreCommandArgs {
    #[command(flatten)]
    data: DataArgs,
    #[command(flatten)]
    library: LibraryArgs,
    #[command(flatten)]
    storage: StorageArgs,
    #[command(flatten)]
    backup: BackupArgs,

    /// The backup to restore, by its time as `defter backup --list` shows it.
    /// Defaults to the newest.
    #[arg(long)]
    manifest: Option<String>,

    /// Restore. Without it, only show what would be done.
    #[arg(long)]
    yes: bool,

    /// The highest library version Zotero clients may have seen, needed only
    /// when the live database cannot be read. Versions are set above it.
    #[arg(long)]
    min_version: Option<i64>,
}

#[derive(Args)]
struct StartArgs {
    #[command(flatten)]
    data: DataArgs,
    #[command(flatten)]
    keys: KeyArgs,
    #[command(flatten)]
    library: LibraryArgs,
    #[command(flatten)]
    storage: StorageArgs,
    #[command(flatten)]
    backup: BackupArgs,

    /// Address to listen on. Keep the default behind a reverse proxy.
    #[arg(long, env = "DEFTER_HOST", default_value = "127.0.0.1")]
    host: String,

    /// TCP port to listen on.
    #[arg(short, long, env = "DEFTER_PORT", default_value_t = 8189)]
    port: u16,

    /// PEM certificate (chain) to serve HTTPS with, e.g. a Cloudflare origin
    /// certificate when a TLS pass-through such as dragoman's --sni-route
    /// forwards connections. Requires --tls-key. Without it, plain HTTP.
    #[arg(long, env = "DEFTER_TLS_CERT", requires = "tls_key")]
    tls_cert: Option<PathBuf>,

    /// PEM private key for --tls-cert.
    #[arg(long, env = "DEFTER_TLS_KEY", requires = "tls_cert")]
    tls_key: Option<PathBuf>,

    /// Public hostname to get an HTTPS certificate for from Let's Encrypt, when
    /// no CDN is in front. Connections for it on port 443 must reach this
    /// server, such as through dragoman's --sni-route. Requires --email.
    #[arg(
        long,
        env = "DEFTER_DOMAIN",
        requires = "email",
        conflicts_with = "tls_cert"
    )]
    domain: Option<String>,

    /// Contact email for the Let's Encrypt account, with --domain.
    #[arg(long, env = "DEFTER_EMAIL")]
    email: Option<String>,

    /// Directory keeping the Let's Encrypt account and certificates between
    /// restarts. Defaults to <data-dir>/acme.
    #[arg(long, env = "DEFTER_ACME_CACHE")]
    acme_cache: Option<PathBuf>,

    /// Use Let's Encrypt's staging environment, whose certificates browsers do
    /// not trust: for trying the setup out without its rate limits.
    #[arg(long, env = "DEFTER_ACME_STAGING")]
    acme_staging: bool,

    /// URL clients reach the server at (the reverse proxy's or CDN's), used in
    /// login, upload and download links. Defaults to https://<domain> with
    /// --domain, otherwise http(s)://<host>:<port>.
    #[arg(long, env = "DEFTER_PUBLIC_URL")]
    public_url: Option<String>,

    /// The user name reported to clients.
    #[arg(long, env = "DEFTER_USERNAME", default_value = "defter")]
    username: String,

    /// Require the SSO proxy in front of /login to forward this identity
    /// (X-Forwarded-Email/-User or X-Auth-Request-Email/-User) to approve a
    /// login. Leave unset on a private network.
    #[arg(long, env = "DEFTER_LOGIN_AUTHORIZED_USER")]
    login_authorized_user: Option<String>,

    /// Largest attachment file accepted, in MiB.
    #[arg(long, env = "DEFTER_MAX_UPLOAD_MB", default_value_t = 1024)]
    max_upload_mb: u64,

    /// Your ORCID iD (or ORCID URL): publishes a profile at /<orcid> with
    /// your ORCID record and every work in My Publications. It can be hidden
    /// on the dashboard.
    #[arg(long, env = "DEFTER_ORCID")]
    orcid: Option<String>,

    /// Issuer URL of an OpenID Connect provider to also sign in through, such
    /// as https://auth.example.org/realms/<realm>. Requires --oidc-client-id and
    /// --oidc-allowed-orcids. API keys keep working.
    #[arg(long, env = "DEFTER_OIDC_ISSUER")]
    oidc_issuer: Option<String>,

    /// The client this server is at the provider. Its redirect URI is
    /// <public-url>/ui/oidc/callback.
    #[arg(long, env = "DEFTER_OIDC_CLIENT_ID")]
    oidc_client_id: Option<String>,

    /// The client's secret. Prefer --oidc-client-secret-file: the environment
    /// is visible in /proc. Leave out for a public client.
    #[arg(long, env = "DEFTER_OIDC_CLIENT_SECRET", hide_env_values = true)]
    oidc_client_secret: Option<String>,

    /// File holding the client's secret.
    #[arg(long, env = "DEFTER_OIDC_CLIENT_SECRET_FILE")]
    oidc_client_secret_file: Option<String>,

    /// The ORCID iDs that may sign in, comma-separated. They may approve Zotero
    /// logins, and hide the public profile. Without --orcid, the profile is the
    /// first of them to sign in.
    #[arg(long, env = "DEFTER_OIDC_ALLOWED_ORCIDS")]
    oidc_allowed_orcids: Option<String>,

    /// The claim holding an account's ORCID iD, in the ID token or userinfo.
    #[arg(long, env = "DEFTER_OIDC_ORCID_CLAIM", default_value = "orcid")]
    oidc_orcid_claim: String,

    /// What the sign-in button calls the provider.
    #[arg(long, env = "DEFTER_OIDC_NAME", default_value = "Keycloak")]
    oidc_name: String,

    /// Where to get Zotero's item schema for GET /schema.
    #[arg(
        long,
        env = "DEFTER_SCHEMA_URL",
        default_value = "https://api.zotero.org/schema"
    )]
    schema_url: String,
}

/// The data directory of a system-wide installation (run as root).
#[cfg(target_os = "macos")]
const SYSTEM_DATA_DIR: &str = "/Library/Application Support/defter";
#[cfg(not(target_os = "macos"))]
const SYSTEM_DATA_DIR: &str = "/var/lib/defter";

/// What the default data directory depends on, read from the process.
struct DataDirEnv {
    /// systemd's `$STATE_DIRECTORY`, set by `StateDirectory=defter`.
    state_directory: Option<String>,
    root: bool,
    home: Option<String>,
    #[cfg_attr(target_os = "macos", allow(dead_code))]
    xdg_data_home: Option<String>,
}

impl DataDirEnv {
    fn current() -> Self {
        let var = |name| std::env::var(name).ok().filter(|v: &String| !v.is_empty());
        DataDirEnv {
            state_directory: var("STATE_DIRECTORY"),
            #[cfg(unix)]
            // SAFETY: geteuid has no preconditions and cannot fail.
            root: unsafe { libc::geteuid() } == 0,
            #[cfg(not(unix))]
            root: false,
            home: var("HOME"),
            xdg_data_home: var("XDG_DATA_HOME"),
        }
    }
}

/// The data directory when none is given:
///
/// 1. `$STATE_DIRECTORY`, under a systemd unit with `StateDirectory=defter`
///    (`/var/lib/defter`); its first entry when it lists several;
/// 2. as root, the system directory: `/var/lib/defter` on Linux,
///    `/Library/Application Support/defter` on macOS;
/// 3. otherwise the user's: `~/Library/Application Support/defter` on macOS,
///    `$XDG_DATA_HOME/defter` or `~/.local/share/defter` elsewhere.
fn default_data_dir(env: &DataDirEnv) -> PathBuf {
    if let Some(dir) = env
        .state_directory
        .as_deref()
        .and_then(|dirs| dirs.split(':').next())
        .filter(|dir| !dir.is_empty())
    {
        return PathBuf::from(dir);
    }
    if env.root {
        return PathBuf::from(SYSTEM_DATA_DIR);
    }
    let home = || env.home.as_deref().map(PathBuf::from);
    #[cfg(target_os = "macos")]
    let user = home().map(|h| h.join("Library/Application Support"));
    #[cfg(not(target_os = "macos"))]
    let user = env
        .xdg_data_home
        .as_deref()
        .map(PathBuf::from)
        // The XDG spec ignores a relative XDG_DATA_HOME.
        .filter(|p| p.is_absolute())
        .or_else(|| home().map(|h| h.join(".local/share")));
    user.map(|dir| dir.join("defter"))
        .unwrap_or_else(|| PathBuf::from(SYSTEM_DATA_DIR))
}

impl DataArgs {
    fn dir(&self) -> PathBuf {
        self.data_dir
            .clone()
            .unwrap_or_else(|| default_data_dir(&DataDirEnv::current()))
    }
}

impl KeyArgs {
    fn source(&self, data_dir: &std::path::Path) -> auth::KeySource {
        auth::KeySource {
            manifest: self.keys.clone(),
            file: self.api_key_file.clone(),
            key: self.api_key.clone(),
            generated: data_dir.join("api-key"),
        }
    }
}

fn fail(message: impl std::fmt::Display) -> ! {
    eprintln!("defter: {message}");
    std::process::exit(1);
}

/// A secret from a file (preferred) or a value.
fn secret(file: &Option<PathBuf>, value: &Option<String>) -> Option<String> {
    match file {
        Some(path) => Some(
            std::fs::read_to_string(path)
                .unwrap_or_else(|e| fail(format!("read {}: {e}", path.display())))
                .trim()
                .to_string(),
        ),
        None => value.clone(),
    }
}

impl StorageArgs {
    fn backend(&self, data_dir: &std::path::Path) -> storage::Backend {
        match &self.s3_bucket {
            Some(bucket) => storage::Backend::S3(storage::S3Config {
                bucket: bucket.clone(),
                endpoint: self.s3_endpoint.clone(),
                region: self.s3_region.clone(),
                access_key_id: secret(&self.s3_access_key_id_file, &self.s3_access_key_id),
                secret_access_key: secret(
                    &self.s3_secret_access_key_file,
                    &self.s3_secret_access_key,
                ),
                virtual_hosted_style: self.s3_virtual_hosted_style,
                prefix: self.s3_prefix.clone(),
            }),
            None => storage::Backend::Local(
                self.files_dir
                    .clone()
                    .unwrap_or_else(|| data_dir.join("files")),
            ),
        }
    }

    fn storage(&self, data_dir: &std::path::Path) -> storage::Storage {
        storage::Storage::new(
            &self.backend(data_dir),
            Duration::from_secs(self.presign_ttl),
            self.proxy_downloads,
        )
        .unwrap_or_else(|e| fail(format!("object storage: {e}")))
    }

    /// Where backups go: the files bucket, or `bucket` with its settings.
    fn backup_storage(&self, data_dir: &std::path::Path, bucket: Option<&str>) -> storage::Storage {
        let backend = match (
            self.backend(data_dir),
            bucket.filter(|b| !b.trim().is_empty()),
        ) {
            (storage::Backend::S3(mut cfg), Some(bucket)) => {
                cfg.bucket = bucket.to_string();
                storage::Backend::S3(cfg)
            }
            (_, Some(_)) => fail(
                "DEFTER_BACKUP_S3_BUCKET needs DEFTER_S3_BUCKET, whose settings it shares"
                    .to_string(),
            ),
            (backend, None) => backend,
        };
        storage::Storage::new(&backend, Duration::from_secs(self.presign_ttl), true)
            .unwrap_or_else(|e| fail(format!("backup storage: {e}")))
    }
}

impl LibraryArgs {
    fn path(&self, data_dir: &std::path::Path) -> PathBuf {
        self.db
            .clone()
            .unwrap_or_else(|| data_dir.join("defter.sqlite3"))
    }

    fn source(&self, data_dir: &std::path::Path) -> backup::Source {
        backup::Source {
            name: format!("u{}", self.user_id),
            path: self.path(data_dir),
        }
    }
}

/// Backups of the library, with attachment files in `files`.
fn backups(
    data_dir: &std::path::Path,
    library: &LibraryArgs,
    storage_args: &StorageArgs,
    backup_args: &BackupArgs,
    files: std::sync::Arc<storage::Storage>,
) -> backup::Backup {
    backup::Backup::new(
        std::sync::Arc::new(
            storage_args.backup_storage(data_dir, backup_args.backup_s3_bucket.as_deref()),
        ),
        files,
        vec![library.source(data_dir)],
        data_dir.join("tmp"),
        backup_args.backup_keep_days,
    )
}

async fn backup_command(args: BackupCommandArgs) {
    let data_dir = args.data.dir();
    let files = std::sync::Arc::new(args.storage.storage(&data_dir));
    let backup = backups(&data_dir, &args.library, &args.storage, &args.backup, files);
    if args.list {
        let manifests = backup::list(&backup.store)
            .await
            .unwrap_or_else(|e| fail(e));
        if manifests.is_empty() {
            println!("no backups in {}", backup.store.description());
        }
        for manifest in manifests {
            let libraries = manifest
                .entries
                .iter()
                .map(|e| format!("{} v{} ({} bytes)", e.name, e.version, e.bytes))
                .collect::<Vec<_>>()
                .join(", ");
            println!(
                "{}  defter {}  {libraries}",
                manifest.created, manifest.defter
            );
        }
        return;
    }
    let report = backup.run().await;
    match report.error {
        Some(error) => fail(error),
        None => match report.manifest {
            Some(manifest) => println!(
                "backed up to {manifest}: {} snapshot(s), {} bytes",
                report.snapshots, report.bytes
            ),
            None => println!("nothing changed since the last backup"),
        },
    }
}

async fn restore_command(args: RestoreCommandArgs) {
    let data_dir = args.data.dir();
    let _lock = backup::lock(&data_dir).unwrap_or_else(|e| fail(e));
    let files = std::sync::Arc::new(args.storage.storage(&data_dir));
    let backup = backups(&data_dir, &args.library, &args.storage, &args.backup, files);
    let source = args.library.source(&data_dir);
    let restored = backup::restore(
        &backup.store,
        &backup.files,
        &source,
        args.manifest.as_deref(),
        &data_dir.join("tmp"),
        args.yes,
        args.min_version,
    )
    .await
    .unwrap_or_else(|e| fail(e));
    println!(
        "{} {} from {} (library version {}), with every version set to {}",
        if args.yes {
            "restored"
        } else {
            "would restore"
        },
        source.path.display(),
        restored.manifest,
        restored.snapshot_version,
        restored.version
    );
    if let Some(previous) = &restored.previous {
        println!("the replaced database is kept as {}", previous.display());
    }
    if args.yes {
        println!(
            "attachment files brought back: {}, still missing: {}",
            restored.files_restored, restored.files_missing
        );
        println!(
            "Zotero clients now see a newer library. Follow \"Backups and restore\" in the \
             README to bring them in line."
        );
    } else {
        println!("run again with --yes to restore");
    }
}

#[tokio::main]
async fn main() {
    tracing_subscriber::fmt()
        .with_env_filter(
            tracing_subscriber::EnvFilter::try_from_default_env()
                .unwrap_or_else(|_| "defter=info,tower_http=warn".into()),
        )
        .init();
    // object_store and reqwest bring rustls with aws-lc-rs, commonmeta with
    // ring; with both compiled in rustls cannot choose, so choose for it.
    let _ = rustls::crypto::aws_lc_rs::default_provider().install_default();

    match Cli::parse().command {
        Command::Start(args) => start(*args).await,
        Command::Key(args) => {
            let (keys, generated) =
                auth::Keys::load(&args.keys.source(&args.data.dir())).unwrap_or_else(|e| fail(e));
            if let Some(path) = generated {
                eprintln!("defter: generated a key in {}", path.display());
            }
            println!("{}", keys.login_key());
        }
        Command::Backup(args) => backup_command(*args).await,
        Command::Restore(args) => restore_command(*args).await,
    }
}

async fn start(args: StartArgs) {
    let data_dir = args.data.dir();
    let (keys, generated) =
        auth::Keys::load(&args.keys.source(&data_dir)).unwrap_or_else(|e| fail(e));
    if let Some(path) = generated {
        tracing::warn!(path = %path.display(), "no API key configured; generated one");
    }
    if keys.short_keys() > 0 {
        tracing::warn!(
            count = keys.short_keys(),
            min = auth::MIN_KEY_LENGTH,
            "API keys shorter than the minimum are easy to guess on the login page; \
             generate keys with `openssl rand -hex 32`"
        );
    }

    // Held while the server runs, so a restore cannot replace the database
    // under it.
    let _lock = backup::lock(&data_dir).unwrap_or_else(|e| fail(e));
    if args.storage.s3_bucket.is_none() {
        tracing::warn!("no S3 bucket configured; keeping attachment files on local disk");
    }
    let storage = args.storage.storage(&data_dir);

    let db_path = args.library.path(&data_dir);
    let db = db::Db::open(&db_path).unwrap_or_else(|e| fail(format!("{}: {e}", db_path.display())));

    let addr: SocketAddr = format!("{}:{}", args.host, args.port)
        .parse()
        .unwrap_or_else(|e| fail(format!("listen address: {e}")));
    let tls = match (&args.domain, &args.email) {
        (Some(domain), Some(email)) => Some(serve::Tls::Acme {
            domain: domain.clone(),
            email: email.clone(),
            cache: args
                .acme_cache
                .clone()
                .unwrap_or_else(|| data_dir.join("acme")),
            staging: args.acme_staging,
        }),
        _ => args
            .tls_cert
            .clone()
            .zip(args.tls_key.clone())
            .map(|(cert, key)| serve::Tls::Files { cert, key }),
    };
    let scheme = if tls.is_some() { "https" } else { "http" };
    let public_url = args
        .public_url
        .clone()
        .or_else(|| args.domain.as_ref().map(|d| format!("https://{d}")))
        .unwrap_or_else(|| format!("{scheme}://{addr}"))
        .trim_end_matches('/')
        .to_string();
    let cfg = app::Config {
        user_id: args.library.user_id,
        username: args.username.clone(),
        public_url: public_url.clone(),
        login_authorized_user: args.login_authorized_user.clone().filter(|s| !s.is_empty()),
        max_upload: args.max_upload_mb.saturating_mul(1024 * 1024),
        schema_url: args.schema_url.clone(),
        orcid: args
            .orcid
            .as_deref()
            .filter(|s| !s.trim().is_empty())
            .map(|s| {
                profile::normalize_orcid(s)
                    .unwrap_or_else(|| fail(format!("--orcid: {s:?} is not a valid ORCID iD")))
            }),
        oidc: args
            .oidc_issuer
            .as_deref()
            .filter(|issuer| !issuer.trim().is_empty())
            .map(|issuer| {
                oidc::OidcConfig::new(
                    issuer,
                    args.oidc_client_id.as_deref().unwrap_or(""),
                    secret(
                        &args
                            .oidc_client_secret_file
                            .as_deref()
                            .filter(|file| !file.trim().is_empty())
                            .map(PathBuf::from),
                        &args.oidc_client_secret,
                    ),
                    args.oidc_allowed_orcids.as_deref().unwrap_or(""),
                    &args.oidc_orcid_claim,
                    &args.oidc_name,
                )
                .unwrap_or_else(|e| fail(e))
            }),
        // With a provider to sign in through, typing a key is opt-in. Without
        // one it is the only way in, so it stays.
        key_login: args
            .oidc_issuer
            .as_deref()
            .is_none_or(|i| i.trim().is_empty())
            || args
                .keys
                .api_key
                .as_deref()
                .is_some_and(|key| !key.trim().is_empty()),
    };
    let app = app::App::new(cfg, db, storage, keys)
        .await
        .unwrap_or_else(|e| fail(e));

    if args.backup.backup_interval_hours > 0 {
        let backup = std::sync::Arc::new(backups(
            &data_dir,
            &args.library,
            &args.storage,
            &args.backup,
            app.storage.clone(),
        ));
        app.storage.keep_deleted(true);
        let _ = app.backup.set(backup.clone());
        let every = Duration::from_secs(args.backup.backup_interval_hours * 3600);
        tokio::spawn(async move {
            // A minute in, so a deploy is backed up soon, then on schedule.
            let mut ticks = tokio::time::interval_at(
                tokio::time::Instant::now() + Duration::from_secs(60),
                every,
            );
            ticks.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
            loop {
                ticks.tick().await;
                backup.run().await;
            }
        });
    } else {
        tracing::warn!("scheduled backups are off (--backup-interval-hours 0)");
    }

    let storage = app.storage.description().to_string();
    let cert = match &tls {
        Some(serve::Tls::Files { cert, .. }) => Some(cert.display().to_string()),
        Some(serve::Tls::Acme { domain, .. }) => Some(format!("Let's Encrypt: {domain}")),
        None => None,
    };
    serve::serve(addr, app::router(app), tls, shutdown(), move |addr| {
        tracing::info!(
            %addr,
            scheme,
            cert,
            public_url,
            db = %db_path.display(),
            storage,
            "defter listening"
        );
    })
    .await
    .unwrap_or_else(|e| fail(e));
}

async fn shutdown() {
    let ctrl_c = async {
        let _ = tokio::signal::ctrl_c().await;
    };
    #[cfg(unix)]
    let terminate = async {
        if let Ok(mut s) = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
        {
            s.recv().await;
        }
    };
    #[cfg(not(unix))]
    let terminate = std::future::pending::<()>();
    tokio::select! {
        _ = ctrl_c => {},
        _ = terminate => {},
    }
    tracing::info!("shutting down");
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn oidc_settings_set_to_nothing_mean_off() {
        let start = |args: &[&str]| {
            let all = ["defter", "start"].iter().chain(args).copied();
            Cli::try_parse_from(all).map(|_| ())
        };
        // The same goes for the variables, which clap treats like these.
        assert!(start(&["--oidc-issuer", ""]).is_ok());
        assert!(start(&["--oidc-client-secret-file", ""]).is_ok());
    }

    #[test]
    fn lets_encrypt_needs_an_email_and_excludes_certificate_files() {
        let parse = |args: &[&str]| {
            let all = ["defter", "start"].iter().chain(args).copied();
            Cli::try_parse_from(all).map(|_| ())
        };
        const DOMAIN: &str = "defter.example.org";
        assert!(parse(&["--domain", DOMAIN, "--email", "me@example.org"]).is_ok());
        assert!(parse(&["--domain", DOMAIN]).is_err());
        assert!(
            parse(&[
                "--domain",
                DOMAIN,
                "--email",
                "me@example.org",
                "--tls-cert",
                "c.pem",
                "--tls-key",
                "k.pem",
            ])
            .is_err()
        );
    }

    fn env(state: Option<&str>, root: bool, home: Option<&str>, xdg: Option<&str>) -> DataDirEnv {
        DataDirEnv {
            state_directory: state.map(String::from),
            root,
            home: home.map(String::from),
            xdg_data_home: xdg.map(String::from),
        }
    }

    #[test]
    fn systemd_state_directory_wins() {
        let dir = default_data_dir(&env(
            Some("/var/lib/defter:/var/lib/other"),
            true,
            None,
            None,
        ));
        assert_eq!(dir, PathBuf::from("/var/lib/defter"));
    }

    #[test]
    fn root_uses_the_system_directory() {
        let dir = default_data_dir(&env(None, true, Some("/root"), None));
        assert_eq!(dir, PathBuf::from(SYSTEM_DATA_DIR));
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn a_mac_user_gets_application_support() {
        let dir = default_data_dir(&env(None, false, Some("/Users/me"), Some("/ignored")));
        assert_eq!(
            dir,
            PathBuf::from("/Users/me/Library/Application Support/defter")
        );
    }

    #[cfg(not(target_os = "macos"))]
    #[test]
    fn a_linux_user_gets_xdg_data_home() {
        let xdg = default_data_dir(&env(None, false, Some("/home/me"), Some("/data")));
        assert_eq!(xdg, PathBuf::from("/data/defter"));
        let local = default_data_dir(&env(None, false, Some("/home/me"), Some("relative")));
        assert_eq!(local, PathBuf::from("/home/me/.local/share/defter"));
    }

    #[test]
    fn no_home_falls_back_to_the_system_directory() {
        assert_eq!(
            default_data_dir(&env(None, false, None, None)),
            PathBuf::from(SYSTEM_DATA_DIR)
        );
    }
}