#!/bin/bash
set -e

LTS_VERSION="22"
NODE_VERSION=$( node --version )
NODE_VERSION_WITHOUT_V=$( echo ${NODE_VERSION} | cut -c2-10 )
COMMIT=$( node scripts/details.js COMMIT )
PACKAGE_VERSION=$( node scripts/details.js VERSION )
PACKAGE_NAME=$( node scripts/details.js NAME )
OS=$( node scripts/details.js OS )
UWS_VERSION=$( node scripts/details.js UWS_VERSION )
PACKAGE_DIR=build/${PACKAGE_VERSION}
DEEPSTREAM_PACKAGE=${PACKAGE_DIR}/deepstream.io
GIT_BRANCH=$( git rev-parse --abbrev-ref HEAD )
CREATE_DISTROS=false

EXECUTABLE_NAME="build/deepstream"

# Needed even for void builds for travis deploy to pass
mkdir -p build

if ! [[ ${NODE_VERSION_WITHOUT_V} == ${LTS_VERSION}* ]]; then
    echo "Packaging only done on $LTS_VERSION.x"
    exit
fi

if [[ -z $1  ]]; then
    if ! [[ ${GIT_BRANCH} = 'master' ]]; then
        echo "Running on branch ${GIT_BRANCH}"
    else
        echo "Running on master"
    fi
fi

if [[ $2 ]]; then
    echo 'Ignoring distros'
elif [[ ${OS} = "linux" ]]; then
    CREATE_DISTROS=true
    echo "Checking if FPM is installed"
    fpm --version
fi

