Bootstrap: docker
From: ubuntu:18.04



%environment
	export LC_ALL=C
	export LC_NUMERIC=C
	export PYTHONPATH=$PYTHONPATH:/sen2chain:/sen2chain/sen2chain/:/usr/local/share/python3.6/dist-package
	export PATH=$PATH:/sen2chain:/sen2chain/sen2chain/:/usr/local/share/python3.6/dist-package:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/Sen2Cor-02.05.05-Linux64/L2A_Bashrc
    export HOME=/sen2chain_home
    export SEN2COR_HOME=/sen2cor_home




%post
	apt-get update
	apt install --assume-yes curl
    apt install --assume-yes python3 && apt install --assume-yes python3-pip
    apt install --assume-yes git
    apt install --assume-yes wget
    add-apt-repository ppa:ubuntugis/ppa &&  apt-get update
    apt-get --assume-yes install gdal-bin libgdal-dev

    export CPLUS_INCLUDE_PATH=/usr/include/gdal
    export C_INCLUDE_PATH=/usr/include/gdal
    pip3 install -Iv setuptools==41.0.0
    pip3 install GDAL==$(gdal-config --version | awk -F'[.]' '{print $1"."$2}')

    mkdir /sen2chain_home
    chmod -R a+rw /sen2chain_home

    cd /
    wget http://step.esa.int/thirdparties/sen2cor/2.5.5/Sen2Cor-02.05.05-Linux64.run
    chmod +x Sen2Cor-02.05.05-Linux64.run
    ./Sen2Cor-02.05.05-Linux64.run
    chmod -R a+rwx /Sen2Cor-02.05.05-Linux64
    mkdir -p /sen2cor_home/sen2cor/2.5/cfg
    cp /Sen2Cor-02.05.05-Linux64/lib/python2.7/site-packages/sen2cor/cfg/L2A_GIPP.xml /sen2cor_home/sen2cor/2.5/cfg/L2A_GIPP.xml
    chmod -R a+rwx /sen2cor_home

    git clone https://framagit.org/espace-dev/sen2chain.git
    cd sen2chain
    pip3 install Cython
    pip3 install PROJ>=7.2.0
    pip3 install pyproj==2.6.1 #1.9.6
    pip3 install -r requirements.txt
    chmod -R a+wrx /sen2chain

%runscript
    #import sen2chain to create config.cfg
    (echo "try:" ; echo "   import sen2chain" ; echo "except ValueError:" ; echo "    print('Python ok')") | python3

    #replace api's id
    echo "pepsid pepspwd" > '/sen2chain_home/sen2chain_data/config/peps.txt'
    sed -i 's/peps_.*/peps_config_path\ =\ \/sen2chain_home\/sen2chain_data\/config\/peps.txt/' /sen2chain_home/sen2chain_data/config/config.cfg

    sed -i 's/scihub_i.*/scihub_id\ =\ scihub_uid/' /sen2chain_home/sen2chain_data/config/config.cfg
    sed -i 's/scihub_p.*/scihub_pwd\ =\ scihub_pwd/' /sen2chain_home/sen2chain_data/config/config.cfg

    #Change SEN2COR_HOME variable and L2A_bashrc
    sed -i 's/sen2cor_.*/sen2cor_bashrc_path\ =\ \/Sen2Cor-02\.05\.05-Linux64\/L2A_Bashrc/' /sen2chain_home/sen2chain_data/config/config.cfg
    sed -i 's/SEN2COR_H.*/SEN2COR_HOME\=\/sen2cor_home\/sen2cor\/2.5/' /Sen2Cor-02.05.05-Linux64/L2A_Bashrc

    bash


