#!/bin/bash

DONT_EXECUTE_ITSELF=$1

PWD=`dirname $0`

HAS_NEW_ISENTIONS=`cat /opt/pos/etc/config.ini | grep VAT_ISENTION=M20_`

POSTGRES_VERSION=`find /etc/postgresql/ | grep pg_hba.conf | cut -d/ -f4 | head -1`

# install new vat isentions
if [ "$HAS_NEW_ISENTIONS" = "" ]; then
  echo "VAT_ISENTION=M00_Sem Isenção" >> /tmp/$$.cfg
  echo "VAT_ISENTION=M01_Artigo 16. n. 6 do CIVA (ou similar)" >> /tmp/$$.cfg
  echo "VAT_ISENTION=M02_Artigo 6. do Decreto-Lei n. 198/90, de 19 de Junho" >> /tmp/$$.cfg
  echo "VAT_ISENTION=M03_Exigibilidade de caixa" >> /tmp/$$.cfg
  echo "VAT_ISENTION=M04_Isento Artigo 13. do CIVA (ou similar)" >> /tmp/$$.cfg
  echo "VAT_ISENTION=M05_Isento Artigo 14. do CIVA (ou similar)" >> /tmp/$$.cfg
  echo "VAT_ISENTION=M06_Isento Artigo 15. do CIVA (ou similar)" >> /tmp/$$.cfg
  echo "VAT_ISENTION=M07_Isento Artigo 9. do CIVA (ou similar)" >> /tmp/$$.cfg
  echo "VAT_ISENTION=M08_IVA - autoliquidação" >> /tmp/$$.cfg
  echo "VAT_ISENTION=M09_IVA - não confere direito a dedução" >> /tmp/$$.cfg
  echo "VAT_ISENTION=M10_IVA - Regime de isenção" >> /tmp/$$.cfg
  echo "VAT_ISENTION=M11_Regime particular do tabaco" >> /tmp/$$.cfg
  echo "VAT_ISENTION=M12_Regime da margem de lucro - Agências de viagens" >> /tmp/$$.cfg
  echo "VAT_ISENTION=M13_Regime da margem de lucro - Bens em segunda mão" >> /tmp/$$.cfg
  echo "VAT_ISENTION=M14_Regime da margem de lucro - Objetos de arte" >> /tmp/$$.cfg
  echo "VAT_ISENTION=M15_Regime da margem de lucro - Objetos coleção e antiguidade" >> /tmp/$$.cfg
  echo "VAT_ISENTION=M16_Isento Artigo 14. do RITI (ou similar)" >> /tmp/$$.cfg
  echo "VAT_ISENTION=M20_IVA - Regime forfetário" >> /tmp/$$.cfg
  echo "VAT_ISENTION=M99_Não sujeito; não tributado (ou similar)" >> /tmp/$$.cfg
  cat /opt/pos/etc/config.ini | grep -v "VAT_ISENTION=" >> /tmp/$$.cfg
  mv /tmp/$$.cfg /opt/pos/etc/config.ini
fi

rm /opt/bin/releases 2> /dev/null
ln -s /opt/pos/common/bin/releases /opt/bin/releases

cp $PWD/network.sh /opt/bin
cp $PWD/usb_keyboard_install /opt/bin

# if db changes are published, apply them
if [ -e "/opt/pos/doc/db_changes.postgres" ]; then
  psql -U root pos < /opt/pos/doc/db_changes.postgres
  psql -U root pos < /opt/pos/doc/db_changes.postgres
  rm /opt/pos/doc/db_changes.postgres
fi

HAS_SYSCTL=`cat /etc/sysctl.conf | grep tcp_tw_reuse`

if [ "$HAS_SYSCTL" = "" ]; then
  echo "net.ipv4.tcp_fin_timeout = 30" >> /etc/sysctl.conf
  echo "net.ipv4.tcp_tw_reuse = 1" >> /etc/sysctl.conf
  echo "net.ipv4.tcp_tw_recycle = 0" >> /etc/sysctl.conf
  echo "net.ipv4.tcp_keepalive_time = 300" >> /etc/sysctl.conf
  echo "net.ipv4.tcp_keepalive_probes =  3" >> /etc/sysctl.conf
  echo "net.ipv4.tcp_keepalive_intvl = 30" >> /etc/sysctl.conf
  echo "net.ipv4.tcp_syncookies = 1" >> /etc/sysctl.conf
  sysctl -a -p
fi

HAS_SYSCTL=`cat /etc/sysctl.conf | grep "net.ipv4.tcp_fin_timeout = 30"`

