use crate::logger::*;
use crate::paths::concat_paths;
use std::io;
pub fn run_init_config() -> io::Result<()> {
let home = std::env::var("HOME").unwrap_or_default();
let config_dir = concat_paths(&home, ".config/fp-dotfiles-manager");
let config_file_path = concat_paths(&config_dir, "config.yml");
log_section("Initializing Configuration...");
let path = std::path::Path::new(&config_file_path);
if path.exists() {
log_warn(&("Configuration file already exists at ".to_owned() + &config_file_path));
return Ok(());
}
std::fs::create_dir_all(&config_dir)?;
let default_config = r#"# ==============================================================================
# Setup & Usage Quick Tutorial
# ==============================================================================
# To start managing your dotfiles with fp-dotfiles-manager, here is what you need to do:
#
# 1. Configuration File:
# This configuration file is placed at ~/.config/fp-dotfiles-manager/config.yml
#
# 2. Tracked Files Lists:
# By default, the lists of files to track are read from:
# - Standard files: ~/.config/fp-dotfiles-manager/tracked
# - Sensitive files: ~/.config/fp-dotfiles-manager/tracked_encrypted
# Place these tracking lists (containing one relative path per line, e.g., .bashrc or .config/git/config)
# at those paths, or use `fp-dotfiles-manager add` to automatically append new files to them.
#
# 3. Post-Setup Bootstrap Hooks:
# Any numerical/alphabetical shell scripts (e.g. 01-packages.sh, 02-symlinks.sh) placed inside:
# ~/.config/fp-dotfiles-manager/bootstrap.d/
# will be executed automatically after setup completes. You can run them manually at any time with:
# fp-dotfiles-manager bootstrap
#
# 4. Sync & Backup:
# - To sync modifications, track newly discovered files, and purge untracked files, run: fp-dotfiles-manager sync
# - To safely archive your tracked dotfiles to a compressed backup, run: fp-dotfiles-manager backup [destination_path]
# ==============================================================================
# Tracking Wildcards, Exclusions, and Encryption Rules
# ==============================================================================
# fp-dotfiles-manager supports standard wildcards, granular exclusions, and
# automated file encryption overrides:
#
# 1. Wildcard and Glob Matches:
# - Use * to match paths, e.g., .config/git/*
# - Folders automatically resolve recursively.
#
# 2. Exclusions (Folder except specific files):
# - Prefix a pattern with ! to exclude it from recursive folder sync.
# - Example in your standard tracked file:
# .config/myfolder
# !.config/myfolder/tmp_*
#
# 3. Encryption Overrides (Folder with encrypted files):
# - If you add a folder to the standard tracked file, but list specific
# sensitive files inside that folder in tracked_encrypted, the engine
# automatically stages those specific files as ENCRYPTED in Chezmoi,
# while keeping the rest of the folder as PLAIN TEXT!
# ==============================================================================
# fp-dotfiles-manager Configuration
# ==============================================================================
# YAML configuration file for your custom dotfiles manager.
# Any of these values can be temporarily overridden by setting environment variables.
# Standard tracking file containing a list of standard home directory files
tracked_file: "~/.config/fp-dotfiles-manager/tracked"
# Encrypted tracking file containing sensitive file patterns (e.g. keys, configs)
tracked_enc_file: "~/.config/fp-dotfiles-manager/tracked_encrypted"
# Post-setup bootstrap scripts directory hooks (.sh scripts run alphabetically)
bootstrap_dir: "~/.config/fp-dotfiles-manager/bootstrap.d"
# Location of the GitLab/SSH key used for dynamic Chezmoi operations
ssh_key_path: "~/.config/fp-dotfiles-manager/ssh_gitlab"
# Passphrase for age encryption identity keys (blank for interactive decrypt)
age_pass: ""
# GitLab/GitHub dotfiles repository path
repo_path: "fptbb/dotfiles"
# Target SSH repository URL used during initialization and synchronization
repo_url: "git@gitlab.com:fptbb/dotfiles.git"
# Executable script hook run before sync (aborting sync on non-zero exit)
pre_sync_hook: "~/.config/fp-dotfiles-manager/hooks/pre-sync.sh"
# Executable script hook run after successful sync completion
post_sync_hook: "~/.config/fp-dotfiles-manager/hooks/post-sync.sh"
# Secret scanning coverage for plaintext files managed by Chezmoi.
# off - never ask Chezmoi to scan for leaked secrets
# warn - scan only the files being added during a run
# enforce - also re-scan the whole managed set, catching older leaks
# Findings are always advisory: they are reported but never block a sync.
# Run `fp-dotfiles-manager audit-secrets` to review already-managed files.
secret_scan: "enforce"
"#;
std::fs::write(&config_file_path, default_config)?;
log_success(
&("Successfully initialized default configuration file at ".to_owned() + &config_file_path),
);
Ok(())
}