#!/bin/bash
set -x
set -e 
VERSION=$1 
DISTDIR=ogcore-$VERSION/opt/opengnsys/ogcore
export COMPOSER_ALLOW_SUPERUSER=1
export APP_ENV=prod
rm -rf ogcore-$VERSION

cd ogcore 
composer dump-env prod
composer install --no-dev --no-interaction --no-progress --optimize-autoloader
cd ..

# Crear directorios necesarios
mkdir -p $DISTDIR/api
mkdir -p $DISTDIR/etc/nginx/certs
mkdir -p $DISTDIR/etc/nginx/conf

for i in bin config migrations public src swagger-assets templates translations var vendor ; do
  cp -pr ogcore/$i $DISTDIR/api/$i
done
cp -pr ogcore/DEBIAN ogcore-$VERSION/
cp -pr ogcore/etc $DISTDIR/
cp ogcore/docker/certs/* $DISTDIR/etc/nginx/certs/
chmod 755 ogcore-$VERSION/DEBIAN/postinst
chmod 755 ogcore-$VERSION/DEBIAN/preinst
cp ogcore/composer.json $DISTDIR/api
cp ogcore/composer.lock $DISTDIR/api
cp ogcore/symfony.lock $DISTDIR/api
cp ogcore/.env.local.php $DISTDIR/api
cp ogcore/env.json $DISTDIR/api

sed -i "s/%%VERSION%%/$VERSION/g" ogcore-$VERSION/DEBIAN/control
# Imprimir el tag actual
echo "Empaquetando TAG $TAG"
rm -f ogcore-$VERSION.deb 
dpkg-deb --build ogcore-$VERSION
