#!/bin/bash
set -e

read -p "Elasticsearch username: [elastic]: " ES_USERNAME
ES_USERNAME=${ES_USERNAME:-elastic}

read -s -p "Elasticsearch password: " ES_PASSWORD
ES_PASSWORD=${ES_PASSWORD:-changeme}

echo

read -p "Elasticsearch URL [http://localhost:9200]: " ES_URL
ES_URL=${ES_URL:-http://localhost:9200}

read -p "Password of sirenserver user [password]:" SIRENSERVER_PASSWORD
SIRENSERVER_PASSWORD=${SIRENSERVER_PASSWORD:-"password"}

read -p "Password of federate user [password]:" FEDERATE_PASSWORD
FEDERATE_PASSWORD=${FEDERATE_PASSWORD:-"password"}

read -p "Password of sirenuser user [password]:" SIRENUSER_PASSWORD
SIRENUSER_PASSWORD=${SIRENUSER_PASSWORD:-"password"}

read -p "Password of sirenadmin user [password]:" SIRENADMIN_PASSWORD
SIRENADMIN_PASSWORD=${SIRENADMIN_PASSWORD:-"password"}

read -p "curl flags: " CURL_FLAGS

echo "Creating sirenserver user"

curl $CURL_FLAGS -XPUT -u $ES_USERNAME:$ES_PASSWORD $ES_URL/_xpack/security/user/sirenserver -H "Content-Type: application/json" -d '{
  "password" : "'$SIRENSERVER_PASSWORD'",
  "full_name": "Siren Server",
  "roles": [ "investigate_system" ]
}'

echo
echo

echo "Creating sirenadmin user"

curl $CURL_FLAGS -XPUT -u $ES_USERNAME:$ES_PASSWORD $ES_URL/_xpack/security/user/sirenadmin -H "Content-Type: application/json" -d '{
  "password" : "'$SIRENADMIN_PASSWORD'",
  "full_name": "Siren Admin",
  "roles": [ "investigate_admin", "investigate_user" ]
}'

echo
echo

echo "Creating sirenuser user"

curl $CURL_FLAGS -XPUT -u $ES_USERNAME:$ES_PASSWORD $ES_URL/_xpack/security/user/sirenuser -H "Content-Type: application/json" -d '{
  "password" : "'$SIRENUSER_PASSWORD'",
  "full_name": "Siren User",
  "roles": [ "investigate_user" ]
}'

echo
echo

echo "Creating federate system user"

curl $CURL_FLAGS -XPUT -u $ES_USERNAME:$ES_PASSWORD $ES_URL/_xpack/security/user/federate -H "Content-Type: application/json" -d '{
  "password" : "'$FEDERATE_PASSWORD'",
  "full_name": "Federate System",
  "roles": [ "federate_system" ]
}'

echo
echo
