#!/bin/zsh

# Given internal user ids, create Abacus profiles to grant them access.

local user_ids=$1
local ticket_num=$2
local dev_username=$3
local dev_id=$4

# leave args blank and get prompted for them
if [[ -z "$user_ids" || -z "$ticket_num" || -z "$dev_username" || -z "$dev_id" ]]; then
  [[ -z "$user_ids" ]] && read "?Enter user_ids (comma separated): " user_ids
  [[ -z "$ticket_num" ]] && read "?Enter ticket_num: " ticket_num
  [[ -z "$dev_username" ]] && read "?Enter dev_username: " dev_username
  [[ -z "$dev_id" ]] && read "?Enter dev_id: " dev_id
fi

local first_line="--liquibase formatted cypher"
local template="./scripts/templates/create-abacus-profile.cypher.template"
local filename="./neo4j/neo4j-orchard/build/changelog/dml/$ticket_num-create-abacus-profile.cypher"
local quoted_ids=${"$(printf "'%s', " ${(s:,:)user_ids})"%, }

sed \
  -e "s/%user_ids/$quoted_ids/g" \
  -e "s/%dev_username/$dev_username/g" \
  -e "s/%dev_id/$dev_id/g" \
  -e "s/%ticket_num/$ticket_num/g" \
  "$template" >> "$filename"

echo -e "\nCypher file created: $filename\n"
echo "Also need to unmask the emails if they're non-Sony. Search \"Creating ABACUS Profiles\" in Notion for instructions."
