build:debian:
  stage: build-debian
  resource_group: builddeb
  artifacts:
   expire_in: 60 minutes
   paths:
    - $CI_PROJECT_DIR 
  image: $CONTAINER_CLIENT_IMAGE
  variables:
    OS: "debian"
  script:
    - apt update
    - apt-get -y upgrade
    - apt install --no-install-recommends --no-install-suggests -y curl unzip base-files automake base-passwd
      bash coreutils dash debianutils diffutils dpkg e2fsprogs findutils grep gzip hostname ncurses-base
      libevent-pthreads-* libevent-dev ncurses-bin perl-base sed login sysvinit-utils tar bsdutils
      mount util-linux libc6-dev libc-dev gcc g++ make dpkg-dev autotools-dev debhelper dh-autoreconf
      libclamav-dev libpcre3-dev zlib1g-dev pkg-config libssl-dev git ca-certificates lsb-release
    - cd $CI_PROJECT_DIR && ./autogen.sh && ./configure  '--prefix=/usr' '--enable-clamd=yes' '--with-proxyuser=e2guardian' '--with-proxygroup=e2guardian' '--sysconfdir=/etc' '--localstatedir=/var' '--enable-icap=yes' '--enable-commandline=yes' '--enable-email=yes' '--enable-ntlm=yes' '--mandir=${prefix}/share/man' '--infodir=${prefix}/share/info' '--enable-pcre=yes' '--enable-sslmitm=yes' 'CPPFLAGS=-mno-sse2 -g -O2'
    - make -j 4
    - find $CI_PROJECT_DIR -name ".git" -exec rm -r "{}" +
    - chown -Rf 1161 $CI_BUILDS_DIR

package:debian:
  stage: create-package-debian
  image: $CONTAINER_CLIENT_IMAGE
  artifacts:
   expire_in: 60 minutes
   paths:
    - $CI_PROJECT_DIR 
  variables:
    PACKPATH: "$CI_PROJECT_DIR"
    PACKDST: "$CI_PROJECT_DIR/scripts/debian_package/e2debian_package"
    OS: "debian"
  script:
    - apt update && apt install --no-install-recommends --no-install-suggests -y curl git ca-certificates util-linux binutils libpcre3 
    - git clone https://github.com/fredbcode/scripts
    - cp ${PACKPATH}/src/e2guardian ${PACKDST}/data/usr/sbin/e2guardian
    - chmod +x ${PACKDST}/data/usr/sbin/e2guardian      
    - cp -Rf ${PACKPATH}/configs ${PACKDST}/data/etc/e2guardian
    - mkdir -p ${PACKPATH}/share/e2guardian/languages
    - cp -Rf ${PACKPATH}/data/languages ${PACKDST}/data/usr/share/e2guardian/
    - cp ${PACKPATH}/data/*.gif ${PACKDST}/data/usr/share/e2guardian/
    - cp ${PACKPATH}/data/*swf ${PACKDST}/data/usr/share/e2guardian/
    - cp ${PACKPATH}/data/scripts/e2guardian.service ${PACKDST}/data/lib/systemd/system
    - find ${PACKDST}/ -type f -name "Makefil*" -delete
    - find ${PACKDST}/ -type f -name "*.in" -delete
    - SIZE=`stat -c %s ${PACKDST}/data` && echo $SIZE && sed -i "s/Installed-Size:.*$/Installed-Size:\ $SIZE/g" ${PACKDST}/control/control
    - VFULL=`${PACKDST}/data/usr/sbin/e2guardian -v | sed -n 1p | cut -d ' ' -f 2` && sed -i "s/Version:.*$/Version:\ $VFULL/g" ${PACKDST}/control/control 
    - cat ${PACKDST}/control/control
    - echo "export VFULL=$VFULL" >> $CI_PROJECT_DIR/scripts/debian_package/variables
    - cd ${PACKDST} && cd .. && ./rebuild.sh e2"$OS"_package
    - find $CI_PROJECT_DIR -name ".git" -exec rm -r "{}" +

test:debian:
  stage: test-publish-debian
  image: $CONTAINER_CLIENT_IMAGE
  artifacts:
   name: builddebian
   expire_in: 60 minutes
   paths:
    - $CI_PROJECT_DIR 
  variables:
    PACKPATH: "$CI_PROJECT_DIR"
    PACKDST: "$CI_PROJECT_DIR/scripts/debian_package"
    CERT_DIR: "/etc/e2guardian/private/"
    OS: "debian"
  script:
    - source $CI_PROJECT_DIR/scripts/debian_package/variables
    - echo $VFULL
    - apt update
    - apt install -y openssl curl git lsb-release libevent-pthreads* libtommath* libpcre3 libssl3 
    - VERSION=`lsb_release -cs`
    - cd ${PACKDST} && dpkg -i e2"$OS"_package.deb 
    - sed -i "s/^#sslmitm.*$/sslmitm\ =\ on/" /etc/e2guardian/e2guardianf1.conf && sed -i "s/^#enablessl.*$/enablessl\ =\ on/" /etc/e2guardian/e2guardian.conf && sed -i "s/^RANDFILE\s*=\s*\\\$ENV/#RANDFILE\ =\ \$ENV/" /etc/ssl/openssl.cnf
    - mkdir -p ${CERT_DIR}generatedcerts
    - openssl genrsa 4096 > "${CERT_DIR}ca.key" && openssl req -subj "/C=US" -new -x509 -days 3650 -key "${CERT_DIR}ca.key" -out "${CERT_DIR}ca.pem" && openssl genrsa 4096 > "${CERT_DIR}cert.key"
    - chown --recursive e2guardian:e2guardian ${CERT_DIR}
    - echo "qwant.com" >> /etc/e2guardian/lists/example.group/bannedsitelist
    - e2guardian -N &
    - sleep 5 && export https_proxy=http://localhost:8080 
    - curl -k -o /tmp/test https://www.google.com 
    - curl -k https://www.qwant.com | grep -i e2guardian 
    - unset https_proxy
    - mkdir -p "${CI_COMMIT_BRANCH}" 
    - mv e2"$OS"_package.deb ${CI_COMMIT_BRANCH}/e2"$OS"_"$VERSION"_V"$VFULL"_$(date +%Y%m%d).deb
    - md5sum ${CI_COMMIT_BRANCH}/e2"$OS"_"$VERSION"_V"$VFULL"_$(date +%Y%m%d).deb >  ${CI_COMMIT_BRANCH}/e2"$OS"_"$VERSION"_V"$VFULL"_$(date +%Y%m%d)_md5.txt
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
    - mkdir -p ~/.ssh
    - eval $(ssh-agent -s)
    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
    - ssh-add <(echo "$SSH_NOSTROMO_KEY")
    - scp -P 822 -r ${CI_COMMIT_BRANCH} e2git@e2guardian.numsys.eu:/datas/e2/html
    - find $CI_PROJECT_DIR -name ".git" -exec rm -r "{}" +
