#!/bin/bash

# handler_quote - sets up a quote gitlab repository using PenText
#
# 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
#                            John Sinteur
#
#    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.7

# These variables should be set environment-specific
[ -z $GITLABCLI ] && GITLABCLI=gitlab
[ -z $GITSERVER ] && GITSERVER=gitlab.local
[ -z $NAMESPACE ] && NAMESPACE=ros
[ -z $NAMESPACEID ] && NAMESPACEID=1
[ -z $PENTEXTREPO ] && PENTEXTREPO=https://github.com/radicallyopensecurity/pentext
PREFIX="off-"
pentext=$(echo $PENTEXTREPO|awk -F '/' '{print $5}')
TEMPLOC=$(mktemp -d)

# Read standard 'command line' variables
[[ ! -z $1 ]] && REPO=$PREFIX$1
REPO=${REPO,,} # follow git specs: lowercase
[[ ! -z $2 ]] && NAMESPACE=$2

trap cleanup EXIT QUIT

# Make sure that the temporary files are always removed
cleanup() {
    trap '' EXIT INT QUIT
    # remove repo if not finished successfully
    if [ -z $finished ] && [ ! -z $project_id ]; then
        $GITLABCLI project delete --id $project_id
        echo "[-] deleted project $project_id"
    fi
    [ -d $TEMPLOC ] && rm -rf $TEMPLOC &>/dev/null
    exit
}

# Clones repo using global (!) variables - v0.3
clone_repo() {
    pushd $TEMPLOC 1>/dev/null
    git clone --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
preflight_checks() {
    if [ -z $REPO ]; then
        echo "Usage: startquote PROJECT_NAME"
        exit
    fi
    if ! which $GITLABCLI &>/dev/null; then
        echo "[-] this script needs the gitlab command line interface (python-gitlab)"
    fi
}

setup_repo() {
    project_id=$($GITLABCLI project create --name $REPO --namespace $NAMESPACEID --issues-enabled true --wiki-enabled true --snippets-enabled true --wall-enabled true --merge-requests-enabled true 2>/dev/null| awk '/id:/{print $2}')
    if [ ! -z $project_id ]; then
        echo "[+] successfully created gitlab project $REPO with id ${project_id}"
    else
        echo "[-] could not create repo $NAMESPACE/$REPO"
        exit 1
    fi
}

# Add standard templates using global (!) variables - v0.2
add_templates() {
    [ -d $TEMPLOC/$pentext ] && rm -rf $TEMPLOC/$pentext &>/dev/null
    pushd $TEMPLOC 1>/dev/null && git clone --depth=1 $PENTEXTREPO &>/dev/null && popd 1>/dev/null

    if [ ! -d $TEMPLOC/$pentext ]; then
        echo "[-] could not clone (and therefore add) pentext repo $TEMPLATEREPO"
        exit 1
    else
        clone_repo
        # copy the framework
        cp -r $TEMPLOC/$pentext/xml/* .
        # remove the docs
        rm -r doc &>/dev/null
    fi
}

add_to_repo() {
    git add * &>/dev/null
    git commit -q -m "Initialized quote repository with PenText using ChatOps" &>/dev/null
    git push -q > /dev/null
}

preflight_checks
echo "startquote v${VERSION} - Humbly setting up your quote framework..."
setup_repo
add_templates
add_to_repo
echo "[+] listo!"
finished=true
