#!/bin/bash

### change image id and naming suffix then run this bitch
image=$1
naming_suffix="_test_with_fixed_regex"

function drive_count_check {
    instance_type=$1
    echo $instance_type | grep -qE d
    if [[ $? == 0 ]]; then
        drive_count=23
    else
        drive_count=24
    fi

    echo $drive_count
}

function launch {
    instance_types="m5 c5 r5 c5d m5d"
    #instance_types=m5
    for i in $instance_types; do
        instance_type=$i".xlarge"
        name=$i$naming_suffix
        drive_count=$(drive_count_check $i)
        response=$(exec aws emr create-cluster --auto-scaling-role EMR_AutoScaling_DefaultRole --applications Name=Hadoop --bootstrap-actions '[{"Path":"s3://ty-emr/XRR/bootstrap/install_sift.sh","Name":"Custom action"}]' --ebs-root-volume-size 10 --ec2-attributes '{"KeyName":"tylebean","AdditionalSlaveSecurityGroups":["sg-3ed30a59"],"InstanceProfile":"EMR_EC2_DefaultRole","SubnetId":"subnet-3db49e4a","EmrManagedSlaveSecurityGroup":"sg-7f523e05","EmrManagedMasterSecurityGroup":"sg-7e523e04","AdditionalMasterSecurityGroups":["sg-3ed30a59","sg-5279fd35"]}' --service-role EMR_DefaultRole --release-label emr-5.17.0 --name "$name" --instance-groups '[{"InstanceCount":1,"EbsConfiguration":{"EbsBlockDeviceConfigs":[{"VolumeSpecification":{"SizeInGB":10,"VolumeType":"gp2"},"VolumesPerInstance":23},{"VolumeSpecification":{"SizeInGB":32,"VolumeType":"gp2"},"VolumesPerInstance":1}],"EbsOptimized":true},"InstanceGroupType":"MASTER","InstanceType":"'$instance_type'","Name":"Master - 1"}]' --scale-down-behavior TERMINATE_AT_TASK_COMPLETION --region us-east-1 --additional-info "{ami64: \"$image\", amiHvm64: \"$image\", hadoopVersion: \"2.7.2\", hadoopConfigurationVersion: \"5.0\"}")
        id=$(echo $response | jq -r '.ClusterId')
        url="https://aegir-us-east-1.amazon.com/Search?searchText=$id&searchType=Guess&domain=prod&realm=us-east-1"
        echo "$name :: $id :: $url"
    done
}

function launch_test {
    instance_types="m5 c5 r5 c5d m5d"
    for i in $instance_types; do
        instance_type=$i".xlarge"
        name=$i$naming_suffix
        drive_count=$(drive_count_check $i)
        response=$(echo "{\"ClusterId\": \"j-$instance_type\"}")
        id=$(echo $response | jq -r '.ClusterId')
        url="https://aegir-us-east-1.amazon.com/Search?searchText=$id&searchType=Guess&domain=prod&realm=us-east-1"
        echo "$name :: $id :: $url"
        echo $url
    done
}

function url_test {
    id="j-2BGP1R9D3NTD6"
    url="https://aegir-us-east-1.amazon.com/Search?searchText=$id&searchType=Guess&domain=prod&realm=us-east-1"
    open $url
}

launch
