#! /bin/bash

case $RESALLOC_NAME in
    *s390x*tokyo*)
        vpc_id=r022-8438169e-d881-4bda-b603-d31fdf0f8b3a
        security_group_id=r022-bf49b90e-c00f-4c68-8707-2936b47b286b
        ssh_key_id=r022-3918e368-8e00-4e23-9119-5e3ce1eb33bd
        instance_type=cz2-2x4
        image_uuid="{{ copr_builder_images.ibm_cloud.s390x.tokyo }}"
        subnets_ids="jp-tok-1:02e7-84755ffa-97bb-4067-b618-24c788c901cb jp-tok-2:02f7-98674f68-aae1-4ea1-a889-5a0b7a07f4b8 jp-tok-3:02g7-6d92562d-b868-411f-a962-99271d609ba6"
        zone=jp-tok
        ;;
    *s390x*us_east*)
        vpc_id=r014-e90f14b0-a9c0-4c0b-bc81-207904b2d19e
        security_group_id=r014-941706bd-062c-4d4c-9512-9a31b8f257d7
        ssh_key_id=r014-fc1c1b90-5a7f-4cc6-a6d4-b273bd19be99
        case $RESALLOC_NAME in
        *_hp_*)
            instance_type=bz2-16x64
            ;;
        *)
            instance_type=mz2-2x16
            ;;
        esac
        image_uuid="{{ copr_builder_images.ibm_cloud.s390x.us_east }}"
        subnets_ids="us-east-1:0757-df8209fd-e4e4-4659-bcca-b60c9eddfb35 us-east-2:0767-4cb7e11d-14db-48a8-9d1d-c59679112cd5 us-east-3:0777-bc697f15-4f13-4843-b338-d8950e3d23c0"
        zone=us-east
        ;;
    *)
        echo "Can't decide what location to assign from: $RESALLOC_NAME"
        exit 1
        ;;
esac

params=()

if [ "$1" == "create" ]; then
    params+=(
        --playbook "{{ provision_directory }}/libvirt-provision.yml"
        --image-uuid "$image_uuid"
        --vpc-id "$vpc_id"
        --security-group-id "$security_group_id"
        --ssh-key-id "$ssh_key_id"
        --instance-type "$instance_type"
        --subnets-ids $subnets_ids
        --
    )
fi

exec resalloc-ibm-cloud-vm \
    --token-file "{{ ibmcloud_token_file }}" \
    --zone "$zone" \
    --log-level debug \
    "$1" \
    "${params[@]}" \
    "${@:2}"
