From a9b6c2bd68ccfd72a833355fce55feda914fe3bd Mon Sep 17 00:00:00 2001 From: Luca Matei Pintilie Date: Wed, 13 Sep 2023 23:59:09 +0200 Subject: Add MMV_NAME and MCP_NAME compile-time variables Due to the sheer popularity of the "mmv" name it might conflict with other programs installed on the user's computer. As such the MMV_NAME and MCP_NAME environmental variables can control the program's name at compile time, allowing the user to avoid name conflicts with other programs. If the variables are not set then they will silently default back to "mmv" and "mcp" respectively. Signed-off-by: Luca Matei Pintilie Co-authored-by: Thomas Voss --- Makefile | 14 ++++++++++---- README.md | 18 ++++++++++++++++++ src/main.rs | 13 ++++++++++--- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index c49dd38..131acc5 100644 --- a/Makefile +++ b/Makefile @@ -6,13 +6,19 @@ MANDIR = ${DPREFIX}/share/man target = target/release/mmv +mmv = $${MMV_NAME:-mmv} +mcp = $${MCP_NAME:-mcp} + all: ${target} ${target}: src/main.rs cargo build --release install: mkdir -p ${DPREFIX}/bin ${DPREFIX}/share/man/man1 - cp ${target} ${DPREFIX}/bin/mmv - cp ${target} ${DPREFIX}/bin/mcp - cp mmv.1 ${MANDIR}/man1 - ln -srf ${MANDIR}/man1/mmv.1 ${MANDIR}/man1/mcp.1 + cp ${target} ${DPREFIX}/bin/${mmv} + cp mmv.1 ${MANDIR}/man1/${mmv}.1 + ln -srf ${DPREFIX}/bin/${mmv} ${DPREFIX}/bin/${mcp} + ln -srf ${MANDIR}/man1/${mmv}.1 ${MANDIR}/man1/${mcp}.1 + +clean: + rm -rf target diff --git a/README.md b/README.md index ce25f4f..2066f2f 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,24 @@ $ make $ sudo make install ``` +The following environment variables can also be set at compile-time to modify +the names of the generated binaries: + +- `$MMV_NAME` + + The name of the file-moving binary (default is `mmv`). This is also used + to name the backups folder in `$XDG_CACHE_HOME`. +- `$MCP_NAME` + + The name of the file-copying binary (default is `mcp`). + +If you are compiling with a custom binary name, you want to make sure that the +environment variables actually get used when performing a `make install`. If +you’re using `sudo`, you want to do this with the `-E` flag. + +``` +$ MMV_NAME=mmv-rs MCP_NAME=mcp-rs make +$ MMV_NAME=mmv-rs MCP_NAME=mcp-rs sudo -E make install +``` + ## Examples and Documentation To avoid repeating myself everywhere, if you would like to see usage examples diff --git a/src/main.rs b/src/main.rs index e8fb883..9885a82 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,6 +19,10 @@ use { tempfile::tempdir, }; + +const MMV_DEFAULT_NAME: &str = "mmv"; +const MCP_DEFAULT_NAME: &str = "mcp"; + struct Flags { pub backup: bool, pub dryrun: bool, @@ -54,7 +58,8 @@ impl Flags { let argv0 = env::args().next().unwrap(); let p = Path::new(&argv0).file_name().unwrap(); - if p == "mcp" { + let mcp_name = option_env!("MCP_NAME").unwrap_or(MCP_DEFAULT_NAME); + if p == mcp_name { flags.mcp = true; flags.backup = false; } @@ -85,7 +90,8 @@ fn usage(bad_flags: Option) -> ! { } let argv0 = env::args().next().unwrap(); let p = Path::new(&argv0).file_name().unwrap(); - if p == "mcp" { + let mcp_name = option_env!("MCP_NAME").unwrap_or(MCP_DEFAULT_NAME); + if p == mcp_name { eprintln!("Usage: {} [-0deiv] command [argument ...]", p.to_str().unwrap()); } else { eprintln!("Usage: {} [-0deinv] command [argument ...]", p.to_str().unwrap()); @@ -173,9 +179,10 @@ fn work() -> Result<(), io::Error> { let cache_base = env::var("XDG_CACHE_HOME").unwrap_or_else(|_| { err!("XDG_CACHE_HOME variable must be set"); }); + let mmv_name = option_env!("MMV_NAME").unwrap_or(MMV_DEFAULT_NAME); cache_dir = [ Path::new(cache_base.as_str()), - Path::new("mmv"), + Path::new(mmv_name), Path::new(ts.as_str()), ] .iter() -- cgit v1.2.3