build:arm:
  stage: build
  image: debian:bookworm-slim
  resource_group: builddarm
  artifacts:
   expire_in: 60 minutes
   paths:
    - $CI_PROJECT_DIR   
  variables:
    OS: "raspbian" 
  script:
   - source $CI_PROJECT_DIR/scripts/debian_package/variables
   - echo $VFULL
   - apt update 
   - apt-get -y upgrade 
   - echo "deb-src http://deb.debian.org/debian bullseye main contrib non-free" >> /etc/apt/sources.list 
   - apt-get install --no-upgrade --no-install-recommends --no-install-suggests -y curl unzip automake coreutils debianutils diffutils e2fsprogs findutils grep unzip ncurses-base libevent-pthreads-* libevent-dev ncurses-bin login sysvinit-utils tar libc6-dev libc-dev gcc g++ make dpkg-dev autotools-dev debhelper dh-autoreconf libclamav-dev libpcre3-dev zlib1g-dev pkg-config libssl-dev ca-certificates lsb-release inotify-tools curl 
   - cd $CI_PROJECT_DIR && make clean  
   - ./autogen.sh
   - ./configure --host=arm-linux-gnueabihf --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 -j 4
   - find $CI_PROJECT_DIR -name ".git" -exec rm -r "{}" +
   - chown -Rf 1161 $CI_BUILDS_DIR
  tags:
     - arm

package:debarm:
  stage: create-package
  image: debian:bookworm-slim
  dependencies:
    - build:arm
  artifacts:
   expire_in: 60 minutes
   paths:
    - $CI_PROJECT_DIR   
  variables:
    PACKPATH: "$CI_PROJECT_DIR"
    PACKDST: "$CI_PROJECT_DIR/scripts/debian_package/e2debian-arm64_package"
    OS: "debian-arm64"
  script:
    - source $CI_PROJECT_DIR/scripts/debian_package/variables
    - apt update
    - apt install -y curl git lsb-release binutils curl ca-certificates util-linux binutils libpcre3 libssl3
    - echo $VFULL
    - VERSION=`lsb_release -cs`
    - 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:\ $VFULL/g" ${PACKDST}/control/control
    - cat ${PACKDST}/control/control
    - 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
    - cd ${PACKDST} && cd .. && ./rebuild.sh e2"$OS"_package
    - find $CI_PROJECT_DIR -name ".git" -exec rm -r "{}" +
  tags:
     - arm

test:debianarm:
  stage: test-publish
  image: debian:bookworm-slim
  dependencies:
    - package:debarm
  artifacts:
   expire_in: 60 minutes
   name: builddebian
   paths:
    - $CI_PROJECT_DIR 
  variables:
    PACKPATH: "$CI_PROJECT_DIR"
    PACKDST: "$CI_PROJECT_DIR/scripts/debian_package/e2debian-arm64_package"
    CERT_DIR: "/etc/e2guardian/private/"
    OS: "debian-arm64"
  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} && cd .. && 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 "{}" +
  tags:
     - arm
