#!/bin/bash
#
# cz-puppet-basepath -- Set the puppet basemodulepath
#
# Written by Bill MacAllister <bill@ca-zephyr.org>
# Copyright (c) 2023 Bill MacAllister <bill@ca-zephyr.org>

##############################################################################
# Subroutines
##############################################################################

function display_usage {
    echo "Usage: cz-puppet-basepath set|help|manual"
    exit 1
}

##############################################################################
# Main routine
##############################################################################

export PATH=/bin:/sbin:/usr/bin:/usr/sbin

if [ "$1" = "" ]
then
    display_usage
fi

# Set configuration defaults and read configuration
# file is present.
BASE_DIR="/afs/@cell/service/puppet"
if [ -e /etc/cz-puppet.conf ]
then
    source /etc/cz-puppet.conf
fi

case $1 in
    help)
        display_usage
        ;;
    manual)
        pod2text $0
        exit 1
        ;;
    set)
        puppet_conf="/etc/puppet/puppet.conf"
        if [ -e $puppet_conf ]
        then
            rm -v $puppet_conf
            echo "# Puppet Apply Configuration" > $puppet_conf
            echo "[main]" > $puppet_conf
        fi
        ppath="${BASE_DIR}/code/modules"
        ppath+=':/etc/puppet/code/modules'
        ppath+=':/usr/share/puppet/modules'
        puppet config set basemodulepath "$ppath"
        puppet config print basemodulepath
        ;;
    *)
        echo "ERROR: unknown action '$1'"
        display_usage
        ;;
esac

exit

DOCS=<<__END_OF_DOCS__

=head1 NAME

cz-puppet-basepath - Set the puppet basemodulepath

=head1 SYNOPSIS

cz-puppet-basepath set|help|manual

=head1 DESCRIPTION

This script set the Puppet configuration property "basemodulepath".
Setting the basemodulepath allows the inclusion of Puppet modules
outside of the default Puppet locations of /etc/puppet/code/modules
and /usr/share/puppet/modules.  Typlically this is run before 
the cz-puppet script.  The script uses the /etc/cz-puppet.conf
file if present.

=head1 ARGUMENTS

=over 4

=item help

Display usage information.

=item manual

Display the man page for this script.

=back

=head1 CONFIGURATION

The optional configuration file /etc/cz-puppet.conf is a simple bash
fragment file.

=head2 Configuration Properties

=over 4

=item BASE_DIR

The BASE_DIR is the path to the top of the Puppet manifests.

=back 

=head1 SEE ALSO

cz-puppet

=head1 COPYRIGHT

Copyright (c) 2023 Bill MacAllister <bill@ca-zephyr.org>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

=head1 AUTHORS

Bill MacAllister <whm@dropbox.com>

=cut

__END_OF_DOCS__


