#!/usr/bin/env bash
### Copyright 1999-2024. WebPros International GmbH. All rights reserved.

PN=$(basename "$0")

help()
{
    cat << EOF
Plesk reconfigurator - utility to change IP addresses used by Plesk

Usage: $PN { <map_file> | --autoconfigure | --remap-ips | --help }

If <map_file> doesn't exists - template will be created, otherwise it will be used to map IP addresses.

--autoconfigure option will attempt to create and process IP mapping automatically. Any new excessive
or old unmapped IP addresses will retain their status and would need to be handled manually either by
rereading IP addresses or by passing a correct map file to this utility.

--remap-ips is an alias for --autoconfigure option.

--help option displays this help page.
EOF
}

main()
{
    # shellcheck disable=SC2016
    echo '`plesk bin ipmanage` should be used instead of this utility' >&2
    if [ "$1" = "--autoconfigure" ] || [ "$1" = "--remap-ips" ]; then
        "/usr/local/psa/bin/ipmanage" --auto-remap
    elif [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
        help
        exit 1
    else
        "/usr/local/psa/bin/ipmanage" --remap "$@"
    fi

    exit $?
}

main "$@"
