stages:
- build:debian
- create-debian-package
- TestDebian
- build:ubuntu
- create-ubuntu-package
- TestUbuntu
# Works in progress ...
#- build:raspbian
#- create-raspbian-package

Package Docker Image Debian:
  stage: build:debian
  artifacts:
   expire_in: 10 minutes
   name: build
   paths:
    - /builds/fredbcode/e2guardian   
  image: amd64/debian:stable
  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 dpatch
      libclamav-dev libpcre3-dev zlib1g-dev pkg-config libssl-dev libssl1.1 git ca-certificates lsb-release
    - cd /builds/fredbcode/e2guardian && ./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 
    - rm -rf .git

Create Debian Package:
  stage: create-debian-package
  artifacts:
   expire_in: 10 minutes
   name: build 
   paths:
    - /builds/fredbcode/e2guardian/scripts/debian_package
  image: amd64/debian:stable
  variables:
    PACKPATH: "/builds/fredbcode/e2guardian"
    PACKDST: "/builds/fredbcode/e2guardian/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
    - git clone https://github.com/fredbcode/scripts
    - cp ${PACKPATH}/src/e2guardian ${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 
    - cd ${PACKDST} && cd .. && ./rebuild.sh e2"$OS"_package
    - rm -rf .git

Test and publish Debian:
  stage: TestDebian
  image: amd64/debian:stable
  variables:
    PACKPATH: "/builds/fredbcode/e2guardian"
    PACKDST: "/builds/fredbcode/e2guardian/scripts/debian_package"
    CERT_DIR: "/home/e2/e2install/"
    OS: "debian"
  script:
    - apt update
    - apt install -y openssl curl git lsb-release libevent-pthreads* libtommath* 
    - 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/^#cacertificatepath/cacertificatepath/;s/#caprivatekeypath/caprivatekeypath/;s/#certprivatekeypath/certprivatekeypath/;s/#generatedcertpath/generatedcertpath/" /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 /home/e2
    - e2guardian -N & 
    - sleep 5 && export https_proxy=http://localhost:8080 && curl -k https://google.com
    - unset https_proxy
    - git clone -b packages --single-branch https://gitlab.com/fredbcode/e2guardian.git gitpackages
    - mkdir -p gitpackages/${CI_COMMIT_REF_NAME} 
    - mv e2"$OS"_package.deb gitpackages/${CI_COMMIT_REF_NAME}/e2"$OS"_"$VERSION"_V"$VFULL"_$(date +%Y%m%d).deb
    - cd gitpackages/${CI_COMMIT_REF_NAME}
    - git add e2"$OS"_"$VERSION"_V"$VFULL"_$(date +%Y%m%d).deb
    - git config --global user.name "${GITLAB_USER_NAME}"
    - git config --global user.email "${GITLAB_USER_EMAIL}"
    - git commit -m "Commit "$OS" ${CI_COMMIT_REF_NAME} Package"
    - git push https://${YOUR_USERNAME}:${PERSONAL_ACCESS_TOKEN}@gitlab.com/fredbcode/e2guardian.git HEAD:packages
   
Package Docker Image Ubuntu:
  stage: build:ubuntu
  artifacts:
   expire_in: 15 minutes
   name: build
   paths:
    - /builds/fredbcode/e2guardian   
  image: amd64/ubuntu:18.04
  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 dpatch
      libclamav-dev libpcre3-dev zlib1g-dev pkg-config libssl-dev libssl1.1 git ca-certificates lsb-release
    - cd /builds/fredbcode/e2guardian && ./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 
    - rm -rf .git

Create Ubuntu Package:
  stage: create-ubuntu-package
  image: amd64/ubuntu:18.04
  artifacts:
   expire_in: 15 minutes
   name: build
   paths:
    - /builds/fredbcode/e2guardian/scripts/debian_package
  variables:
    PACKPATH: "/builds/fredbcode/e2guardian"
    PACKDST: "/builds/fredbcode/e2guardian/scripts/debian_package/e2ubuntu_package"
    OS: "ubuntu"
  script:
    - apt update && apt install --no-install-recommends --no-install-suggests -y curl git ca-certificates util-linux binutils
    - cp ${PACKPATH}/src/e2guardian ${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 
    - cd ${PACKDST} && cd .. && ./rebuild.sh e2"$OS"_package
    - rm -rf .git

Test and publish Ubuntu:
  stage: TestUbuntu 
  image: amd64/ubuntu:18.04
  variables:
    PACKPATH: "/builds/fredbcode/e2guardian"
    PACKDST: "/builds/fredbcode/e2guardian/scripts/debian_package"
    CERT_DIR: "/home/e2/e2install/"
    OS: "ubuntu"
  script:
    - apt update
    - apt install -y curl openssl git lsb-release libevent-pthreads* libtommath* 
    - VERSION=`lsb_release -cs`
    - VFULL=`${PACKDST}/e2debian_package/data/usr/sbin/e2guardian -v | sed -n 1p | cut -d ' ' -f 2`
    - 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/^#cacertificatepath/cacertificatepath/;s/#caprivatekeypath/caprivatekeypath/;s/#certprivatekeypath/certprivatekeypath/;s/#generatedcertpath/generatedcertpath/" /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 /home/e2
    - e2guardian -N & 
    - sleep 5 && export https_proxy=http://localhost:8080 && curl -k https://google.com
    - unset https_proxy
    - git clone -b packages --single-branch https://gitlab.com/fredbcode/e2guardian.git gitpackages
    - mkdir -p gitpackages/${CI_COMMIT_REF_NAME} 
    - mv e2"$OS"_package.deb gitpackages/${CI_COMMIT_REF_NAME}/e2"$OS"_"$VERSION"_V"$VFULL"_$(date +%Y%m%d).deb
    - cd gitpackages/${CI_COMMIT_REF_NAME}
    - git add e2"$OS"_"$VERSION"_V"$VFULL"_$(date +%Y%m%d).deb
    - git config --global user.name "${GITLAB_USER_NAME}"
    - git config --global user.email "${GITLAB_USER_EMAIL}"
    - git commit -m "Commit "$OS" ${CI_COMMIT_REF_NAME} Package"
    - git push https://${YOUR_USERNAME}:${PERSONAL_ACCESS_TOKEN}@gitlab.com/fredbcode/e2guardian.git HEAD:packages
   
     
# TODO: CONFIGURE configure --host=arm works only on real machine ?
# 
#Package Docker Image Raspbian:
#  stage: build:raspbian
#  artifacts:
#   expire_in: 1 hour
#   name: build-raspbian
#   paths:
#    - /builds   
#  image: amd64/debian:stable
#  variables:
#    OS: "rasbian" 
#  script:
#    - echo "deb-src http://deb.debian.org/debian stretch main" >> /etc/apt/sources.list 
#    - echo "deb-src http://deb.debian.org/debian-security/ stretch/updates main" >> /etc/apt/sources.list 
#    - echo "deb-src http://deb.debian.org/debian stretch-updates main" >> /etc/apt/sources.list 
#    - 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 dpatch
#      libclamav-dev libpcre3-dev zlib1g-dev pkg-config libssl-dev libssl1.1 git ca-certificates lsb-release gcc-arm-linux-gnueabi g++-arm-linux-gnueabi
#    - dpkg --add-architecture armhf
#    - apt-get update
#    - apt-get install -y build-essential crossbuild-essential-armhf
#    - apt-get build-dep -y -aarmhf e2guardian
#    - git clone https://github.com/raspberrypi/tools.git --depth 1 /tmp/tools
#    - export ARCH=arm
#    - export CROSS_COMPILE=arm-bcm2708hardfp-linux-gnueabi-
#    - export CC=/tmp/tools/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/bin/arm-bcm2708hardfp-linux-gnueabi-gcc
#    - export NM=/tmp/tools/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/bin/arm-bcm2708hardfp-linux-gnueabi-nm
#    - export LD=/tmp/tools/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/bin/arm-bcm2708hardfp-linux-gnueabi-ld
#    - export CXX=/tmp/tools/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/bin/arm-bcm2708hardfp-linux-gnueabi-g++
#    - export RANLIB=/tmp/tools/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/bin/arm-bcm2708hardfp-linux-gnueabi-ranlib
#    - export AR=/tmp/tools/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/bin/arm-bcm2708hardfp-linux-gnueabi-ar
#    - cd /builds/fredbcode/e2guardian 
#    - ./autogen.sh 
#    - export
#    - ./configure --host=arm --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 --enable-pcre=yes --enable-sslmitm=yes
#    - make ARCH=arm CROSS_COMPIL=arm-linux-gnueabi-
#    - rm -rf .git
#
##Create Raspbian Package:
#  stage: create-raspbian-package
#  image: amd64/debian:stable
#  variables:
#    PACKPATH: "/builds/fredbcode/e2guardian"
#    PACKDST: "/builds/fredbcode/e2guardian/scripts/debian_package/e2raspbian_package"
#    OS: "raspbian"
#  script:
#    - cd /builds/fredbcode/e2guardian
#    - apt update && apt install --no-install-recommends --no-install-suggests -y curl git ca-certificates lsb-release util-linux binutils
#    - git clone https://github.com/fredbcode/scripts
#    - cp ${PACKPATH}/src/e2guardian ${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/
#    - 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
#    - sed -i "s/Version:.*$/Version:\ ${CI_COMMIT_REF_NAME}/g" ${PACKDST}/control/control 
#    - cd ${PACKDST} && cd .. && ./rebuild.sh e2"$OS"_package
#    - git clone -b packages --single-branch https://gitlab.com/fredbcode/e2guardian.git gitpackages
#    - mkdir -p gitpackages/${CI_COMMIT_REF_NAME}
#    - mv e2"$OS"_package.deb gitpackages/${CI_COMMIT_REF_NAME}/e2"$OS"_${CI_COMMIT_REF_NAME}_$(date +%Y%m%d).deb
#    - cd gitpackages/${CI_COMMIT_REF_NAME}
#    - git add *.deb
#    - git config --global user.name "${GITLAB_USER_NAME}"
#    - git config --global user.email "${GITLAB_USER_EMAIL}"
#    - git commit -m "Commit "$OS" ${CI_COMMIT_REF_NAME} Package" 
#    - git push https://${YOUR_USERNAME}:${PERSONAL_ACCESS_TOKEN}@gitlab.com/fredbcode/e2guardian.git HEAD:packages
#    - rm -rf *