function compile {
    echo "Starting deepstream.io packaging with Node.js $NODE_VERSION_WITHOUT_V"
    rm -rf build
    mkdir build

    echo "Installing missing npm packages, just in case something changes"
    npm i

    echo "Transpiling"
    npm run tsc

    echo "Generate License File using unmodified npm packages"
    ./scripts/license-aggregator.js > build/DEPENDENCIES.LICENSE

    echo "Generating meta.json"
    node scripts/details.js META

    # Creating package structure
    rm -rf build/${PACKAGE_VERSION}
    mkdir -p ${DEEPSTREAM_PACKAGE}
    mkdir ${DEEPSTREAM_PACKAGE}/var
    mkdir ${DEEPSTREAM_PACKAGE}/lib
    mkdir ${DEEPSTREAM_PACKAGE}/doc

    cd ${DEEPSTREAM_PACKAGE}/lib
    echo '{ "name": "TEMP" }' > package.json

    echo "Adding uWebSockets.js to libs"
    npm install --omit=dev --install-strategy=shallow uWebSockets.js@${UWS_VERSION}

    echo "Adding cache plugins"
    npm install --omit=dev --install-strategy=shallow \
        @deepstream/cache-redis \
        @deepstream/cache-memcached \
        # @deepstream/cache-hazelcast

    echo "Adding cluster plugins"
    npm install --omit=dev --install-strategy=shallow \
        @deepstream/clusternode-redis

    echo "Adding storage plugins"
    npm install --omit=dev --install-strategy=shallow \
        @deepstream/storage-mongodb \
        @deepstream/storage-rethinkdb \
        @deepstream/storage-elasticsearch \
        @deepstream/storage-postgres

    echo "Adding logger plugins"
    npm install --omit=dev --install-strategy=shallow \
        @deepstream/logger-winston

    mv node_modules/* .
    rm -rf node_modules package.json
    cd -

    echo "Creating '$EXECUTABLE_NAME', this will take a while..."
    LTS=${LTS_VERSION} OS=${OS} EXECUTABLE_NAME=${EXECUTABLE_NAME} node scripts/pkg.js

    PROC_ID=$!
    SECONDS=0;
    while kill -0 "$PROC_ID" >/dev/null 2>&1; do
        echo -ne "\rCompiling deepstream... ($SECONDS)"
        sleep 10
    done

    echo ""

    if wait ${pid}; then
        echo -e "\tPkg Build Succeeded"
    else
        echo -e "\tPkg Build Failed"
        exit 1
    fi

    echo "Adding docs"
    echo -e "\tAdding Readme"
    echo "Documentation is available at https://deepstreamhub.com/open-source
    " > ${DEEPSTREAM_PACKAGE}/doc/README
    echo -e "\tAdding Changelog"
    cp CHANGELOG.md ${DEEPSTREAM_PACKAGE}/doc/CHANGELOG.md
    echo -e "\tAdding Licenses"
    curl -L https://raw.githubusercontent.com/nodejs/node/v22.x/LICENSE -o ${DEEPSTREAM_PACKAGE}/doc/NODE.LICENSE
    mv build/DEPENDENCIES.LICENSE ${DEEPSTREAM_PACKAGE}/doc/LICENSE

    echo "Moving deepstream into package structure at $DEEPSTREAM_PACKAGE"
    cp -r conf ${DEEPSTREAM_PACKAGE}/
    cp build/deepstream ${DEEPSTREAM_PACKAGE}/

    echo "Patching config file for zip lib and var directories"
    cp -f ./conf/config.yml ${DEEPSTREAM_PACKAGE}/conf/config.yml

    if [[ ${OS} = "darwin" ]]; then
        sed -i '' 's@#libDir: ../lib@libDir: ../lib@' ${DEEPSTREAM_PACKAGE}/conf/config.yml
    else
        sed -i 's@#libDir: ../lib@libDir: ../lib@' ${DEEPSTREAM_PACKAGE}/conf/config.yml
    fi
}

function windows {
    COMMIT_NAME="deepstream.io-windows-$PACKAGE_VERSION-$COMMIT.zip "
    CLEAN_NAME="deepstream.io-windows-$PACKAGE_VERSION.zip"

    echo "OS is windows"
    echo -e "\tCreating zip deepstream.io-windows-$PACKAGE_VERSION.zip"
    cd ${DEEPSTREAM_PACKAGE}
    7z a ../${COMMIT_NAME} . > /dev/null
    cp ../${COMMIT_NAME} ../${CLEAN_NAME}
    cd -
}

function mac {
    COMMIT_NAME="deepstream.io-mac-${PACKAGE_VERSION}-${COMMIT}"
    CLEAN_NAME="deepstream.io-mac-${PACKAGE_VERSION}"

    echo "OS is mac"
    echo -e "\tCreating ${CLEAN_NAME}"

    cd ${DEEPSTREAM_PACKAGE}
    zip -r ../${COMMIT_NAME}.zip .
    cp ../${COMMIT_NAME}.zip ../${CLEAN_NAME}.zip
    cd -

    rm -rf build/osxpkg
    mkdir -p build/osxpkg/bin
    mkdir -p build/osxpkg/etc/deepstream
    mkdir -p build/osxpkg/lib/deepstream
    mkdir -p build/osxpkg/share/doc/deepstream
    mkdir -p build/osxpkg/var/log/deepstream

    cp -r ${DEEPSTREAM_PACKAGE}/deepstream build/osxpkg/bin/deepstream
    cp -r ${DEEPSTREAM_PACKAGE}/conf/* build/osxpkg/etc/deepstream
    cp -r ${DEEPSTREAM_PACKAGE}/lib/* build/osxpkg/lib/deepstream
    cp -r ${DEEPSTREAM_PACKAGE}/doc/* build/osxpkg/share/doc/deepstream

    echo "Patching config file for lib and var directories"
    sed -i '' 's@ ../lib@ /usr/local/lib/deepstream@' build/osxpkg/etc/deepstream/config.yml
    sed -i '' 's@ ../var@ /usr/local/var/log/deepstream@' build/osxpkg/etc/deepstream/config.yml

    cp build/osxpkg/etc/deepstream/config.yml build/osxpkg/etc/deepstream/config.defaults

    chmod -R 777 build/osxpkg/bin
    chmod -R 777 build/osxpkg/share
    chmod -R 777 build/osxpkg/var
    chmod -R 777 build/osxpkg/lib
    chmod -R 777 build/osxpkg/etc

    echo "\tCreating *.pkg"
    pkgbuild \
        --root build/osxpkg \
        --identifier deepstream.io \
        --version ${PACKAGE_VERSION} \
        --info scripts/resources/PackageInfo \
        --install-location /usr/local \
        ${DEEPSTREAM_PACKAGE}/../${COMMIT_NAME}.pkg

    cp \
      ${DEEPSTREAM_PACKAGE}/../${COMMIT_NAME}.pkg \
      ${DEEPSTREAM_PACKAGE}/../${CLEAN_NAME}.pkg

    rm -rf build/osxpkg
}

function linux {
    echo "OS is linux"

    echo -e "\tCreating tar.gz"

    COMMIT_NAME="deepstream.io-linux-${PACKAGE_VERSION}-${COMMIT}.tar.gz"
    CLEAN_NAME="deepstream.io-linux-${PACKAGE_VERSION}.tar.gz"

    cd ${DEEPSTREAM_PACKAGE}
    tar czf ../${COMMIT_NAME} .
    cp ../${COMMIT_NAME} ../${CLEAN_NAME}
    cd -
}

function distros {
    echo -e "\tPatching config file for linux distros..."

    if [[ ${OS} = "darwin" ]]; then
        sed -i '' 's@ ../lib@ /var/lib/deepstream@' ${DEEPSTREAM_PACKAGE}/conf/config.yml
        sed -i '' 's@ ../var@ /var/log/deepstream@' ${DEEPSTREAM_PACKAGE}/conf/config.yml
    else
        sed -i 's@ ../lib@ /var/lib/deepstream@' ${DEEPSTREAM_PACKAGE}/conf/config.yml
        sed -i 's@ ../var@ /var/log/deepstream@' ${DEEPSTREAM_PACKAGE}/conf/config.yml
    fi

    echo -e "\t\tCreating rpm"

    fpm \
        -s dir \
        -t rpm \
        --package ./build/ \
        --package-name-suffix ${COMMIT} \
        -n deepstream.io \
        -v ${PACKAGE_VERSION} \
        --license "MIT" \
        --vendor "deepstreamHub GmbH" \
        --description "deepstream.io rpm package" \
        --url https://deepstream.io/ \
        -m "<info@deepstreamhub.com>" \
        --after-install ./scripts/resources/daemon/after-install \
        --before-remove ./scripts/resources/daemon/before-remove \
        --before-upgrade ./scripts/resources/daemon/before-upgrade \
        --after-upgrade ./scripts/resources/daemon/after-upgrade \
        -f \
        ${DEEPSTREAM_PACKAGE}/doc/=/usr/share/doc/deepstream/ \
        ${DEEPSTREAM_PACKAGE}/conf/=/etc/deepstream/conf.d/ \
        ${DEEPSTREAM_PACKAGE}/lib/=/var/lib/deepstream/ \
        ./build/deepstream=/usr/bin/deepstream

    echo -e "\t\tCreating deb"
    fpm \
        -s dir \
        -t deb \
        --package ./build \
        --package-name-suffix ${COMMIT} \
        -n deepstream.io \
        -v ${PACKAGE_VERSION} \
        --license "MIT" \
        --vendor "deepstreamHub GmbH" \
        --description "deepstream.io deb package" \
        --url https://deepstream.io/ \
        -m "<info@deepstreamhub.com>" \
        --after-install ./scripts/resources/daemon/after-install \
        --before-remove ./scripts/resources/daemon/before-remove \
        --before-upgrade ./scripts/resources/daemon/before-upgrade \
        --after-upgrade ./scripts/resources/daemon/after-upgrade \
        -f \
        --deb-no-default-config-files \
        ${DEEPSTREAM_PACKAGE}/doc/=/usr/share/doc/deepstream/ \
        ${DEEPSTREAM_PACKAGE}/conf/=/etc/deepstream/conf.d/ \
        ${DEEPSTREAM_PACKAGE}/lib/=/var/lib/deepstream/ \
        ./build/deepstream=/usr/bin/deepstream
}

function clean {
    rm -rf ${DEEPSTREAM_PACKAGE}
}

compile

if [[ $OS = "win32" ]]; then
    windows
elif [[ ${OS} = "darwin" ]]; then
    mac
elif [[ ${OS} = "linux" ]]; then
    linux
    if [[ ${CREATE_DISTROS} = true ]]; then
        distros
    fi
fi

clean

echo "Files in build directory are $( ls build/ )"
echo "Done"
