#! /bin/sh
set -e

echo "**********************************************************************"
echo "*                                                                    *"
echo "* Thank you for buying subscriptions from Millennium Software Design *"
echo "*                https://millenniumsoftwaredesign.com                *"
echo "*                                                                    *"
echo "**********************************************************************"

OS=""
INSTALL_DIR=/opt/msd
INSTALL_DEPS=n

ask_for_os () {
    while true; do
        printf "Choose (U)buntu 18.04 or (R)ed Hat Enterprise Linux 9: "
        read -r choice
        case $choice in
            [Uu]*)
                OS="ubuntu_18.04"
                break
                ;;
            [Rr]*)
                OS="rhel_9"
                break
                ;;
            *)
                echo "Please answer U or R."
                ;;
        esac
    done
}

ask_for_install_dir () {
    while true; do
        echo "Please provide the base installation directory"
        echo "(or hit enter for default: $INSTALL_DIR)"
        printf "> "
        read -r dir
        if [ "$dir" = "" ]; then
            echo "using default: $INSTALL_DIR"
            break
        elif [ ! -w "$dir" ]; then
            echo "You entered $dir but that directory does not exist"
        elif [ ! -x "$dir" ]; then
            echo "You entered $dir but you don't have permission to create file there"
        else
            INSTALL_DIR=$dir
            break
        fi
    done
}

ask_to_install_deps () {
    echo "Install dependencies when finished (requires sudo)? y/n"
    printf "> "
    read -r choice
    case $choice in
        [Yy]*)
            INSTALL_DEPS=y
            ;;
        [Nn]*)
            INSTALL_DEPS=n
            ;;
        *)
            echo "please answer y or n"
            exit 1
            ;;
    esac
}

deps_failed () {
    echo "WARNING: couldn't install needed packages, please install these dependencies:"
    printf "  "
    echo "$@"
}

install_ubuntu_deps () {
    sudo apt-get -y install "$@" || deps_failed "$@"
}

install_rhel_deps () {
    sudo dnf -y install epel-release || deps_failed epel-release
    sudo crb enable || \
        echo "WARNING: couldn't run crb enable, please do so yourself"
    sudo dnf -y install "$@" || deps_failed "$@"
}

product () {
    # assumes tar file is formatted product-version-variant.tar with
    # no - in product, version, or variant strings
    echo "$1" | cut -d '-' -f1 | sed 's:\./::g'
}

version () {
    # assumes tar file is formatted product-version-variant.tar
    # no - in product, version, or variant strings
    echo "$1" | sed 's:.*-\(v.*\)-.*:\1:'
}

os_variant_choices () {
    echo "Choices are: rhel_9"
}

if [ "$#" = "0" ]; then
    ask_for_os
    ask_for_install_dir
    ask_to_install_deps
else
    OPTS=$(getopt --options="h,i,d:" --longoptions="help,install-dir,install-deps:" --name "$0" -- "$@") || exit 3
    eval set -- "$OPTS"
    while true; do
        case "$1" in
            -h|--help)
                echo "Installs Millennium Software Design's"
                echo "Design and Verification Suite (AKA, msd_dvs)"
                echo ""
                echo "If no arguments are given this script will ask for the "
                echo "information it needs.  To avoid that, pass an os variant "
                echo "to install for. $(os_variant_choices)"
                echo ""
                echo "An installation directory can be specified with -d or "
                echo "--install-dir, or the default of /opt/msd will be used."
                exit 1
                ;;
            -i|--install-dir)
                INSTALL_DIR="$2"
                shift 2
                ;;
            -d|--install-deps)
                INSTALL_DEPS=y
                shift
                ;;
            --)
                shift
                break
                ;;
            *)
                echo "ERROR: bug in install script if you see this message"
                exit 2
                ;;
        esac
    done
    case "$1" in
        rhel_9|rhel-9|rhel9|redhat9)
            OS="rhel_9"
            ;;
        *)
            echo "ERROR: Please specify a valid OS variant to install"
            os_variant_choices
            exit 1
    esac
fi

install_all_deps () {
    case $OS in
        [Uu]*)
            install_ubuntu_deps "$@"
            ;;
        [Rr]*)
            install_rhel_deps "$@"
            ;;
        *) echo "Error, invalid OS.";;
    esac
}

SRC_DIR=$PWD
PRODUCT=$(product ./*.tar)
VERSION=$(version ./*.tar)
mkdir -p tools
cd tools
tar xvf ../"$PRODUCT"-"$VERSION"-"$OS".tar
cd "$INSTALL_DIR"
for tarfile in "$SRC_DIR"/tools/*.tar; do
    tar xvf "$tarfile"
done
rm -r "$SRC_DIR"/tools/

# put dependencies in the $@ array:
case $OS in
    [Uu]*)
        # gtkwave
        set -- libtcl8.6 libtk8.6 libcanberra-gtk-module
        # ghdl
        set -- "$@" libgnat-7
        # cocotb
        set -- "$@" make gcc g++ swig python3-dev
        ;;
    [Rr]*)
        set -- \
            perl \
            ccache \
            gcc-toolset-14 \
            python3.11 \
            mold \
            numactl \
            gperftools-devel
        ;;
esac

if [ "$INSTALL_DEPS" = "y" ]; then
    install_all_deps "$@"
else
    echo "install these packages: " "$@"
fi

# update env file and copy to to installation directory
env_file="$SRC_DIR"/"$PRODUCT"-"$OS".env
sed -i "s:<install-dir>:$INSTALL_DIR:" "$env_file"
sed -i "s:<product-version>:$PRODUCT-$VERSION:" "$env_file"
cp "$env_file" "$INSTALL_DIR"/"$PRODUCT"-"$VERSION"/
printf "\nInstallation successful!\n"
