apiVersion: v1
data:
  clientgitinit.bash: |
    git config --global user.email yuuvis-init@yuuvis.org
    git config --global user.name yuuvis-init

    echo 'git clone'
    if git clone {{.Values.yuuvis.git.protocol}}://{{.Values.yuuvis.git.user}}:{{.Values.yuuvis.git.password}}@{{.Values.yuuvis.git.url}} --branch {{.Values.yuuvis.git.branch}}; 
    then
        echo 'git branch {{.Values.yuuvis.git.branch}} exists'
        cd {{.Values.yuuvis.git.name}}

        if [ ! -f apps/client/system/schema.xml ];
        then   
            mkdir -p apps/client/system/
        fi

        if [ ! -f apps/clientsystem/system/schema.xml ];
        then  
            mkdir -p apps/clientsystem/system/
        fi

        cp /yuuvis/data/schema.xml apps/client/system/schema.xml
        cp /yuuvis/data/clientsystem_schema.xml apps/clientsystem/system/schema.xml

        if [ ! -f apps/api-web/text/i18n_de.json ]; 
        then
            mkdir -p apps/api-web/text/
            cp /yuuvis/data/i18n_de.json apps/api-web/text/i18n_de.json
            cp /yuuvis/data/i18n_en.json apps/api-web/text/i18n_en.json
        else
            jq -s '.[0] * .[1]' apps/api-web/text/i18n_de.json /yuuvis/data/i18n_de.json > merge_i18n_de.json
            jq -s '.[0] * .[1]' apps/api-web/text/i18n_en.json /yuuvis/data/i18n_en.json > merge_i18n_en.json
            rm apps/api-web/text/i18n_de.json
            rm apps/api-web/text/i18n_en.json
            mv merge_i18n_de.json apps/api-web/text/i18n_de.json
            mv merge_i18n_en.json apps/api-web/text/i18n_en.json
        fi

        jq -s '.[0] * .[1]' system/systemHookConfiguration.json /yuuvis/data/systemHookConfiguration.json > merge_hook.json
        rm system/systemHookConfiguration.json
        mv merge_hook.json system/systemHookConfiguration.json
        
        git add .
        git commit -m 'init api-web config'

        echo 'git pull'
        git pull -s recursive -X ours

        echo 'git push'    
        if git push origin {{.Values.yuuvis.git.branch}}
        then
            echo 'push sucessful'
        else
            echo 'push unsucessful'
            exit 1
        fi
    else
        echo 'git and/or git branch not found'
        exit 1 
    fi
kind: ConfigMap
metadata:
  name: clientinit