if [ "$HAS_SYSCTL" != "" ]; then
  perl -pi -e "s/net\.ipv4\.tcp_fin_timeout = 30/net.ipv4.tcp_fin_timeout = 2/g" /etc/sysctl.conf
  echo "net.ipv4.tcp_orphan_retries = 1" >> /etc/sysctl.conf
  sysctl -a -p
fi

HAS_MCT_MODULES=`cat /etc/modules | grep mct_u232`

if [ "$HAS_MCT_MODULES" != "" ]; then
  cat /etc/modules | grep -v mct_u232 > /tmp/ttt.txt
  mv /tmp/ttt.txt /etc/modules
  depmod -a
  reboot
fi

#
# cleanup /etc/modules
#

HAS_MODULES=0
HAS_USBLP=`cat /etc/modules | grep usblp | grep -v grep`
HAS_USBSERIAL=`cat /etc/modules | grep usbserial | grep -v grep`

if [ "$HAS_USBLP" != "" ]; then
  HAS_MODULES=1
fi

if [ "$HAS_USBSERIAL" != "" ]; then
  HAS_MODULES=1
fi

if [ "$HAS_MODULES" = "1" ]; then
  perl -pi -e "s/usblp//g" /etc/modules
  perl -pi -e "s/usbserial//g" /etc/modules
  depmod -a
  reboot
fi

# safer postgres
HAS_SYNCHRONOUS_COMMIT=`cat /etc/postgresql/$POSTGRES_VERSION/main/postgresql.conf | grep "synchronous_commit = on"`

if [ "$HAS_SYNCHRONOUS_COMMIT" = "" ]; then
  perl -pi -e 's/synchronous_commit = off/synchronous_commit = on/' /etc/postgresql/$POSTGRES_VERSION/main/postgresql.conf*
fi

rm /opt/pos/common/skins/PT/reports/01_daily_simple/11_offers_by_group.txt 2> /dev/null
rm /opt/pos/common/skins/PT/reports/02_daily_detailed/11_offers_by_group.txt 2> /dev/null
rm /opt/pos/common/skins/PT/reports/03_daily_clerk/11_offers_by_group.txt 2> /dev/null
rm /opt/pos/common/skins/PT/reports/04_dayclose/11_offers_by_group.txt 2> /dev/null
rm /opt/pos/common/skins/PT/reports/04_dayclose/04_vat_payed.txt 2> /dev/null
rm /opt/pos/common/skins/PT/reports/03_daily_clerk/04_vat_payed.txt 2> /dev/null
rm /opt/pos/common/skins/PT/reports/02_daily_detailed/04_vat_payed.txt 2> /dev/null
rm /opt/pos/common/skins/PT/reports/01_daily_simple/04_vat_payed.txt 2> /dev/null

# remove hp rules
if [ -e "/etc/udev/rules.d/40-hplip.rules" ]; then
  rm /etc/udev/rules.d/40-hplip.rules 2> /dev/null
  rm /etc/udev/rules.d/56-hpmud_add_printer.rules 2> /dev/null
  rm /etc/udev/rules.d/56-hpmud_support.rules 2> /dev/null
  rm /etc/udev/rules.d/86-hpmud_plugin.rules 2> /dev/null
fi

if [ -e "/opt/pos/.machine_id" ]; then
  rm /opt/pos/.machine_id
fi

#if [ -e "/opt/pos/install/bashrc" ]; then
#  cp /opt/pos/install/bashrc /root/.bashrc
#fi

SKIN=`cat /opt/pos/etc/config.ini | grep LANG= | grep -v "#" | cut -f2 -d"="`

if [ -e "/opt/pos/common/bin/pos.gambas" ]; then
  rm /opt/pos/common/bin/pos.gambas
fi

