#!/bin/bash

# Temporary user for deployment
DEPLOY_USER="<add-your-gitlab-user>"

# Token for access to repositories
TOKEN="<add-your-gitlab-user-token>"

# Names of modules and Jmeter repos
JMETER="jmeter"
SERVER_EMULATOR="jmeter-serveremulator-plugin"
DDOS="jmeter-ddos-plugin"
NETWORK_ANALYZER="jmeter-netanalyzer-plugin"
REPORT_GENERATOR="jmeter-reportgenerator-plugin"
ADVANCED_THREAD_GROUP="jmeter-advancedthreadgroups-plugin"
NET_EMULATOR="jmeter-netemulator-plugin"
WEB_GENERATOR="jmeter-webgenerator-plugin"

# All modules which is used as array
MODULES="$SERVER_EMULATOR $DDOS $NETWORK_ANALYZER $REPORT_GENERATOR $ADVANCED_THREAD_GROUP $NET_EMULATOR $WEB_GENERATOR"

# Java download link for openjdk
JAVA_DOWNLOAD_LINK=https://download.java.net/java/GA/jdk17.0.2/dfd4a8d0985749f896bed50d7138ee7f/8/GPL/openjdk-17.0.2_linux-x64_bin.tar.gz
JAVA_NAME="jdk-17.0.2"

# Libs for modules
# Net analyzer
JFREE_CHART=https://repo1.maven.org/maven2/org/jfree/jfreechart/1.5.3/jfreechart-1.5.3.jar
# Net analyzer save to pcap
PCAP4J=https://repo1.maven.org/maven2/org/pcap4j/pcap4j-core/1.8.2/pcap4j-core-1.8.2.jar
PCAP4J_JNA_DEPENDENCY=https://repo1.maven.org/maven2/net/java/dev/jna/jna/5.11.0/jna-5.11.0.jar
PCAP4J_SLF4J_DEPENDENCY=https://repo1.maven.org/maven2/org/slf4j/slf4j-api/1.7.36/slf4j-api-1.7.36.jar
# Net analyzer pdf signature
PDF_BOX="https://repo1.maven.org/maven2/org/apache/pdfbox/pdfbox/2.0.26/pdfbox-2.0.26.jar"
PDF_FONTS="https://repo1.maven.org/maven2/org/apache/pdfbox/fontbox/2.0.26/fontbox-2.0.26.jar"
BOUNTYCASTLE_BCPROV="https://repo1.maven.org/maven2/org/bouncycastle/bcprov-jdk15on/1.70/bcprov-jdk15on-1.70.jar"
# DDoS IPv6
IPV6="https://repo1.maven.org/maven2/com/github/seancfoley/ipaddress/5.3.4/ipaddress-5.3.4.jar"

MODULES_LIBS="${JFREE_CHART} ${PCAP4J} ${PCAP4J_JNA_DEPENDENCY} ${PCAP4J_SLF4J_DEPENDENCY} ${PDF_BOX} ${PDF_FONTS} ${BOUNTYCASTLE_BCPROV} ${IPV6}"

# Set help text
usage="./$(basename "$0") [-h/--help] [-p/--build_path <path>] [-j/--java_path <path>] [-d/--download_jdk] [-b/--build_jmeter]

where:
    -h/--help 		Show this help text. Optional.
    -p/--build_path 	Home path for jmeter and modules repositories, defualt is home directory. Optional.
    -j/--java_path 	Path to java when located on system. When not specified system java is used. Optional.
    -d/--download_jdk 	Download openjdk java and use it. Optional.
    -b/--build_jmeter 	Build jmeter. When not specified only modules are build. Optional."

# Set arguments
script_name=$(basename "$0")
short=:p:j:dbh
long=build_path:,java_path:,download_jdk,build_jmeter,help

# Init getopt with arguments
TEMP=$(getopt -o $short --long $long --name "$script_name" -- "$@")
eval set -- "${TEMP}"

# Set flags
download_jdk=false
build_jmeter=false

# Init paths
BUILD_PATH="~/jmeter-main"
JAVA_PATH=""

# Clone module repository take one argument which is repo name
clone_repository() {
    eval cd $BUILD_PATH
    if [ ! -d $1 ]; then
        git clone "https://${DEPLOY_USER}:${TOKEN}@gitlab.com/but-projects/ict-tester/jmeter-plugins/$1.git"
    else
        cd $1
        git fetch
        git rebase origin/main
    fi
}

