.linux-prepare:

  cache:
    key: $CI_JOB_NAME
    paths:
      - ccache/

  extends: .prepare

  before_script:
    ##
    ## If a TUNNEL_USER_KEY is defined then start ssh-agent and add the key
    ##
    - if ! [ -z ${TUNNEL_USER_KEY+x} ]; then eval $(ssh-agent -s); fi
    - if ! [ -z ${TUNNEL_USER_KEY+x} ]; then echo "$TUNNEL_USER_KEY" | tr -d '\r' | ssh-add - > /dev/null; fi
    - if ! [ -z ${TUNNEL_USER_KEY+x} ]; then mkdir -p ~/.ssh && chmod 700 ~/.ssh; fi
    - if ! [ -z ${TUNNEL_USER_KEY+x} ]; then echo -e "Host gitlab.linphone.org\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config; fi

    ##
    ## Then configure ccache
    ##
    - mkdir -p ccache
    - echo "max_size = $CCACHE_SIZE" > ccache/ccache.conf
    - echo $CCACHE_SIZE
    - echo ${PWD}/ccache
    - export CCACHE_BASEDIR=${PWD}
    - export CCACHE_DIR=${PWD}/ccache
    - ccache -s
    - find $CI_PROJECT_DIR -name '.git' -exec bash -c 'git config --global --add safe.directory ${0%/.git}' {} \;
    - git describe --debug || true
    - cd external/linphone-sdk
    - git describe --debug || true
    - cd ../..

  after_script:
    - if ! [ -z ${TUNNEL_USER_KEY+x} ]; then rm -rf ~/.ssh || true; fi
    - export CCACHE_DIR=${PWD}/ccache
    - ccache -s

