apiVersion: v1
kind: ConfigMap
metadata:
  name: viewer-setup-script-cm
data:
  officer_setup.sh: |
    #!/bin/bash

    counter=1
    threshold=30
    while [[ ! "200 302" =~ "$(curl -s -o /dev/null -w ''%{http_code}'' localhost:5000/reports/setup)" ]];
    do echo waiting for setup page... attempt $counter/$threshold;
    let counter=counter+1; sleep 20;
    if [ $counter -ge $threshold ]; then break; fi
    done

    export DATABASE_URL=postgresql://$REDASH_DATABASE_USER:$REDASH_DATABASE_PASSWORD@$REDASH_DATABASE_HOSTNAME/$REDASH_DATABASE_DB

    default_admin_group_id='1'
    default_group_id='2'

    #Create Admin user:
    users=$(python ./manage.py users list)
    if [[ $users != *"$SYSTEM_ADMIN_EMAIL"* ]]; then
    python ./manage.py users create_root $SYSTEM_ADMIN_EMAIL system-admin-user-do-not-disable --password $SYSTEM_ADMIN_PASS

    # Cut permissions for default group
    # python ./manage.py groups change_permissions $default_group_id --permissions view_query,view_source,execute_query,list_dashboards,list_alerts

    # Return permissions to admin group
    # python ./manage.py groups change_permissions $default_admin_group_id --permissions admin,super_admin,create_dashboard,create_query,edit_dashboard,edit_query,view_query,view_source,execute_query,list_users,schedule_query,list_dashboards,list_alerts,list_data_sources
    fi
