#!/bin/bash -u

# Copyright   2020   Prisyach Tatyana (STC-innovations Ltd)
# Apache 2.0.

segments=$1
dset=$2
dir_chime6=$3
json=$4

if [ $dset == "dev" ]; then
  awk -F "_|-| " -v dir_chime6=$dir_chime6 -v S02="false" -v S09="false" '
                  BEGIN { printf"%s","{\n    \"alias\": {\n        \"dev\": [\n            \"S02\",\n            \"S09\"\n        ]\n    },\n    \"datasets\": {\n"; }
                  {if (($1 == "S02" && S02 == "true") || ($1 == "S09" && S09 == "true")) { printf"%s",",\n"; }
                  else if ($1 == "S09" && S09 == "false") { printf"%s","\n        },\n"; }
                  if ($1 == "S02") {
                    if (S02 == "false") { printf"%s","        \"S02\": {\n"; S02="true"; }
                    if ($3 == "1") { spk="P05"; }
                    else if ($3 == "2") { spk="P06"; }
                    else if ($3 == "3") { spk="P07"; }
                    else { spk="P08"; }
                    printf"%s","            \"" $1"_"$2"-"$3"-"$4"-"$5 "\": {\n                \"audio_path\": {\n                    \"observation\": {\n                        \"U01\": [\n                            \""dir_chime6"/S02_U01.CH1.wav\",\n                            \""dir_chime6"/S02_U01.CH2.wav\",\n                            \""dir_chime6"/S02_U01.CH3.wav\",\n                            \""dir_chime6"/S02_U01.CH4.wav\"\n                        ],\n                        \"U02\": [\n                            \""dir_chime6"/S02_U02.CH1.wav\",\n                            \""dir_chime6"/S02_U02.CH2.wav\",\n                            \""dir_chime6"/S02_U02.CH3.wav\",\n                            \""dir_chime6"/S02_U02.CH4.wav\"\n                        ],\n                        \"U03\": [\n                            \""dir_chime6"/S02_U03.CH1.wav\",\n                            \""dir_chime6"/S02_U03.CH2.wav\",\n                            \""dir_chime6"/S02_U03.CH3.wav\",\n                            \""dir_chime6"/S02_U03.CH4.wav\"\n                        ],\n                        \"U04\": [\n                            \""dir_chime6"/S02_U04.CH1.wav\",\n                            \""dir_chime6"/S02_U04.CH2.wav\",\n                            \""dir_chime6"/S02_U04.CH3.wav\",\n                            \""dir_chime6"/S02_U04.CH4.wav\"\n                        ],\n                        \"U05\": [\n                            \""dir_chime6"/S02_U05.CH1.wav\",\n                            \""dir_chime6"/S02_U05.CH2.wav\",\n                            \""dir_chime6"/S02_U05.CH3.wav\",\n                            \""dir_chime6"/S02_U05.CH4.wav\"\n                        ],\n                        \"U06\": [\n                            \""dir_chime6"/S02_U06.CH1.wav\",\n                            \""dir_chime6"/S02_U06.CH2.wav\",\n                            \""dir_chime6"/S02_U06.CH3.wav\",\n                            \""dir_chime6"/S02_U06.CH4.wav\"\n                        ]\n                    },\n                    \"worn\": {\n                        \"P05\":\n                            \""dir_chime6"/S02_P05.wav\",\n                        \"P06\":\n                            \""dir_chime6"/S02_P06.wav\",\n                        \"P07\":\n                            \""dir_chime6"/S02_P07.wav\",\n                        \"P08\":\n                            \""dir_chime6"/S02_P08.wav\"\n                    }\n                },\n                \"end\": "int($9*16000)",\n                \"gender\": \""unk"\",\n                \"location\": \""unk"\",\n                \"notes\": [],\n                \"num_samples\": "int($9*16000-$8*16000)",\n                \"reference_array\": \""unk"\",\n                \"session_id\": \"S02\",\n                \"speaker_id\": \""spk"\",\n                \"start\": "int($8*16000)",\n                \"transcription\": \"\"\n            }";
                  } else if ($1 == "S09") {
                    if (S09 == "false") { printf"%s","        \"S09\": {\n"; S09="true"; }
                    if ($3 == "1") { spk="P25"; }
                    else if ($3 == "2") { spk="P26"; }
                    else if ($3 == "3") { spk="P27"; }
                    else { spk="P28"; }
                    printf"%s","            \"" $1"_"$2"-"$3"-"$4"-"$5  "\": {\n                \"audio_path\": {\n                    \"observation\": {\n                        \"U01\": [\n                            \""dir_chime6"/S09_U01.CH1.wav\",\n                            \""dir_chime6"/S09_U01.CH2.wav\",\n                            \""dir_chime6"/S09_U01.CH3.wav\",\n                            \""dir_chime6"/S09_U01.CH4.wav\"\n                        ],\n                        \"U02\": [\n                            \""dir_chime6"/S09_U02.CH1.wav\",\n                            \""dir_chime6"/S09_U02.CH2.wav\",\n                            \""dir_chime6"/S09_U02.CH3.wav\",\n                            \""dir_chime6"/S09_U02.CH4.wav\"\n                        ],\n                        \"U03\": [\n                            \""dir_chime6"/S09_U03.CH1.wav\",\n                            \""dir_chime6"/S09_U03.CH2.wav\",\n                            \""dir_chime6"/S09_U03.CH3.wav\",\n                            \""dir_chime6"/S09_U03.CH4.wav\"\n                        ],\n                        \"U04\": [\n                            \""dir_chime6"/S09_U04.CH1.wav\",\n                            \""dir_chime6"/S09_U04.CH2.wav\",\n                            \""dir_chime6"/S09_U04.CH3.wav\",\n                            \""dir_chime6"/S09_U04.CH4.wav\"\n                        ],\n                        \"U06\": [\n                            \""dir_chime6"/S09_U06.CH1.wav\",\n                            \""dir_chime6"/S09_U06.CH2.wav\",\n                            \""dir_chime6"/S09_U06.CH3.wav\",\n                            \""dir_chime6"/S09_U06.CH4.wav\"\n                        ]\n                    },\n                    \"worn\": {\n                        \"P25\":\n                            \""dir_chime6"/S09_P25.wav\",\n                        \"P26\":\n                            \""dir_chime6"/S09_P26.wav\",\n                        \"P27\":\n                            \""dir_chime6"/S09_P27.wav\",\n                        \"P28\":\n                            \""dir_chime6"/S09_P28.wav\"\n                    }\n                },\n                \"end\": "int($9*16000)",\n                \"gender\": \""unk"\",\n                \"location\": \""unk"\",\n                \"notes\": [],\n                \"num_samples\": "int($9*16000-$8*16000)",\n                \"reference_array\": \""unk"\",\n                \"session_id\": \"S09\",\n                \"speaker_id\": \""spk"\",\n                \"start\": "int($8*16000)",\n                \"transcription\": \"\"\n            }";}
                  }
                  END { printf"%s","\n        }\n    }\n}\n"; }' $segments > $json
