#!/bin/bash
#
# Creates an archive suitable for distribution (standard layout for binaries,
# libraries, etc.) from a release archive. Assumes that .

set -e

# Special overrides to allow passing in Scala version-specific args
if [ -n "$1" ]; then
    SCALA_VERSION="$1"
fi
if [ -n "$2" ]; then
    DESTDIR="$2"
fi

if [ -z "${VERSION}" -o -z "${SOURCE_VERSION}" -o -z "${SCALA_VERSION}" -o -z "${DESTDIR}" -o -z "${CONFLUENT_VERSION}" -o -z "${SKIP_TESTS}" ]; then
    echo "VERSION, SOURCE_VERSION, SCALA_VERSION, DESTDIR, CONFLUENT_VERSION, and SKIP_TESTS environment variables must be set."

    exit 1
fi

if [ -z ${SYSCONFDIR} ]; then
    SYSCONFDIR="${PREFIX}/etc/kafka"
fi

if `echo ${SCALA_VERSION} | grep '^2[.]8[.]' >/dev/null` || `echo ${SCALA_VERSION} | grep '^2[.]9[.]' >/dev/null`; then
    SCALA_VERSION_SHORT=${SCALA_VERSION}
else
    SCALA_VERSION_SHORT=`echo ${SCALA_VERSION} | awk -F. '{print $1"."$2}'`
fi

BINPATH=${PREFIX}/bin
LIBPATH=${PREFIX}/share/java/kafka
DOCPATH=${PREFIX}/share/doc/kafka
FILESTREAM_CONNECTORS_PATH=${PREFIX}/share/filestream-connectors

# systemd's service path lives outside of /usr,
# but we need to honour archive-targets zip/tar-file component prefix
if [[ ${PREFIX} == /usr ]]; then
    # non-archive standard /usr prefix, ignore the prefix.
    SYSTEMDPATH=/lib/systemd/system
else
    # prefix is component-VERSION..
    SYSTEMDPATH=${PREFIX}/lib/systemd/system
fi

INSTALL="install -D -m 644"
INSTALL_X="install -D -m 755"
TMP_ARCHIVE_PATH="kafka_archive_tmp"

rm -rf ${DESTDIR}${PREFIX}
mkdir -p ${DESTDIR}${PREFIX}
mkdir -p ${DESTDIR}${BINPATH}
mkdir -p ${DESTDIR}${LIBPATH}
mkdir -p ${DESTDIR}${DOCPATH}
mkdir -p ${DESTDIR}${SYSCONFDIR}

# Install systemd service files
for svc in debian/*.service ; do
    ${INSTALL} -o root -g root -D $svc ${DESTDIR}${SYSTEMDPATH}/$(basename $svc)
done
 
###
### Kafka
###
rm -rf ${TMP_ARCHIVE_PATH}
mkdir -p ${TMP_ARCHIVE_PATH}
tar -xf core/build/distributions/kafka_${SCALA_VERSION_SHORT}-${SOURCE_VERSION}.tgz -C ${TMP_ARCHIVE_PATH} --strip-components 1

${INSTALL_X} -o root -g root ${TMP_ARCHIVE_PATH}/bin/connect-* ${DESTDIR}${BINPATH}/
${INSTALL_X} -o root -g root ${TMP_ARCHIVE_PATH}/bin/kafka-* ${DESTDIR}${BINPATH}/
${INSTALL_X} -o root -g root ${TMP_ARCHIVE_PATH}/bin/zookeeper-* ${DESTDIR}${BINPATH}/
${INSTALL_X} -o root -g root ${TMP_ARCHIVE_PATH}/bin/trogdor* ${DESTDIR}${BINPATH}/
if [ "${INCLUDE_WINDOWS_BIN}" == "yes" ]; then
    mkdir -p ${DESTDIR}${BINPATH}/windows
    ${INSTALL_X} -o root -g root ${TMP_ARCHIVE_PATH}/bin/windows/*.bat ${DESTDIR}${BINPATH}/windows/
fi

# CC-15522: Move filestream connectors to a separate directory so that they are not available on Connect workers out of the box
mkdir ${DESTDIR}${FILESTREAM_CONNECTORS_PATH}
${INSTALL} -o root -g root ${TMP_ARCHIVE_PATH}/libs/connect-file-* ${DESTDIR}${FILESTREAM_CONNECTORS_PATH}
rm ${TMP_ARCHIVE_PATH}/libs/connect-file-*

${INSTALL} -o root -g root ${TMP_ARCHIVE_PATH}/libs/* ${DESTDIR}${LIBPATH}/
for path in ${TMP_ARCHIVE_PATH}/config/*; do
    base=$(basename ${path})
    if [[ -d "${path}" ]]; then
        mkdir -p -m 0755 ${DESTDIR}${SYSCONFDIR}/${base}/
        ${INSTALL} -o root -g root -t ${DESTDIR}${SYSCONFDIR}/${base}/ ${path}/*
    else
        ${INSTALL} -o root -g root ${path} ${DESTDIR}${SYSCONFDIR}/${base}
    fi
done

${INSTALL} -o root -g root ${TMP_ARCHIVE_PATH}/LICENSE* ${DESTDIR}${DOCPATH}/
${INSTALL} -o root -g root ${TMP_ARCHIVE_PATH}/NOTICE* ${DESTDIR}${DOCPATH}/

ln -s ./kafka_${SCALA_VERSION_SHORT}-${SOURCE_VERSION}.jar ${DESTDIR}${LIBPATH}/kafka.jar # symlink for unversioned access to jar

${INSTALL} -o root -g root ${TMP_ARCHIVE_PATH}/libs/kafka-clients-${SOURCE_VERSION}.jar ${DESTDIR}${LIBPATH}/
${INSTALL} -o root -g root ${TMP_ARCHIVE_PATH}/libs/kafka-streams-${SOURCE_VERSION}.jar ${DESTDIR}${LIBPATH}/

rm -rf ${TMP_ARCHIVE_PATH}


if [[ $CREATE_VAR_LOG_KAFKA == y ]]; then
    mkdir -p ${DESTDIR}/var/log/kafka
fi
