#!/usr/bin/sh
#----------------------------------------------------------------------------
# /tmp/install.sh - lld21 installation
#
# Creation   :  2025-12-03 holbru
# Last update:  $Id$
#
# Copyright (c) 2025-@@YEAR@@ Holger Bruenjes, holgerbruenjes(at)gmx(dot)net
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#----------------------------------------------------------------------------

# set package name
package_name=lld21

# ---------------------------------------------------------------------------
# activate
# ---------------------------------------------------------------------------
ver=21
veri=2118
   
/usr/sbin/update-alternatives \
   --install /usr/bin/lld lld /usr/bin/lld-${ver} ${veri} \
   --slave /usr/bin/ld.lld ld.lld /usr/bin/ld.lld-${ver} \
   --slave /usr/bin/ld64.lld ld64.lld /usr/bin/ld64.lld-${ver} \
   --slave /usr/bin/lld-link lld-link /usr/bin/lld-link-${ver} \
   --slave /usr/bin/wasm-ld wasm-ld /usr/bin/wasm-ld-${ver}

/usr/sbin/update-alternatives --install /usr/bin/ld ld /usr/bin/ld.lld 1

# activate the best version
/usr/sbin/update-alternatives --auto lld

# check what is the best version
best=$(/usr/sbin/update-alternatives --query lld | /usr/bin/gawk '/Best/ {print $2}')

# if the best version not this version, use this version manual
if ! echo "${best}" | /usr/bin/grep -q "${ver}"
then
    echo "lld manual /usr/bin/lld-${ver}" | /usr/sbin/update-alternatives --set-selections  >/dev/null 2>&1
fi

# activate the best version
/usr/sbin/update-alternatives --auto ld

# check what is the best version
best=$(/usr/sbin/update-alternatives --query ld | /usr/bin/gawk '/Best/ {print $2}')

# if the best version not this version, use this version manual
if ! echo "${best}" | /usr/bin/grep -q "${ver}"
then
    echo "ld manual /usr/bin/ld-${ver}" | /usr/sbin/update-alternatives --set-selections  >/dev/null 2>&1
fi


inst_clang=$(eisman check clang${ver})
if [ "${inst_clang}" = "installed" ]
then
    # activate the best version
    /usr/sbin/update-alternatives --auto clang

    # check what is the best version
    best=$(/usr/sbin/update-alternatives --query clang | /usr/bin/gawk '/Best/ {print $2}')

    # if the best version not this version, use this version manual
    if ! echo "${best}" | /usr/bin/grep -q "${ver}"
    then
        echo "clang manual /usr/bin/clang-${ver}" | /usr/sbin/update-alternatives --set-selections  >/dev/null 2>&1
    fi
fi

inst_llvm=$(eisman check llvm${ver})
if [ "${inst_llvm}" = "installed" ]
then
    # activate the best version
    /usr/sbin/update-alternatives --auto llvm-ar

    # check what is the best version
    best=$(/usr/sbin/update-alternatives --query llvm-ar | /usr/bin/gawk '/Best/ {print $2}')

    # if the best version not this version, use this version manual
    if ! echo "${best}" | /usr/bin/grep -q "${ver}"
    then
        echo "llvm-ar manual /usr/bin/llvm-ar-${ver}" | /usr/sbin/update-alternatives --set-selections  >/dev/null 2>&1
    fi
fi

exit 0
# ---------------------------------------------------------------------------
# end
# ---------------------------------------------------------------------------
