#!/usr/bin/env bash
set -x

unset LD_LIBRARY_PATH
SOFTWARE=${SOFTWARE:-puppet}
VERSION=${VERSION:-0.24.8}
PKGVERSION=${VERSION}\,REV=$(date +%Y.%m.%d)
DESC=${DESC:-"a system configuration tool"}
TMP=${TMP:-~/blastwave/tmp}
SRC=${SRC:-~/blastwave/src}
BUILD=${BUILD:-~/blastwave/build}
OUTPUT=${OUTPUT:-~/blastwave/output}
SVN=${SVN:-/opt/csw/bin/svn}
PKGDIR=${PKGDIR:-~/blastwave/pkg}
TAG=$(echo REL_${VERSION} | sed -e 's/\./\_/g')
PREFIX=${PREFIX:-/opt/csw}
USER=$(perl -e '$user = getpwuid($<); print $user')
GROUP=$(perl -e '$gid = getgrgid($(); print $gid')
RUBY=${PREFIX}/bin/ruby
FIND=${FIND:-${PREFIX}/bin/gfind}
TAR=${PREFIX}/bin/gtar
SED=${PREFIX}/bin/gsed
HOTLINE=http://www.opencsw.org/bugtrack/
EMAIL=glaw@opencsw.org


VARDIR=${VARDIR:-/var/${PREFIX}/${SOFTWARE}}
CONFDIR=${CONFDIR:-/etc/${PREFIX}/${SOFTWARE}}

FATAL() {
    echo $1
    echo "Fatal Error: $1"
    echo $1
    exit $2
    }

# get the required version of the software and untar into build directory
mkdir -p ${TMP} ${SRC} ${BUILD} ${OUTPUT}
rm -rf ${TMP}/${SOFTWARE}*  ${BUILD}/${SOFTWARE}* ${PKGDIR}/${SOFTWARE}*


cd ${BUILD}  || FATAL "cannot cd into build directory! exiting..." 3
${TAR} xzf ${SRC}/${SOFTWARE}-${VERSION}.tar.gz || FATAL "cannot untar source...
 exiting..." 2

# create pkginfo file

mkdir -p ${PKGDIR}/${SOFTWARE}/
cd ${PKGDIR}/${SOFTWARE}
echo "PKG=CSW${SOFTWARE}
NAME=${SOFTWARE} - ${DESC}
ARCH=all
VERSION=${PKGVERSION}
CATEGORY=application,system
VENDOR=http://reductivelabs.com/projects/${SOFTWARE} packaged for CSW by Gary Law and Mark Phillips
HOTLINE=${HOTLINE}
EMAIL=${EMAIL}" > pkginfo

# copyright file

cp ${BUILD}/${SOFTWARE}-${VERSION}/COPYING ${PKGDIR}/${SOFTWARE}/copyright

grep ${VERSION} ${PKGDIR}/${SOFTWARE}/pkginfo || FATAL "pkginfo version mismatch! exiting..." 1

# build the depend file

cd ${PKGDIR}/${SOFTWARE}
echo 'P CSWfacter facter >= 1.5.2' > depend
cd

# build the checkinstall script

cd ${PKGDIR}/${SOFTWARE}
echo 'exit 0' > checkinstall

# preinstall scripts
cat > preinstall << EOF
PKG_INSTALL_ROOT=\${PKG_INSTALL_ROOT:-/}
\$PKG_INSTALL_ROOT/usr/sbin/chroot \$PKG_INSTALL_ROOT/ /usr/bin/getent group puppet > /dev/null || /usr/sbin/chroot \$PKG_INSTALL_ROOT /usr/sbin/groupadd puppet
\$PKG_INSTALL_ROOT/usr/sbin/chroot \$PKG_INSTALL_ROOT/ /usr/bin/getent passwd puppet > /dev/null || /usr/sbin/chroot \$PKG_INSTALL_ROOT /usr/sbin/useradd -g puppet -c "Puppet" -d ${VARDIR} -s "/bin/true" puppet

mkdir -p \$PKG_INSTALL_ROOT/${CONFDIR}
chown root:bin \$PKG_INSTALL_ROOT/${CONFDIR}
EOF

# postinstall scripts
cat > postinstall << EOF
PKG_INSTALL_ROOT=\${PKG_INSTALL_ROOT:-/}
/usr/bin/test -d \$PKG_INSTALL_ROOT/opt/csw/etc/puppet || /usr/sbin/chroot \$PKG_INSTALL_ROOT /usr/bin/ln -s ${CONFDIR} ${PREFIX}/etc/puppet
/usr/bin/test -d \$PKG_INSTALL_ROOT/etc/puppet || /usr/sbin/chroot \$PKG_INSTALL_ROOT /usr/bin/ln -s ${CONFDIR} /etc/puppet
/usr/bin/test -d \$PKG_INSTALL_ROOT/opt/csw/var/puppet || /usr/sbin/chroot \$PKG_INSTALL_ROOT /usr/bin/ln -s /${VARDIR} /opt/csw/var/puppet
/usr/bin/test -d \$PKG_INSTALL_ROOT/var/puppet || /usr/sbin/chroot \$PKG_INSTALL_ROOT /usr/bin/ln -s /${VARDIR} /var/puppet
touch \$PKG_INSTALL_ROOT/${CONFDIR}/puppet.conf
/usr/bin/test -f \$PKG_INSTALL_ROOT/opt/csw/etc/csw.conf && . \$PKG_INSTALL_ROOT/opt/csw/etc/csw.conf 
/usr/bin/test -f \$PKG_INSTALL_ROOT/etc/opt/csw/csw.conf && . \$PKG_INSTALL_ROOT/etc/opt/csw/csw.conf 
if [ "\$autoenable_daemons" != "no" ] ; then
    /usr/sbin/chroot \$PKG_INSTALL_ROOT /usr/sbin/svccfg import /opt/csw/var/svc/manifest/puppetd.xml || /bin/true
    /usr/sbin/chroot \$PKG_INSTALL_ROOT /usr/sbin/svcadm enable svc:network/puppetd  || /bin/true