# old skin cleanup
if [ -e "/opt/pos/common/skins/$SKIN/db_export.pl" ]; then
  echo "UPDATE clerks SET skin = ''" > /tmp/sql_$$.txt
  psql -U root pos < /tmp/sql_$$.txt
  rm /tmp/sql_$$.txt

  rm /opt/pos/common/skins/$SKIN/db_export.pl
  rm /opt/pos/common/skins/$SKIN/db_import
  rm /opt/pos/common/skins/$SKIN/machine_configurator.cfg
  rm -rf /opt/pos/common/skins/$SKIN/translations/*
fi

if [ -d "/opt/pos/codigo" ]; then
  rm -rf /opt/pos/codigo
fi

# clean a wrongly inserted report (48_vat_saft.txt)
find /opt/pos/common/reports | grep 48_vat_saft | xargs rm -f

#if [ "$DONT_EXECUTE_ITSELF" = "" ]; then
  reindexdb -U root pos -t sales_headers
  reindexdb -U root pos -t sales_payments
  reindexdb -U root pos -t sales_details
  reindexdb -U root pos -t sales_headers_aux
  reindexdb -U root pos -t sales_hashes
  reindexdb -U root pos -t counters

  #/opt/pos/install/install.sh DONT_EXECUTE_ITSELF
#fi

if [ ! -e "/opt/pos/etc/.fix_sh_inserted_dates.txt" ]; then
  /opt/pos/server/apps/fix_sh_inserted_dates.pl 2> /dev/null
fi

# ###################################################################
# change postgres stats folder - BEGIN
# ###################################################################

STATS_REPLACE_DID=0

# postgresql.conf
if [ -e "/etc/postgresql/$POSTGRES_VERSION/main/postgresql.conf" ]; then
  OLD_STATS=`cat /etc/postgresql/$POSTGRES_VERSION/main/postgresql.conf | grep "/tmp/pg_stat_tmp"`

  if [ "$OLD_STATS" != "" ]; then
    echo "##> postgresql.conf"
    sed -i 's/\/tmp\/pg_stat_tmp/\/run\/postgresql/' /etc/postgresql/$POSTGRES_VERSION/main/postgresql.conf
    chown postgres:postgres /etc/postgresql/$POSTGRES_VERSION/main/postgresql.conf
    STATS_REPLACE_DID=1
  fi
fi

# ###################################################################
# change postgres stats folder - END
# ###################################################################

rm /opt/pos/lib/App/POS/Printer/W_*.pm

# makes sure fluxbox has only one workspace
perl -pi -e "s/session\.screen0\.workspaces:.*4/session.screen0.workspaces: 1/" /root/.fluxbox/init

#HAS_OLD_GMAIL_METHOD=`cat /opt/pos/etc/config.ini | grep INTERNET_GMAIL`

#if [ "$HAS_OLD_GMAIL_METHOD" != "" ]; then
#  cat /opt/pos/etc/config.ini | grep -v INTERNET_GMAIL > /tmp/$$
#  mv /tmp/$$ /opt/pos/etc/config.ini
#fi

SSH_HAS_NODNS=`cat /etc/ssh/sshd_config | grep UseDNS`

if [ "$SSH_HAS_NODNS" = "" ]; then
  echo "UseDNS no" >> /etc/ssh/sshd_config
  /etc/init.d/ssh restart
fi

SSH_HAS_StrictHostKeyChecking=`cat /etc/ssh/ssh_config | grep "StrictHostKeyChecking no"`

if [ "$SSH_HAS_StrictHostKeyChecking" = "" ]; then
  echo "    StrictHostKeyChecking no" >> /etc/ssh/ssh_config
fi

HAS_NOATIME=`cat /etc/fstab | grep ext4 | grep "noatime,nodiratime"`

if [ "$HAS_NOATIME" = "" ]; then
  perl -pi -e 's/errors=/noatime,nodiratime,errors=/' /etc/fstab
fi

# install gambas3 version if gambas3 compiler is available
if [ -e "/usr/bin/gbx3" ] && [ -e "$PWD/pos.3" ]; then
  cp $PWD/../common/bin/pos /opt/pos/common/bin/pos.2
  mv $PWD/pos.3 /opt/pos/common/bin/pos
  chmod 755 /opt/pos/common/bin/pos
fi

SPEAKER_BLACKLISTED=`cat /etc/modprobe.d/blacklist.conf | grep "# blacklist pcspkr"`

if [ "$SPEAKER_BLACKLISTED" = "" ]; then
  perl -pi -e "s/blacklist pcspkr/# blacklist pcspkr/g" /etc/modprobe.d/blacklist.conf
  modprobe pcspkr
fi

if [ -e "/opt/pos/common/skins/$SKIN/sign_server" ]; then
  if [ -e "/opt/pos/server/apps/cm_fix.pl" ]; then
    /opt/pos/server/apps/cm_fix.pl
  fi
fi

if [ ! -e "/usr/bin/evtest" ]; then
  cp $PWD/evtest /usr/bin/evtest
  chmod 755 /usr/bin/evtest
fi

cp $PWD/kbd /opt/bin/kbd
chmod 755 /opt/bin/kbd

IS_ZESTY=`lsb_release -a | grep zesty`

if [ "$IS_ZESTY" != "" ]; then
  perl -pi -e 's/gb\.archive/old-releases/g' /etc/apt/sources.list
fi

HAS_CRON_ENTRIES=`crontab -l | grep hardlock_backup`

if [ "$HAS_CRON_ENTRIES" = "" ]; then
  crontab -l | grep -v hardlock_backup | grep -v interval_from_ui > /tmp/$$.mycron
  echo "# backup to hardlock every 5 hours" >> /tmp/$$.mycron
  echo "0 */5 * * * /opt/pos/common/bin/hardlock_backup" >> /tmp/$$.mycron
  echo "" >> /tmp/$$.mycron
  echo "# misc stuff every 5 minutes" >> /tmp/$$.mycron
  echo "*/5 * * * * /opt/pos/common/bin/interval_from_ui.sh" >> /tmp/$$.mycron
  crontab /tmp/$$.mycron
  rm /tmp/$$.mycron
