#!/bin/sh
pkgmgr="/usr/sbin/packagemanager"

dir=/var/tmp/packagemanager
if [ -d /etc/puppet/env/production ]
then
	puppet=/etc/puppet/env/production
else
	puppet=/etc/puppet
fi

if [ -f $puppet/manifests/nodes/autonodes.pp ]
then
	nodes="$puppet/manifests/nodes/autonodes.pp"
fi

[ -n "$dir" ] || exit 1
[ -d "$dir" ] || mkdir "$dir" || exit 1
rm -rf "$dir/*"
mkdir -p "$dir/manifests/profiles" "$dir/modules/packagemanager/manifests" "$dir/manifests/nodes"

# $pkgmgr listlists
if [ "$1" = "autoupdate" ]
then
 $pkgmgr autoupdate
fi

$pkgmgr puppetprofiles "$dir/manifests/profiles"
$pkgmgr exportpuppet    "$dir/modules/packagemanager/manifests"
if [ -n "$nodes" ]
then
	$pkgmgr puppetnodes "$dir/manifests/nodes/autonodes.pp"
fi

mv $dir/manifests/profiles/* $puppet/manifests/profiles
mv $dir/modules/packagemanager/manifests/* $puppet/modules/packagemanager/manifests
[ -n "$nodes" ] && mv $dir/manifests/nodes/autonodes.pp "$nodes"