# Build the module 
build_repo() {
    eval cd "${BUILD_PATH}/$1"
    echo "Building $1 in $(pwd)"
    git status
    ./gradlew build
}

download_dependency_files() {
    wget -O "${JMETER_PATH_TO_LIBS}/$(basename $1)" $1
}

# Copy build modules to jmeter and create zip
copy_and_zip() {
    eval cd $BUILD_PATH
    local JMETER_PATH="$(pwd)"
    local JMETER_PATH_TO_LIBS="${JMETER_PATH}/${JMETER}/lib/ext"
    local MODULE_GRADLE_BUILD="build/libs"

    for i in $MODULES_LIBS; do
        download_dependency_files $i
    done

    cp -t $JMETER_PATH_TO_LIBS "${JMETER_PATH}/${SERVER_EMULATOR}/${MODULE_GRADLE_BUILD}/ServerEmulator.jar"
    cp -t $JMETER_PATH_TO_LIBS "${JMETER_PATH}/${DDOS}/${MODULE_GRADLE_BUILD}/Ddos.jar"
    cp -t $JMETER_PATH_TO_LIBS "${JMETER_PATH}/${NETWORK_ANALYZER}/${MODULE_GRADLE_BUILD}/NetAnalyzer.jar"
    cp -t $JMETER_PATH_TO_LIBS "${JMETER_PATH}/${REPORT_GENERATOR}/${MODULE_GRADLE_BUILD}/ReportGenerator.jar"
    cp -t $JMETER_PATH_TO_LIBS "${JMETER_PATH}/${ADVANCED_THREAD_GROUP}/${MODULE_GRADLE_BUILD}/AdvancedThreadGroups.jar"
    cp -t $JMETER_PATH_TO_LIBS "${JMETER_PATH}/${NET_EMULATOR}/${MODULE_GRADLE_BUILD}/NetEmulator.jar"
    cp -t $JMETER_PATH_TO_LIBS "${JMETER_PATH}/${WEB_GENERATOR}/${MODULE_GRADLE_BUILD}/WebGenerator.jar" 

    cp -rn $JAVA_HOME $JMETER_PATH
    echo "$(basename ${JAVA_HOME})" > "java_version.txt"
    echo "Creating zip file."
    zip -q -r jmeter.zip "${JMETER}" "$(basename ${JAVA_HOME})" "java_version.txt"
}

# Parse arguments
while :; do
    case "${1}" in
    -j | --java_path) java_path=$2;
    	if [[ ${java_path::1} == "-" ]]; then
                echo "$usage" >&2
                exit 1
        fi
        shift 2;;
    -p | --build_path) build_path=$2;
        if [[ ${build_path::1} == "-" ]]; then
                echo "$usage" >&2
                exit 1
        fi
        shift 2;;
    -d | --download_jdk) download_jdk=true; shift;;
    -b | --build_jmeter) build_jmeter=true; shift;;
    -h | --help) echo "$usage" >&2; exit ;;
    --) shift; break ;;
    esac
done

# Set directory for repository to download
if [[ $build_path != '' ]]; then
    BUILD_PATH=$build_path
else
    mkdir -p ~/jmeter-main
fi

# Download or select java to use
if [[ $download_jdk == true ]]; then
    eval cd $BUILD_PATH
    wget $JAVA_DOWNLOAD_LINK -O "${JAVA_NAME}.tar.gz"
    tar -zxvf "${JAVA_NAME}.tar.gz"
    JAVA_PATH="$(pwd)/${JAVA_NAME}"
elif [[ $java_path != '' ]]; then
    JAVA_PATH=$java_path
else
    JAVA_PATH=$(dirname $(dirname $(readlink -f $(which java))))
fi

eval "JAVA_HOME=${JAVA_PATH}" export JAVA_HOME

echo "Path to jmeter and module is ${BUILD_PATH}"
echo "JAVA_HOME is ${JAVA_HOME}"

#init and build Jmeter repository
if [[ $build_jmeter == true ]];then
    eval cd $BUILD_PATH
    pwd
    if [ ! -d $JMETER ]; then
        git clone "https://${DEPLOY_USER}:${TOKEN}@gitlab.com/but-projects/ict-tester/${JMETER}.git"
    else
        cd $JMETER
        git fetch
        git rebase origin/main
    fi

    eval cd "${BUILD_PATH}/${JMETER}"
    echo "Building ${JMETER} in $(pwd)"
    git status
    ./gradlew build
fi	

# Init and build repositories
echo "Cloning and building modules"
for i in $MODULES; do
    clone_repository $i
    build_repo $i
done

copy_and_zip