fi

systemctl stop ModemManager.service
systemctl disable ModemManager.service

# this version of usb-mount does a dosfsck on the mounted device
cp $PWD/usb-mount.sh /opt/bin/usb-mount.sh
chmod 755 /opt/bin/usb-mount.sh

# this version of shutdown.sh tries to unmount all the mounted devices
cp $PWD/shutdown.sh /opt/bin/shutdown.sh
chmod 755 /opt/bin/shutdown.sh

# if both include/documents_footer2.tmpl and include/documents_footer.tmpl, empty include/documents_footer.tmpl file
if ((`stat -c%s "/opt/pos/common/print_templates/include/documents_footer2.tmpl"`==`stat -c%s "/opt/pos/common/print_templates/include/documents_footer.tmpl"`)); then
  > /opt/pos/common/print_templates/include/documents_footer.tmpl
fi

if [ ! -e "/opt/pos/.machine_id" ]; then
  rm -f /opt/pos/.machine_id
fi

HAS_SMB_CUSTOMER_DISPLAY=`cat /etc/samba/smb.conf | grep "\[customerdisplay\]"`

if [ "$HAS_SMB_CUSTOMER_DISPLAY" = "" ]; then
  echo "" >> /etc/samba/smb.conf
  echo "[customerdisplay]" >> /etc/samba/smb.conf
  echo "comment = customer display share" >> /etc/samba/smb.conf
  echo "path = /opt/pos/common/addons/graphical_display/default_files/contents/playlist1" >> /etc/samba/smb.conf
  echo "browseable = no" >> /etc/samba/smb.conf
  echo "read only = no" >> /etc/samba/smb.conf
  echo "guest ok = yes" >> /etc/samba/smb.conf
fi

HAS_SMB_CUSTOMER_DISPLAY2=`cat /etc/samba/smb.conf | grep "\[customerdisplay2\]"`

if [ "$HAS_SMB_CUSTOMER_DISPLAY2" = "" ]; then
  echo "" >> /etc/samba/smb.conf
  echo "[customerdisplay2]" >> /etc/samba/smb.conf
  echo "comment = customer display v2 share" >> /etc/samba/smb.conf
  echo "path = /opt/pos/common/addons/graphical_display_v2/media" >> /etc/samba/smb.conf
  echo "browseable = no" >> /etc/samba/smb.conf
  echo "read only = no" >> /etc/samba/smb.conf
  echo "guest ok = yes" >> /etc/samba/smb.conf
fi

# disable visibility of samba shares
perl -pi -e "s/browseable = yes/browseable = no/g" /etc/samba/smb.conf

if [ -e "/etc/udev/rules.d/98-local.rules" ]; then
  HAS_UDEV_RULE=`cat /etc/udev/rules.d/98-local.rules | grep usb_dev_map`

  if [ "$HAS_UDEV_RULE" = "" ]; then
    echo 'ACTION=="add", SUBSYSTEM=="usb", DRIVERS=="usb", RUN+="/opt/pos/common/bin/usb_dev_map"' >> /etc/udev/rules.d/98-local.rules
    udevadm control --reload-rules && udevadm trigger
  fi

  HAS_UDEV_RULE=`cat /etc/udev/rules.d/98-local.rules | grep customer_display_1`

  if [ "$HAS_UDEV_RULE" = "" ]; then
    echo "ACTION==\"add\", ATTRS{idVendor}==\"067b\", ATTRS{idProduct}==\"23a3\", SYMLINK+=\"customer_display_1\", RUN+=\"/bin/sh -c 'modprobe usbserial vendor=0x067b product=0x23a3'\"" >> /etc/udev/rules.d/98-local.rules
    udevadm control --reload-rules && udevadm trigger
  fi
