#!/usr/bin/env bash
#
# Generated by: https://github.com/swagger-api/swagger-codegen.git
#

frameworkVersion={{targetFrameworkNuget}}
netfx=${frameworkVersion#net}

echo "[INFO] Target framework: ${frameworkVersion}"

echo "[INFO] Download nuget and packages"
wget -nc https://nuget.org/nuget.exe;
mozroots --import --sync
mono nuget.exe install src/{{packageName}}/packages.config -o packages;

echo "[INFO] Copy DLLs to the 'bin' folder"
mkdir -p bin;
cp packages/Newtonsoft.Json.8.0.3/lib/{{targetFrameworkNuget}}/Newtonsoft.Json.dll bin/Newtonsoft.Json.dll;
cp packages/RestSharp.105.1.0/lib/{{targetFrameworkNuget}}/RestSharp.dll bin/RestSharp.dll;
{{#generatePropertyChanged}}
cp packages/Fody.1.29.2/Fody.dll bin/Fody.dll
cp packages/PropertyChanged.Fody.1.51.3/PropertyChanged.Fody.dll bin/PropertyChanged.Fody.dll
cp packages/PropertyChanged.Fody.1.51.3/Lib/dotnet/PropertyChanged.dll bin/PropertyChanged.dll
{{/generatePropertyChanged}}

echo "[INFO] Run 'mcs' to build bin/{{{packageName}}}.dll"
mcs -sdk:${netfx} -r:bin/Newtonsoft.Json.dll,\
{{#generatePropertyChanged}}
bin/Fody.dll,\
bin/PropertyChanged.Fody.dll,\
bin/PropertyChanged.dll,\
{{/generatePropertyChanged}}
bin/RestSharp.dll,\
System.ComponentModel.DataAnnotations.dll,\
System.Runtime.Serialization.dll \
-target:library \
-out:bin/{{packageName}}.dll \
-recurse:'src/{{packageName}}/*.cs' \
-doc:bin/{{packageName}}.xml \
-platform:anycpu

if [ $? -ne 0 ]
then
  echo "[ERROR] Compilation failed with exit code $?"
  exit 1
else
  echo "[INFO] bin/{{{packageName}}}.dll was created successfully"
fi
