#!/usr/bin/env bash
# -------------------------------------------------------------------------- #
# Copyright 2002-2025, OpenNebula Project, OpenNebula Systems                #
#                                                                            #
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
# not use this file except in compliance with the License. You may obtain    #
# a copy of the License at                                                   #
#                                                                            #
# http://www.apache.org/licenses/LICENSE-2.0                                 #
#                                                                            #
# Unless required by applicable law or agreed to in writing, software        #
# distributed under the License is distributed on an "AS IS" BASIS,          #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
# See the License for the specific language governing permissions and        #
# limitations under the License.                                             #
# -------------------------------------------------------------------------- #

# Prepare target directory
readonly SELF=$(realpath "$0" | xargs dirname) && cd "$SELF/"

[ -d "${SELF}/lib" ] && rm -rf "${SELF}/lib"

install -d "$SELF/lib/"

# Download Python packages
declare -A PACKAGES=(
    ["pulp"]="2.9.0"
    ["xsdata"]="24.12"
    ["typing-extensions"]="4.12.2"
)

for package in "${!PACKAGES[@]}"; do
    version="${PACKAGES[$package]}"
    echo "Installing $package==$version..."
    pip install --no-deps --target=lib "$package==$version"
done

# Remove precompiled bytecode
find "$SELF/lib/" -type d -name "__pycache__" -exec rm -rf {} +

# Remove uneeded folders
CBC_BASE="${SELF}/lib/pulp/solverdir/cbc"

folders=("${CBC_BASE}/osx" \
         "${CBC_BASE}/win" \
         "${CBC_BASE}/linux/32")

for folder in "${folders[@]}"; do
    [ -d "$folder" ] && rm -rf "$folder"
done
