#!/bin/env bash

SCRIPT=$0;
SCRIPT_NAME=`basename $SCRIPT`;
SCRIPT_DIR=$(readlink -f "$(dirname $SCRIPT)");
WORK_DIR=$(pwd);
PROJECT_DIR="$(readlink -f $SCRIPT_DIR/..)";

BUILD_DIR="$PROJECT_DIR/build";

source "$PROJECT_DIR/lib/version.bash";


mkdir -p $BUILD_DIR;
rm -f $BUILD_DIR/*;

deb_version="$ODOO_HELPER_VERSION";

if [ -z "$deb_version" ]; then
    echo "Version is not specified";
    exit 1;
fi

echo "Building package for version '$deb_version'...";

deb_depends="git wget lsb-release 
    procps libevent-dev g++ libpq-dev libsass-dev
    python3-dev python3-virtualenv libjpeg-dev libyaml-dev
    libfreetype6-dev zlib1g-dev libxml2-dev libxslt-dev bzip2
    libsasl2-dev libldap2-dev libssl-dev libffi-dev fontconfig
    libmagic1";
deb_depends_opt=$(for dep in $deb_depends; do echo "--depends $dep"; done);

fpm -s dir -t deb \
    --package "$BUILD_DIR/odoo-helper-scripts.deb" \
    --name odoo-helper-scripts \
    --description "Just a simple collection of odoo scripts. mostly to ease addons development process (allows to install and manage odoo instances in virtualenv)" \
    --config-files /etc/odoo-helper.conf \
    --vendor "Dmytro Katyukha" \
    --maintainer "Dmytro Katyukha" \
    --url "https://katyukha.gitlab.io/odoo-helper-scripts/" \
    --category "utils" \
    --iteration ubuntu\
    --architecture all \
    --version $deb_version  \
    $deb_depends_opt \
    --license "Mozilla Public License, v. 2.0" \
    --deb-recommends expect-dev \
    --deb-recommends tcl8.6 \
    $PROJECT_DIR/bin/=/usr/local/bin/ \
    $PROJECT_DIR/lib/=/opt/odoo-helper-scripts/lib/ \
    $PROJECT_DIR/CHANGELOG.md=/opt/odoo-helper-scripts/ \
    $PROJECT_DIR/LICENSE=/opt/odoo-helper-scripts/ \
    $PROJECT_DIR/defaults/odoo-helper.conf=/etc/
