1UNAME=`uname -s`
2if [ "$UNAME" == "Linux" ]
3then
4    DETECTED_OS="linux"
5    ON_WINDOWS=false
6    ON_OSX=false
7    ON_LINUX=true
8    CS_COMPILER=xbuild
9    LAUNCHER="mono"
10    PYTHON_RUNNER="python3"
11elif [ "$UNAME" == "Darwin" ]
12then
13    DETECTED_OS="osx"
14    ON_WINDOWS=false
15    ON_OSX=true
16    ON_LINUX=false
17    CS_COMPILER=xbuild
18    LAUNCHER="mono"
19    PYTHON_RUNNER="python3"
20else
21    DETECTED_OS="windows"
22    ON_WINDOWS=true
23    ON_OSX=false
24    ON_LINUX=false
25    CS_COMPILER=MSBuild.exe
26    LAUNCHER=""
27    PYTHON_RUNNER="py -3"
28fi
29
30ARCH_UNAME=`uname -m`
31# macOS and Linux uses different names for 64-bit Arm
32if [ "$ARCH_UNAME" == "aarch64" ] || [ "$ARCH_UNAME" == "arm64" ]
33then
34    DETECTED_ARCH="arm"
35else
36    DETECTED_ARCH="x64"
37fi
38function get_path {
39    if $ON_WINDOWS
40    then
41        var="`cygpath -aw "$1"`"
42        echo -n ${var//\\/\\\\}
43    else
44        echo -n "$1"
45    fi
46}
47
48function clone_if_necessary() {
49    NAME="$1"
50    REMOTE="$2"
51    BRANCH="$3"
52    TARGET_DIR="$4"
53    GUARD="$5"
54
55    if [ -e "$GUARD" ]
56    then
57        top_ref=`git ls-remote -h $REMOTE $BRANCH 2>/dev/null | cut -f1`
58        if [ "$top_ref" == "" ]
59        then
60            echo "Could not access remote $REMOTE. Continuing without verification of the state of $NAME ."
61            exit
62        fi
63        pushd "$TARGET_DIR" >/dev/null
64        cur_ref=`git rev-parse HEAD`
65        master_ref=`git rev-parse $BRANCH`
66        if [ $master_ref != $cur_ref ]
67        then
68            echo "The $NAME repository is not on the local $BRANCH branch. This situation should be handled manually."
69            exit
70        fi
71        popd >/dev/null
72        if [ $top_ref == $cur_ref ]
73        then
74            echo "Required $NAME repository already downloaded. To repeat the process remove $GUARD file."
75            exit
76        fi
77        echo "Required $NAME repository is available in a new version. It will be redownloaded..."
78    fi
79
80    rm -rf "$TARGET_DIR"
81    git clone --depth=1 --single-branch --branch=$BRANCH $REMOTE $(get_path "$TARGET_DIR")
82}
83
84function add_path_property {
85    sanitized_path=$(sed 's:\\:/:g' <<< `get_path "$3"`)
86    sed -i.bak "s#</PropertyGroup>#  <$2>$sanitized_path</$2>"'\
87</PropertyGroup>#' "$1"
88}
89
90function get_min_mono_version {
91    cat $ROOT_PATH/tools/mono_version
92}
93
94function verify_mono_version {
95    MINIMUM_MONO=`get_min_mono_version`
96    if ! [ -x "$(command -v $LAUNCHER)" ]
97    then
98        echo "$LAUNCHER not found. Renode requires Mono $MINIMUM_MONO or newer. Please refer to documentation for installation instructions. Exiting!"
99        exit 1
100    fi
101
102    # Check mono version
103    MINIMUM_MONO_MAJOR=`echo $MINIMUM_MONO | cut -d'.' -f1`
104    MINIMUM_MONO_MINOR=`echo $MINIMUM_MONO | cut -d'.' -f2`
105
106    INSTALLED_MONO=`$LAUNCHER --version | head -n1 | cut -d' ' -f5`
107    INSTALLED_MONO_MAJOR=`echo $INSTALLED_MONO | cut -d'.' -f1`
108    INSTALLED_MONO_MINOR=`echo $INSTALLED_MONO | cut -d'.' -f2`
109
110    if [ $INSTALLED_MONO_MAJOR -lt $MINIMUM_MONO_MAJOR ] || [ $INSTALLED_MONO_MAJOR -eq $MINIMUM_MONO_MAJOR -a $INSTALLED_MONO_MINOR -lt $MINIMUM_MONO_MINOR ]
111    then
112        echo "Wrong Mono version detected: $INSTALLED_MONO. Renode requires Mono $MINIMUM_MONO or newer. Please refer to documentation for installation instructions. Exiting!"
113        exit 1
114    fi
115}
116
117# NOTE: if we switch to using msbuild on all platforms, we can get rid of this function and only use the '-' prefix
118function build_args_helper() {
119    local retStr=""
120    for p in "$@" ; do
121        if [ "$CS_COMPILER" = 'xbuild' ] ; then
122            retStr="${retStr} /$p"
123        else
124            retStr="${retStr} -$p"
125        fi
126    done
127    echo ${retStr}
128}
129