#!/usr/bin/sh
#----------------------------------------------------------------------------
# /tmp/install.sh - lldb21 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=lldb21

# ---------------------------------------------------------------------------
# activate
# ---------------------------------------------------------------------------
ver=21
veri=2118
   
/usr/sbin/update-alternatives \
   --install /usr/bin/lldb lldb /usr/bin/lldb-${ver} ${veri} \
   --slave /usr/bin/lldb-argdumper lldb-argdumper /usr/bin/lldb-argdumper-${ver} \
   --slave /usr/bin/lldb-dap lldb-dap /usr/bin/lldb-dap-${ver} \
   --slave /usr/bin/lldb-instr lldb-instr /usr/bin/lldb-instr-${ver} \
   --slave /usr/bin/lldb-server lldb-server /usr/bin/lldb-server-${ver}

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

# check what is the best version
best=$(/usr/sbin/update-alternatives --query lldb | /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 "lldb manual /usr/bin/lldb-${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_lld=$(eisman check lld${ver})
if [ "${inst_lld}" = "installed" ]
then
    # 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
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
# ---------------------------------------------------------------------------
