| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 | #!/bin/bash
check_env() {
    #
    # Check that environment variables have been set properly
    #
    PREFIX="$(readlink -m "$PREFIX")"
    test -d "$PREFIX" || die "PREFIX points to non-existent directory: $PREFIX"
}
deploy_item() {
    #
    # Deploy item and make it look like wanted
    #
    # usage: deploy_item src dst [mode]
    #
    # Both src and dst must be names of actual items[1],
    # whereas dst must not exist.  On update, dst is
    # usually to be replaced but that is uninstall's
    # job!
    #
    #  [1] Ie. src=foo and dst=/foo/bar does *always*
    #      mean that foo will become 'bar'.  This is
    #      different than traditional `cp` behavior,
    #      when this depends if 'bar' already exists
    #      as a directory.
    #
    # If mode is omitted or empty, MKIT_DEFAULT_MODE is
    # used instead.
    #
    # Directories are copied recursively, and mode is
    # applied only to files.
    #
    local src="$1"
    local dst="$2"
    local mode="${3:-$MKIT_DEFAULT_MODE}"
    if test -d "$src";
    then
        cp -Tvr "$src" "$dst"
        find "$dst" -type f -print0 | xargs -0 chmod -c "$mode"
    else
        command -p install -DTvm "$mode" "$src" "$dst"
    fi
}
get_dst() {
    #
    # Find out target path for src file $2 of group $1
    #
    local grp=$1
    local src=$2
    echo "$(get_root "$grp")/$(ini 1value "files:$grp:$src")"
}
get_root() {
    #
    # Find out target rooot for group $1
    #
    local grp="$1"
    local root=$(ini 1value "roots:$grp")
    test -n "$root" || die "missing in config.ini: roots:$grp"
    echo "$root"
}
install() {
    #
    # Install product
    #
    check_env
    local dst group mode src
    ini values "lists:group" \
      | while read group;
        do
            mode=$(ini 1value "modes:$group")
            ini lskeys "files:$group" \
              | while read src;
                do
                    dst=$(get_dst "$group" "$src")
                    deploy_item "$src" "$dst" "$mode"
                done
        done
    test -f "$MKIT_LOCAL/autoclean" && clean
    true
}
uninstall() {
    #
    # Uninstall product
    #
    check_env
    local dst group src
    ini values "lists:group" \
      | while read group;
        do
            ini lskeys "files:$group" \
              | while read src;
                do
                    dst=$(get_dst "$group" "$src")
                    rm -vrf "$dst"
                done
        done
}
 |