fi

# adds the "cron" param to hardlock backup so it can randomly sleep before backing up
crontab -l | sed 's/hardlock_backup$/hardlock_backup cron/' | crontab
crontab -l | sed 's/hardlock_backup cron/hardlock_backup "" cron/' | crontab

#HAS_NETWORK=`/opt/pos/common/bin/has_network`

#if [ "$HAS_NETWORK" = "YES" ]; then
#  MULTICAST_INSTALLED=`dpkg -l | grep libio-socket-multicast-perl`
#
#  if [ "$MULTICAST_INSTALLED" = "" ]; then
#    apt-get update
#    apt-get install -y libio-socket-multicast-perl
#    #pid_run=`ps xa | grep run_software.sh | grep -v grep | awk '{ print $1 }'`
#    
#    #if [ "$pid_run" != "" ]; then
#    #  kill -9 $pid_run 2> /dev/null
#    #  /opt/pos/common/bin/run_software.sh &
#    #fi
#  fi
#fi

# make sure auto upgrades are disabled
perl -pi -e 's/APT::Periodic::Unattended-Upgrade "1"/APT::Periodic::Unattended-Upgrade "0"/g' /etc/apt/apt.conf.d/10periodic
perl -pi -e 's/APT::Periodic::Unattended-Upgrade "1"/APT::Periodic::Unattended-Upgrade "0"/g' /etc/apt/apt.conf.d/20auto-upgrades

HAS_KERNEL_MASKED=`cat /etc/apt/apt.conf.d/49unattended-upgrades | grep linux-modules-extra`

if [ "$HAS_KERNEL_MASKED" = "" ]; then
  perl -pi -e "s/Package-Blacklist \{/Package-Blacklist \{\n  \"linux-headers\";\n  \"linux-image\";\n  \"linux-modules\";\n  \"linux-modules-extra\";/g" /etc/apt/apt.conf.d/50unattended-upgrades
fi

if [ -e "/etc/acpi/handler.sh" ]; then
  sed -i 's/shutdown.txt/shutdown_acpi.txt/' /etc/acpi/handler.sh
fi

if [ -e "/etc/acpi/powerbtn.sh" ]; then
  sed -i 's/shutdown.txt/shutdown_acpi.txt/' /etc/acpi/powerbtn.sh
fi

HAS_QRCODE_MODULE=`find /usr/local/share/perl | grep QRcode.pm`

if [ "$HAS_QRCODE_MODULE" = "" ]; then
  cp $PWD/GD-Barcode-1.15.tar.gz /tmp
  cd /tmp
  tar zxf GD-Barcode-1.15.tar.gz
  cd GD-Barcode-1.15
  perl Makefile.PL
  make
  make install
  cd $PWD
  rm -rf /tmp/GD-Barcode-1.15*
fi

if [ ! -e "/usr/bin/cutycapt" ]; then
  cp $PWD/cutycapt /usr/bin
  chmod 755 /usr/bin/cutycapt
fi

rm /etc/ImageMagick-6/policy.xml 2> /dev/null

# add support for mercusys cards
if [ ! -e "/etc/udev/rules.d/99-TLWN725Nv2.rules" ]; then
  echo "ACTION==\"add\", ATTRS{idVendor}==\"2c4e\", ATTRS{idProduct}==\"0102\", RUN+=\"/sbin/modprobe 8188eu\" RUN+=\"/bin/sh -c 'echo 2c4e 0102> /sys/bus/usb/drivers/r8188eu/new_id'\"" > /etc/udev/rules.d/99-TLWN725Nv2.rules
fi

HAS_NETWORK=`/opt/pos/common/bin/has_network`

if [ "$HAS_NETWORK" = "YES" ]; then
  #apt-get -y install libdbd-pg-perl libspreadsheet-xlsx-perl
  #cpanm -n Mojo::Pg Mojolicious::Plugin::RenderFile Mojolicious::Plugin::Directory

  # needed for /opt/pos/common/bin/nif_online.pl
  cpanm -n Mozilla::CA HTML::Entities
fi

UDEV_FILE="/etc/udev/rules.d/98-local.rules"

DEV_TO_MAP="customer_display_1"

