#!/bin/bash

# handler_invoice - builds PDF invoices from quotes
#
# This script is part of the PenText framework
#                            https://pentext.org
#
#    Copyright (C) 2016      Radically Open Security
#                            https://www.radicallyopensecurity.com
#
#                 Author(s): Peter Mosmans
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.


VERSION=0.5
DOCBUILDER=/usr/local/bin/docbuilder.py
TEMPLOC=$(mktemp -d)
DATESTAMP=$(date +"%Y-%m-%d")
INVOICE="00/000"

# These variables should be set environment-specific
[ -z $GITSERVER ] && GITSERVER=gitlab.local
[ -z $GITWEB ] && GITWEB=https://${GITSERVER}
[ -z $NAMESPACE ] && NAMESPACE=ros
BRANCH=master
TARGET=quote

# Read standard 'command line' variables
[[ ! -z $1 ]] && REPO=$1
[[ ! -z $2 ]] && INVOICE=$2

# Reading positional parms is a bit ugly, shifting parms or getopt would be nicer
if [[ ! -z $3 ]]; then
    if [[ ! $3 == -* ]]; then
        NAMESPACE=$3
    else
        PARMS=$3
    fi
fi
if [[ ! -z $4 ]]; then
    if [[ ! $3 == -* ]]; then
        BRANCH=$4
    else
        PARMS="$PARMS $4"
    fi
fi
if [[ $# -ge 5 ]]; then
    shift 4
    PARMS="$PARMS $@"
fi
trap cleanup EXIT QUIT

# Make sure that the temporary files are always removed
cleanup() {
    trap '' EXIT INT QUIT
    [ -d $TEMPLOC ] && rm -rf $TEMPLOC &>/dev/null
    exit
}

# As quote used to be called offerte or offer,
# this function retains backward compatibility - v0.2
backwards_compatible() {
    if [[ $TARGET == "quote" ]] && [ ! -f $TARGET.xml ]; then
        TARGET="offerte"
    fi
}

# Clones repo using global (!) variables - v0.2
clone_repo() {
    pushd $TEMPLOC 1>/dev/null
    git clone -b $BRANCH --depth=1 -q ssh://git@${GITSERVER}/${NAMESPACE}/${REPO}.git &>/dev/null
    if [ ! -d $TEMPLOC/$REPO ]; then
        echo "[-] could not clone repo ${NAMESPACE}/${REPO}"
        exit 1
    else
        cd $REPO
    fi
}

# Preflight checks using global (!) variables - v0.2
preflight_checks() {
    if [ -z $REPO ]; then
        echo "Usage: invoice REPOSITORY [INVOICE_NUMBER [NAMESPACE [BRANCH]]] [-v]"
        exit
    fi
    if [ ! -f $DOCBUILDER ]; then
        echo "[-] this script needs docbuilder.py ($DOCBUILDER)"
    fi
}

build() {
    if [ ! -d source ]; then
        echo "[-] missing necessary pentext framework files"
        exit 1
    fi
    pushd source &>/dev/null
    backwards_compatible
    targetpdf=target/invoice-latest.pdf
    $DOCBUILDER -c -i $TARGET.xml -o ../$targetpdf -x ../xslt/generate_invoice.xsl -invoice "$INVOICE" -date $DATESTAMP --fop ../target/invoice.fo $PARMS
    if [[ $? -ne 0 ]]; then
        echo "[-] Sorry, failed to generate $targetpdf"
        exit 1
    fi
    popd &>/dev/null
    if [ ! -f target/invoice-latest.pdf ]; then
        echo "[-] hmmm... failed to build PDF file (could not find $targetpdf)"
        exit 1
    fi
}

add_to_repo() {
    git add target/invoice-latest.pdf
    git commit -q -m "Invoice $INVOICE automatically generated using ChatOps" &>/dev/null
    git push -q >/dev/null
}

preflight_checks
echo "invoice v$VERSION - Congrats, another project from conception to ka-CHING"
clone_repo
build
add_to_repo
echo "[+] listo! Check out $GITWEB/$NAMESPACE/$REPO/raw/$BRANCH/$targetpdf"