fi
EOF

# preremove
cat > preremove << EOF
/usr/sbin/svcadm disable svc:network/puppetmasterd 2>/dev/null || /bin/true
/usr/sbin/svccfg delete svc:network/puppetmasterd 2>/dev/null || /bin/true
/usr/sbin/svcadm disable svc:network/puppetd 2>/dev/null || /bin/true
/usr/sbin/svccfg delete svc:network/puppetd 2>/dev/null || /bin/true
EOF

# postremove
cat > postremove << EOF
/usr/sbin/svcadm disable svc:network/puppetmasterd 2>/dev/null || /bin/true
/usr/sbin/svccfg delete svc:network/puppetmasterd 2>/dev/null || /bin/true
/usr/sbin/svcadm disable svc:network/puppetd 2>/dev/null || /bin/true
/usr/sbin/svccfg delete svc:network/puppetd 2>/dev/null || /bin/true
rm /etc/puppet || /bin/true
rm /opt/csw/etc/puppet || /bin/true
rm /var/puppet || /bin/true
rm /opt/csw/var/puppet || /bin/true
rm -rf /opt/csw/lib/ruby/site_ruby/1.8/puppet
EOF

# install the software

cd ${BUILD}/${SOFTWARE}-${VERSION}/
${SED} -e "s|/var/puppet|${VARDIR}|g" < ./lib/puppet/defaults.rb > ./lib/puppet/defaults.rb.csw && mv ./lib/puppet/defaults.rb{.csw,} 
${SED} -e "s|/etc/puppet|${CONFDIR}|g" < ./lib/puppet/defaults.rb > ./lib/puppet/defaults.rb.csw && mv ./lib/puppet/defaults.rb{.csw,} 
${SED} -e "s|/etc/puppet|${CONFDIR}|g" < ./conf/solaris/smf/puppetd.xml > ./conf/solaris/smf/puppetd.xml.csw && mv ./conf/solaris/smf/puppetd.xml{.csw,}

DESTDIR=${PKGDIR}/${SOFTWARE}/ ${RUBY} install.rb
mkdir -p ${PKGDIR}/${SOFTWARE}/${CONFDIR}
mkdir -p ${PKGDIR}/${SOFTWARE}/${VARDIR}/run/
mkdir -p ${PKGDIR}/${SOFTWARE}/${PREFIX}/var/svc/manifest/
mkdir -p ${PKGDIR}/${SOFTWARE}/${PREFIX}/lib/svc/method/
mkdir -p ${PKGDIR}/${SOFTWARE}/${PREFIX}/share/man/man8
cd  ${PKGDIR}/${SOFTWARE}/${PREFIX}/share/man/man8
gunzip *
cd -
install -c ${PKGDIR}/${SOFTWARE}/${PREFIX}/var/svc/manifest/ ./conf/solaris/smf/puppetd.xml 
install -c ${PKGDIR}/${SOFTWARE}/${PREFIX}/var/svc/manifest/ ./conf/solaris/smf/puppetmasterd.xml 
install -c ${PKGDIR}/${SOFTWARE}/${PREFIX}/lib/svc/method/  ./conf/solaris/smf/svc-puppetd 
install -c ${PKGDIR}/${SOFTWARE}/${PREFIX}/lib/svc/method/  ./conf/solaris/smf/svc-puppetmasterd 
install -m 644 -c ${PKGDIR}/${SOFTWARE}/${CONFDIR} ./examples/root/etc/puppet/*
cd ${PKGDIR}/${SOFTWARE}/${CONFDIR}
for f in $(ls ${PKGDIR}/${SOFTWARE}/${CONFDIR})
do
    if [ -f $f ]
    then
        touch $f
        mv $f $f.example
    fi
done
cd

# build the package

cd ${PKGDIR}/${SOFTWARE}/

cat > prototype << EOF
i pkginfo
i depend
i checkinstall
i copyright
i preinstall
i postinstall
i preremove
i postremove
d none ${PREFIX}/lib/svc 0755 root bin
d none /opt/csw/var/svc  0755 root bin
d none /etc/opt  0755 root bin
d none /etc/${PREFIX}  0755 root bin
d none /var/opt 0755 root bin
d none /var/opt/csw 0755 root bin
EOF

${FIND} ${PKGDIR}/${SOFTWARE}/${PREFIX} | pkgproto ${PKGDIR}/${SOFTWARE}/${PREFIX}=/${PREFIX} | sed -e "s|${USER} ${GROUP}|root bin|g" >> prototype
${FIND} ${PKGDIR}/${SOFTWARE}/${CONFDIR} | pkgproto ${PKGDIR}/${SOFTWARE}/${CONFDIR}=/${CONFDIR} | sed -e "s|${USER} ${GROUP}|root bin|g" >> prototype
${FIND} ${PKGDIR}/${SOFTWARE}/${VARDIR} | pkgproto ${PKGDIR}/${SOFTWARE}/${VARDIR}=/${VARDIR} | sed -e "s|${USER} ${GROUP}|root bin|g" >> prototype

pkgmk -o -d $OUTPUT -r / -a all
filename=${SOFTWARE}-${PKGVERSION}-SunOS`uname -r`-all-CSW.pkg
pkgtrans -s $OUTPUT $OUTPUT/$filename CSW${SOFTWARE}