if [ -e "$UDEV_FILE" ]; then
  HAS_IT=`cat $UDEV_FILE | grep $DEV_TO_MAP`

  if [ "$HAS_IT" = "" ]; then
    echo "ACTION==\"add\", ATTRS{idVendor}==\"067b\", ATTRS{idProduct}==\"23a3\", SYMLINK+=\"$DEV_TO_MAP\"" >> $UDEV_FILE
  fi

else
  echo "ACTION==\"add\", ATTRS{idVendor}==\"067b\", ATTRS{idProduct}==\"23a3\", SYMLINK+=\"$DEV_TO_MAP\"" > $UDEV_FILE
fi

HAS_M19=`cat /opt/pos/etc/config.ini | grep VAT_ISENTION=M19_`
HAS_M26=`cat /opt/pos/etc/config.ini | grep VAT_ISENTION=M26_`
HAS_M21=`cat /opt/pos/etc/config.ini | grep VAT_ISENTION=M21_`
HAS_M30=`cat /opt/pos/etc/config.ini | grep VAT_ISENTION=M30_`
HAS_M31=`cat /opt/pos/etc/config.ini | grep VAT_ISENTION=M31_`
HAS_M32=`cat /opt/pos/etc/config.ini | grep VAT_ISENTION=M32_`
HAS_M33=`cat /opt/pos/etc/config.ini | grep VAT_ISENTION=M33_`
HAS_M40=`cat /opt/pos/etc/config.ini | grep VAT_ISENTION=M40_`
HAS_M41=`cat /opt/pos/etc/config.ini | grep VAT_ISENTION=M41_`
HAS_M42=`cat /opt/pos/etc/config.ini | grep VAT_ISENTION=M42_`
HAS_M43=`cat /opt/pos/etc/config.ini | grep VAT_ISENTION=M43_`

if [ "$HAS_M19" = "" ]; then
  sed -i '1s/^/VAT_ISENTION=M19_Outras Isencoes\n/' /opt/pos/etc/config.ini
fi

if [ "$HAS_M26" = "" ]; then
  sed -i '1s/^/VAT_ISENTION=M26_Isencao de IVA com direito a deducao no cabaz alimentar\n/' /opt/pos/etc/config.ini
fi

if [ "$HAS_M21" = "" ]; then
  sed -i '1s/^/VAT_ISENTION=M21_IVA - nao confere direito a deducao (ou expressao similiar)\n/' /opt/pos/etc/config.ini
fi

if [ "$HAS_M30" = "" ]; then
  sed -i '1s/^/VAT_ISENTION=M30_IVA - autoliquidacao\n/' /opt/pos/etc/config.ini
fi

if [ "$HAS_M31" = "" ]; then
  sed -i '1s/^/VAT_ISENTION=M31_IVA - autoliquidacao\n/' /opt/pos/etc/config.ini
fi

if [ "$HAS_M32" = "" ]; then
  sed -i '1s/^/VAT_ISENTION=M32_IVA - autoliquidacao\n/' /opt/pos/etc/config.ini
fi

if [ "$HAS_M33" = "" ]; then
  sed -i '1s/^/VAT_ISENTION=M33_IVA - autoliquidacao\n/' /opt/pos/etc/config.ini
fi

if [ "$HAS_M40" = "" ]; then
  sed -i '1s/^/VAT_ISENTION=M40_IVA - autoliquidacao\n/' /opt/pos/etc/config.ini
fi

if [ "$HAS_M41" = "" ]; then
  sed -i '1s/^/VAT_ISENTION=M41_IVA - autoliquidacao\n/' /opt/pos/etc/config.ini
fi

if [ "$HAS_M42" = "" ]; then
  sed -i '1s/^/VAT_ISENTION=M42_IVA - autoliquidacao\n/' /opt/pos/etc/config.ini
fi

if [ "$HAS_M43" = "" ]; then
  sed -i '1s/^/VAT_ISENTION=M43_IVA - autoliquidacao\n/' /opt/pos/etc/config.ini
fi

# install all deb in the install folder
# dpkg -E -i $PWD/*.deb

# check for mysql support in gambas3, if not exists, install
if [ -e "/usr/bin/gbx3" ]; then
  HAS_MYSQL_SUPPORT=`dpkg -l | grep gb-db-mysql`

  if [ "$HAS_MYSQL_SUPPORT" = "" ]; then
    dpkg -E -i $PWD/gambas3-gb-db_3.14.3-2ubuntu3.1_amd64.deb $PWD/gambas3-gb-db-mysql_3.14.3-2ubuntu3.1_amd64.deb $PWD/gambas3-runtime_3.14.3-2ubuntu3.1_amd64.deb
  fi
fi