elif [ $dset == "eval" ]; then
  awk -F "_|-| " -v dir_chime6=$dir_chime6 -v S01="false" -v S21="false" '
                  BEGIN { printf"%s","{\n    \"alias\": {\n        \"eval\": [\n            \"S01\",\n            \"S21\"\n        ]\n    },\n    \"datasets\": {\n"; }
                  {if (($1 == "S01" && S01 == "true") || ($1 == "S21" && S21 == "true")) { printf"%s",",\n"; }
                  else if ($1 == "S21" && S21 == "false") { printf"%s","\n        },\n"; }
                  if ($1 == "S01"){
                    if (S01 == "false") { printf"%s","        \"S01\": {\n"; S01="true"; }
                    if ($3 == "1") { spk="P01"; }
                    else if ($3 == "2") { spk="P02"; }
                    else if ($3 == "3") { spk="P03"; }
                    else { spk="P04"; }
                    printf"%s","            \"" $1"_"$2"-"$3"-"$4"-"$5  "\": {\n                \"audio_path\": {\n                    \"observation\": {\n                        \"U01\": [\n                            \""dir_chime6"/S01_U01.CH1.wav\",\n                            \""dir_chime6"/S01_U01.CH2.wav\",\n                            \""dir_chime6"/S01_U01.CH3.wav\",\n                            \""dir_chime6"/S01_U01.CH4.wav\"\n                        ],\n                        \"U02\": [\n                            \""dir_chime6"/S01_U02.CH1.wav\",\n                            \""dir_chime6"/S01_U02.CH2.wav\",\n                            \""dir_chime6"/S01_U02.CH3.wav\",\n                            \""dir_chime6"/S01_U02.CH4.wav\"\n                        ],\n                        \"U04\": [\n                            \""dir_chime6"/S01_U04.CH1.wav\",\n                            \""dir_chime6"/S01_U04.CH2.wav\",\n                            \""dir_chime6"/S01_U04.CH3.wav\",\n                            \""dir_chime6"/S01_U04.CH4.wav\"\n                        ],\n                        \"U05\": [\n                            \""dir_chime6"/S01_U05.CH1.wav\",\n                            \""dir_chime6"/S01_U05.CH2.wav\",\n                            \""dir_chime6"/S01_U05.CH3.wav\",\n                            \""dir_chime6"/S01_U05.CH4.wav\"\n                        ],\n                        \"U06\": [\n                            \""dir_chime6"/S01_U06.CH1.wav\",\n                            \""dir_chime6"/S01_U06.CH2.wav\",\n                            \""dir_chime6"/S01_U06.CH3.wav\",\n                            \""dir_chime6"/S01_U06.CH4.wav\"\n                                                ]\n                                        }\n                },\n                \"end\": "int($9*16000)",\n                \"gender\": \""unk"\",\n                \"location\": \""unk"\",\n                \"notes\": [],\n                \"num_samples\": "int($9*16000-$8*16000)",\n                \"reference_array\": \""unk"\",\n                \"session_id\": \"S01\",\n                \"speaker_id\": \""spk"\",\n                \"start\": "int($8*16000)",\n                \"transcription\": \"\"\n            }";
                  } else if ($1 == "S21") {
                    if (S21 == "false") { printf"%s","        \"S21\": {\n"; S21="true"; }
                    if ($3 == "1") { spk="P45"; }
                    else if ($3 == "2") { spk="P46"; }
                    else if ($3 == "3") { spk="P47"; }
                    else { spk="P48"; }
                    printf"%s","            \"" $1"_"$2"-"$3"-"$4"-"$5  "\": {\n                \"audio_path\": {\n                    \"observation\": {\n                        \"U01\": [\n                            \""dir_chime6"/S21_U01.CH1.wav\",\n                            \""dir_chime6"/S21_U01.CH2.wav\",\n                            \""dir_chime6"/S21_U01.CH3.wav\",\n                            \""dir_chime6"/S21_U01.CH4.wav\"\n                        ],\n                        \"U02\": [\n                            \""dir_chime6"/S21_U02.CH1.wav\",\n                            \""dir_chime6"/S21_U02.CH2.wav\",\n                            \""dir_chime6"/S21_U02.CH3.wav\",\n                            \""dir_chime6"/S21_U02.CH4.wav\"\n                        ],\n                        \"U03\": [\n                            \""dir_chime6"/S21_U03.CH1.wav\",\n                            \""dir_chime6"/S21_U03.CH2.wav\",\n                            \""dir_chime6"/S21_U03.CH3.wav\",\n                            \""dir_chime6"/S21_U03.CH4.wav\"\n                         ],\n                        \"U04\": [\n                            \""dir_chime6"/S21_U04.CH1.wav\",\n                            \""dir_chime6"/S21_U04.CH2.wav\",\n                            \""dir_chime6"/S21_U04.CH3.wav\",\n                            \""dir_chime6"/S21_U04.CH4.wav\"\n                        ],\n                        \"U05\": [\n                            \""dir_chime6"/S21_U05.CH1.wav\",\n                            \""dir_chime6"/S21_U05.CH2.wav\",\n                            \""dir_chime6"/S21_U05.CH3.wav\",\n                            \""dir_chime6"/S21_U05.CH4.wav\"\n                        ],\n                        \"U06\": [\n                            \""dir_chime6"/S21_U06.CH1.wav\",\n                            \""dir_chime6"/S21_U06.CH2.wav\",\n                            \""dir_chime6"/S21_U06.CH3.wav\",\n                            \""dir_chime6"/S21_U06.CH4.wav\"\n                                                ]\n                    }\n                },\n                \"end\": "int($9*16000)",\n                \"gender\": \""unk"\",\n                \"location\": \""unk"\",\n                \"notes\": [],\n                \"num_samples\": "int($9*16000-$8*16000)",\n                \"reference_array\": \""unk"\",\n                \"session_id\": \"S21\",\n                \"speaker_id\": \""spk"\",\n                \"start\": "int($8*16000)",\n                \"transcription\": \"\"\n            }";}
                  }
                  END { printf"%s","\n        }\n    }\n}\n"; }' $segments > $json
fi
