#!/usr/bin/env bash
# info_ru: Компоненты сервисов поддержки T-FLEX 17 и 18
########################################################################
export WH_WINDOWS_VER="10"
export WH_WINE_USE="wine_wh_tflex_10-9_amd64"
export BASE_PFX="tflex_pfx_x64_v03"
export WINEARCH="win64"
export WINEPREFIX="tflex"
export PROG_VERSION=""
export WH_XDG_OPEN="log"
export INSTALL_DLL="corefonts d3dcompiler_47 dotnet48 dotnet20 vcrun2019 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"

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_EXE_ADE2007="$AUTOINSTALL_DIR_LIN/Components/AccessDatabaseEngine.exe"
AUTOINSTALL_EXE_TSC2="$AUTOINSTALL_DIR_LIN/Components/Setup_TSC2.msi"
AUTOINSTALL_EXE_VC_X86="$AUTOINSTALL_DIR_LIN/Components/vc_redist.x86.exe"
AUTOINSTALL_EXE_VC_X64="$AUTOINSTALL_DIR_LIN/Components/vc_redist.x64.exe"

prepair_wine

if [[ -d "$WINEPREFIX" ]] \
&& grep -q "t-flex-cad" "$WINEPREFIX/winetricks.log" \
&& systemctl list-units --type service --state running | grep -q aksusbd \
&& systemctl list-units --type service --state running | grep -q 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
        try_download "https://www.tflex.ru/downloads/Prerequisites_T-FLEX_Linux.zip" "${AUTOINSTALL_ZIP_LIN}"
        unpack "$AUTOINSTALL_ZIP_LIN" "$AUTOINSTALL_DIR_LIN"

        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 "Запускаем подготовку префикса."

            unpack "$AUTOINSTALL_EXE_ADE2007" "$AUTOINSTALL_DIR_LIN/ADE2007"
            wine_run_install "$AUTOINSTALL_DIR_LIN/ADE2007/AceRedist.msi" /q

            wine_run_install "$AUTOINSTALL_EXE_VC_X86" /quiet
            wine_run_install "$AUTOINSTALL_EXE_VC_X64" /quiet
            wine_run_install "$AUTOINSTALL_EXE_TSC2" /q

            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/fake_hasp.reg"
            "$WINELOADER" regedit "$AUTOINSTALL_DIR_LIN/Components/ado-32.reg"
            "$WINELOADER" regedit "$AUTOINSTALL_DIR_LIN/Components/ado-64.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"

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