#!/usr/bin/env bash
# info_ru: Компоненты сервисов поддержки T-FLEX 17
########################################################################
export WH_WINDOWS_VER="10"
export WH_WINE_USE="wine_wh_tflex_10-9_amd64"
export BASE_PFX="tflex17_pfx_x64_v02"
export WINEARCH="win64"
export WINEPREFIX="tflex17"
export PROG_VERSION=""
export WH_XDG_OPEN="log"
export INSTALL_DLL="corefonts d3dcompiler_47 dotnet48 vcrun2022 ucrtbase2019 msxml6 fontsmooth=rgb baekmuk droid eufonts ipamona liberation lucida opensymbol sourcehansans tahoma takao uff unifont vlgothic wenquanyi wenquanyizenhei"
export WH_USE_MESA_GL_OVERRIDE="1"

# Компоненты поддержки T-FLEX PLM (Linux)
GRDCONTROL_VER="4.3.0-0"
AUTOINSTALL_ZIP_LIN="$WH_TMP_DIR/Prerequisites_T-FLEX_Linux.zip"
AUTOINSTALL_DIR_LIN="$WH_TMP_DIR/Prerequisites_T-FLEX_Linux"
AUTOINSTALL_REG_LIN="$AUTOINSTALL_DIR_LIN/Components/fake_hasp.reg"

# Компоненты поддержки T-FLEX PLM (Windows)
AUTOINSTALL_ZIP_WIN="$WH_TMP_DIR/T-FLEX 17 Prerequisites.zip"
AUTOINSTALL_DIR_WIN="$WH_TMP_DIR/Компоненты поддержки T-FLEX 17"
AUTOINSTALL_EXE_WIN1="$AUTOINSTALL_DIR_WIN/Access Database Engine 2007/AccessDatabaseEngine.exe"
AUTOINSTALL_EXE_WIN_MSI="$AUTOINSTALL_DIR_WIN/Access Database Engine 2007"
AUTOINSTALL_EXE_WIN2="$AUTOINSTALL_DIR_WIN/TSC2/Setup_TSC2.msi"

prepair_wine

if [[ -d "$WINEPREFIX" ]] \
&& grep -q "t-flex-cad" "$WINEPREFIX/winetricks.log" \
&& systemctl list-units --type service --state running | grep aksusbd \
&& systemctl list-units --type service --state running | grep hasplmd \
&& rpm -q grdcontrol | grep -q "$GRDCONTROL_VER"
then
    print_info "Префикс $PREFIX_NAME готов к установке ПО."
    print_info "Сервисы: aksusbd и hasplmd уже запущены. grdcontrol-$GRDCONTROL_VER установлен."
else
    print_warning "Службы aksusbd и hasplmd не запущены, или grdcontrol-$GRDCONTROL_VER не установлен."

    if [[ "$BASE_PFX" == "none" ]] \
    || ! systemctl list-units --type service --state running | grep aksusbd \
    || ! systemctl list-units --type service --state running | grep hasplmd \
    || ! rpm -q grdcontrol | grep -q "$GRDCONTROL_VER"
    then
        # Компоненты поддержки T-FLEX PLM (Linux)
        if try_download "https://www.tflex.ru/downloads/Prerequisites_T-FLEX_Linux.zip" "${AUTOINSTALL_ZIP_LIN}" ; then
            unpack "$AUTOINSTALL_ZIP_LIN" "$AUTOINSTALL_DIR_LIN"
            "$WINELOADER" regedit "$AUTOINSTALL_REG_LIN"
        fi

        if rpm -q grdcontrol | grep -q "$GRDCONTROL_VER"
        then print_info "grdcontrol-$GRDCONTROL_VER уже установлен в системе."
        else su_run rpm -iv "$AUTOINSTALL_DIR_LIN/Guardant_Linux/grdcontrol.x86_64.rpm"
        fi

        if [[ "$BASE_PFX" == "none" ]] ; then
            print_info "Запускаем подготовку префикса."
            # Компоненты поддержки T-FLEX PLM (Windows)
            if try_download "https://www.tflex.ru/downloads/T-FLEX%2017%20Prerequisites.zip" "${AUTOINSTALL_ZIP_WIN}" ; then
                unpack "$AUTOINSTALL_ZIP_WIN" "$WH_TMP_DIR"
                unpack "$AUTOINSTALL_EXE_WIN1" "$AUTOINSTALL_EXE_WIN_MSI"

                wine_run_install "$AUTOINSTALL_EXE_WIN_MSI/AceRedist.msi" /q
                wine_run_install "$AUTOINSTALL_EXE_WIN2" /q
            fi
            rm -fR "$DRIVE_C/Program Files/Common Files/System"
            rm -fR "$DRIVE_C/Program Files (x86)/Common Files/System"

            cp -r "$AUTOINSTALL_DIR_LIN/Components/Program Files/Common Files/System" \
                   "$DRIVE_C/Program Files/Common Files"
            cp -r "$AUTOINSTALL_DIR_LIN/Components/Program Files (x86)/Common Files/System" \
                   "$DRIVE_C/Program Files (x86)/Common Files"

            mv -f "$AUTOINSTALL_DIR_LIN/Components/Windows/System32/"*.dll \
                  "$DRIVE_C/windows/system32"

            "$WINELOADER" regedit "$AUTOINSTALL_DIR_LIN/Components/ado-32.reg"
            "$WINELOADER"64 regedit "$AUTOINSTALL_DIR_LIN/Components/ado-64.reg"
            "$WINELOADER" regedit "$AUTOINSTALL_DIR_LIN/Components/tflex.reg"
        fi

        if systemctl list-units --type service --state running | grep aksusbd \
        && systemctl list-units --type service --state running | grep hasplmd
        then
            print_info "Службы aksusbd и hasplmd уже запущены. Пропускаем..."
        else
            echo "#!/usr/bin/env bash
            cd \"$AUTOINSTALL_DIR_LIN/HASP_Linux/aksusbd/\"
            bash ./dinst
            mv -f \"$AUTOINSTALL_DIR_LIN/HASP_Linux/\"*.so /var/hasplm/
            systemctl restart hasplmd.service" > "$WH_TMP_DIR/tflex_aksusbd.sh"
            chmod +x "$WH_TMP_DIR/tflex_aksusbd.sh"
            unset TFLEX_ERROR
            print_info "Будут установлены и запущены службы aksusbd и hasplmd."
            su_run "$WH_TMP_DIR/tflex_aksusbd.sh" || TFLEX_ERROR=1
            try_remove_file "$WH_TMP_DIR/tflex_aksusbd.sh"
        fi
    fi

    try_remove_dir "$AUTOINSTALL_DIR_LIN"
    try_remove_dir "$AUTOINSTALL_DIR_WIN"

    if [[ $TFLEX_ERROR == "1" ]] ; then
        fatal "Произошла ошибка во время подготовки префикса, или установки компонентов ПО."
    else
        echo "t-flex-cad-prepair" >> "$WINEPREFIX/winetricks.log"
        try_remove_file "$AUTOINSTALL_ZIP_LIN"
        try_remove_file "$AUTOINSTALL_ZIP_WIN"
        print_info "Подготовка префикса и установка компонентов завершены."
    fi
fi
