#!/bin/bash

hasComposer=`command -v composer`
if [[ $hasComposer == '' ]]
    then

    sudo apt-get update -yqq
    sudo apt-get install vim -yqq
    sudo apt-get install unzip -yqq

    sudo apt-get install php -yqq
    sudo apt-get install -yqq php-mbstring mcrypt php-pgsql php-curl php-json php-intl php-gd php-xml php-zip php-bz2 php7.4-opcache php-mysql php-xdebug composer php-bcmath php-pear php-dev
    sudo pecl channel-update pecl.php.net


    sudo php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"
    sudo php composer-setup.php
    sudo php -r "unlink('composer-setup.php');"
    sudo mv composer.phar /usr/bin/composer
    # install Oracle Support
    sudo wget https://download.oracle.com/otn_software/linux/instantclient/199000/instantclient-basic-linux.x64-19.9.0.0.0dbru.zip -q
    sudo wget https://download.oracle.com/otn_software/linux/instantclient/199000/instantclient-sdk-linux.x64-19.9.0.0.0dbru.zip -q
    sudo wget https://download.oracle.com/otn_software/linux/instantclient/199000/instantclient-sqlplus-linux.x64-19.9.0.0.0dbru.zip -q
    sudo wget https://download.oracle.com/otn_software/linux/instantclient/199000/instantclient-jdbc-linux.x64-19.9.0.0.0dbru.zip -q
    sudo wget https://download.oracle.com/otn_software/linux/instantclient/199000/instantclient-odbc-linux.x64-19.9.0.0.0dbru.zip -q
    sudo unzip -o instantclient-basic-linux.x64-19.9.0.0.0dbru.zip -d /etc
    sudo unzip -o instantclient-sdk-linux.x64-19.9.0.0.0dbru.zip -d /etc
    sudo unzip -o instantclient-sqlplus-linux.x64-19.9.0.0.0dbru.zip -d /etc
    sudo unzip -o instantclient-jdbc-linux.x64-19.9.0.0.0dbru.zip -d /etc
    sudo unzip -o instantclient-odbc-linux.x64-19.9.0.0.0dbru.zip -d /etc
    sudo rm instantclient-basic-linux.x64-19.9.0.0.0dbru.zip instantclient-sdk-linux.x64-19.9.0.0.0dbru.zip instantclient-sqlplus-linux.x64-19.9.0.0.0dbru.zip instantclient-jdbc-linux.x64-19.9.0.0.0dbru.zip instantclient-odbc-linux.x64-19.9.0.0.0dbru.zip -f

    #- sudo echo /etc/instantclient_19_9/ > /etc/ld.so.conf.d/oracle-instantclient
    #- sudo ldconfig
    settlePath=`sudo cat /etc/profile|grep ORACLE_HOME`
    if [[ $settlePath == '' ]]; then sudo sed '$a\export ORACLE_HOME=/etc/instantclient_19_9\n export PATH=\$PATH:/etc/instantclient_19_9\n export LD_LIBRARY_PATH=\$LD_LIBRARY_PATH:/etc/instantclient_19_9\' /etc/profile -i;  fi
    source /etc/profile;
    sudo apt-get  install libaio*
    result=`sudo pecl list|grep oci8`
    if [[ $result == '' ]] ; then printf "instantclient,/etc/instantclient_19_9\n" |sudo pecl install oci8|grep; fi
    sudo cp /usr/lib/php/20190902/oci8.so /usr/lib/php/20190902/oci8_12c;
    phpIni=`php -i|grep "Loaded Configuration File"` && arr=(${phpIni//=>/}) && iniPath=${arr[3]}
    sudo sed -i 's/^;\(extension=oci8_12c\)/\1/' $iniPath

    composer update
    sudo chown gitlab-runner:docker vendor -R
fi
