···1616#
1717# See comments on each function for more details.
18181919-# installManPage <path> [...<path>]
1919+# installManPage [--name <path>] <path> [...<path>]
2020#
2121# Each argument is checked for its man section suffix and installed into the appropriate
2222# share/man/man<n>/ directory. The function returns an error if any paths don't have the man
2323# section suffix (with optional .gz compression).
2424+#
2525+# Optionally accepts pipes as input, which when provided require the `--name` argument to
2626+# name the output file.
2727+#
2828+# installManPage --name foobar.1 <($out/bin/foobar --manpage)
2429installManPage() {
2525- local path
2626- for path in "$@"; do
2727- if test -z "$path"; then
3030+ local arg name='' continueParsing=1
3131+ while { arg=$1; shift; }; do
3232+ if (( continueParsing )); then
3333+ case "$arg" in
3434+ --name)
3535+ name=$1
3636+ shift || {
3737+ nixErrorLog "${FUNCNAME[0]}: --name flag expected an argument"
3838+ return 1
3939+ }
4040+ continue;;
4141+ --name=*)
4242+ # Treat `--name=foo` that same as `--name foo`
4343+ name=${arg#--name=}
4444+ continue;;
4545+ --)
4646+ continueParsing=0
4747+ continue;;
4848+ esac
4949+ fi
5050+5151+ nixInfoLog "${FUNCNAME[0]}: installing $arg${name:+ as $name}"
5252+ local basename
5353+5454+ # Check if path is empty
5555+ if test -z "$arg"; then
5656+ # It is an empty string
2857 nixErrorLog "${FUNCNAME[0]}: path cannot be empty"
2958 return 1
3059 fi
3131- nixInfoLog "${FUNCNAME[0]}: installing $path"
3232- local basename
3333- basename=$(stripHash "$path") # use stripHash in case it's a nix store path
6060+6161+ if test -n "$name"; then
6262+ # Provided name. Required for pipes, optional for paths
6363+ basename=$name
6464+ elif test -p "$arg"; then
6565+ # Named pipe requires a file name
6666+ nixErrorLog "${FUNCNAME[0]}: named pipe requires --name argument"
6767+ else
6868+ # Normal file without a name
6969+ basename=$(stripHash "$arg") # use stripHash in case it's a nix store path
7070+ fi
7171+7272+ # Check that it is well-formed
3473 local trimmed=${basename%.gz} # don't get fooled by compressed manpages
3574 local suffix=${trimmed##*.}
3675 if test -z "$suffix" -o "$suffix" = "$trimmed"; then
3737- nixErrorLog "${FUNCNAME[0]}: path missing manpage section suffix: $path"
7676+ nixErrorLog "${FUNCNAME[0]}: path missing manpage section suffix: $arg"
3877 return 1
3978 fi
7979+8080+ # Create the out-path
4081 local outRoot
4182 if test "$suffix" = 3; then
4283 outRoot=${!outputDevman:?}
4384 else
4485 outRoot=${!outputMan:?}
4586 fi
4646- local outPath="${outRoot}/share/man/man$suffix/$basename"
4747- install -D --mode=644 --no-target-directory "$path" "$outPath"
8787+ local outPath="${outRoot}/share/man/man$suffix/"
8888+ nixInfoLog "${FUNCNAME[0]}: installing to $outPath"
8989+9090+ # Install
9191+ if test -p "$arg"; then
9292+ # install doesn't work with pipes on Darwin
9393+ mkdir -p "$outPath" && cat "$arg" > "$outPath/$basename"
9494+ else
9595+ install -D --mode=644 --no-target-directory -- "$arg" "$outPath/$basename"
9696+ fi
9797+9898+ # Reset the name for the next page
9999+ name=
48100 done
49101}
50102