#!/usr/bin/env bash
# info_ru: Просмотр, печать и аннотирование документов T-FLEX CAD
########################################################################
export PROG_NAME="T-FLEX Viewer 17"
export WH_WINDOWS_VER="10"
export WH_WINE_USE="wine_x_tkg_10-0_amd64"
export BASE_PFX="tflex17_pfx_x64_v01"
export WINEARCH="win64"
export WINEPREFIX="tflex17"
export PROG_VERSION=""
export PROG_ICON="tflexcad"
export INSTALL_DLL="corefonts d3dcompiler_47 dotnet48 vcrun2019 ucrtbase2019 msxml6 fontsmooth=rgb baekmuk droid eufonts ipamona liberation lucida opensymbol sourcehansans tahoma takao uff unifont vlgothic wenquanyi wenquanyizenhei"

prepair_wine
if [[ "$BASE_PFX" == "none" ]] \
|| ! systemctl list-units --type service --state running | grep aksusbd \
|| ! systemctl list-units --type service --state running | grep hasplmd
then
    # Компоненты поддержки T-FLEX PLM 17 (Linux)
    AUTOINSTALL_ZIP_LIN="${WH_TMP_DIR}/Prerequisites_T-FLEX_17_Linux.zip"
    AUTOINSTALL_DIR_LIN="${WH_TMP_DIR}/Prerequisites_T-FLEX_17_Linux"
    AUTOINSTALL_EXE_LIN1="${WH_TMP_DIR}/Prerequisites_T-FLEX_17_Linux/Components/fake_hasp.reg"
    if try_download "https://www.tflex.ru/downloads/Prerequisites_T-FLEX_17_Linux.zip" "${AUTOINSTALL_ZIP_LIN}" ; then
        7z x -y "${AUTOINSTALL_ZIP_LIN}" -o"${AUTOINSTALL_DIR_LIN}"
        try_remove_file "$AUTOINSTALL_ZIP_LIN"
        "$WINELOADER" regedit "${AUTOINSTALL_EXE_LIN1}"
    fi

    if [[ "$BASE_PFX" == "none" ]] ; then
        # Компоненты поддержки T-FLEX PLM 17 (Windows)
        AUTOINSTALL_ZIP_WIN="${WH_TMP_DIR}/T-FLEX 17 Prerequisites.zip"
        AUTOINSTALL_DIRWIN="${WH_TMP_DIR}"
        AUTOINSTALL_EXE_WIN1="${WH_TMP_DIR}/Компоненты поддержки T-FLEX 17/Access Database Engine 2007/AccessDatabaseEngine.exe"
        AUTOINSTALL_EXE_WIN_MSY="${WH_TMP_DIR}/Компоненты поддержки T-FLEX 17/Access Database Engine 2007"
        AUTOINSTALL_EXE_WIN2="${WH_TMP_DIR}/Компоненты поддержки T-FLEX 17/TSC2/Setup_TSC2.msi"
        if try_download "https://www.tflex.ru/downloads/T-FLEX%2017%20Prerequisites.zip" "${AUTOINSTALL_ZIP_WIN}" ; then
            7z x -y "${AUTOINSTALL_ZIP_WIN}" -o"${AUTOINSTALL_DIRWIN}"
            try_remove_file "$AUTOINSTALL_ZIP_WIN"

            7z x -y "${AUTOINSTALL_EXE_WIN1}" -o"${AUTOINSTALL_EXE_WIN_MSY}"
            try_remove_file "$AUTOINSTALL_EXE_WIN1"

            wine_run_install "${AUTOINSTALL_EXE_WIN_MSY}/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 "${WH_TMP_DIR}/Prerequisites_T-FLEX_17_Linux/Components/Program Files/Common Files/System" "$DRIVE_C/Program Files/Common Files"
        cp -r "${WH_TMP_DIR}/Prerequisites_T-FLEX_17_Linux/Components/Program Files (x86)/Common Files/System" "$DRIVE_C/Program Files (x86)/Common Files"
        mv -f "${WH_TMP_DIR}/Prerequisites_T-FLEX_17_Linux/Components/Windows/System32/"*.dll "$DRIVE_C/windows/system32"

        "$WINELOADER" regedit "${WH_TMP_DIR}/Prerequisites_T-FLEX_17_Linux/Components/ado-32.reg"
        "$WINELOADER"64 regedit "${WH_TMP_DIR}/Prerequisites_T-FLEX_17_Linux/Components/ado-64.reg"
        "$WINELOADER" regedit "${WH_TMP_DIR}/Prerequisites_T-FLEX_17_Linux/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 \"${WH_TMP_DIR}/Prerequisites_T-FLEX_17_Linux/HASP_Linux/aksusbd/\"
        bash ./dinst
        mv -f \"${WH_TMP_DIR}/Prerequisites_T-FLEX_17_Linux/HASP_Linux/\"*.so /var/hasplm/
        systemctl restart hasplmd.service" > "${WH_TMP_DIR}/tflex17_aksusbd.sh"
        chmod +x ${WH_TMP_DIR}/tflex17_aksusbd.sh
        su_run "${WH_TMP_DIR}/tflex17_aksusbd.sh"
        try_remove_file "${WH_TMP_DIR}/tflex17_aksusbd.sh"
    fi
    try_remove_dir "$AUTOINSTALL_DIR_LIN"
    try_remove_dir "${WH_TMP_DIR}/Компоненты поддержки T-FLEX 17/"
fi

# Программа T-FLEX Viewer
export AUTOINSTALL_ZIP="${WH_TMP_DIR}/T-FLEX Viewer 17.zip"
export AUTOINSTALL_DIR="${WH_TMP_DIR}"
export AUTOINSTALL_EXE="${WH_TMP_DIR}/T-FLEX Viewer 17/T-FLEX Viewer 17.msi"

if try_download "https://www.tflex.ru/downloads/T-FLEX%20Viewer%2017.zip" "${AUTOINSTALL_ZIP}" ; then
    7z x -y "${AUTOINSTALL_ZIP}" -o"${AUTOINSTALL_DIR}"
    try_remove_file "${AUTOINSTALL_ZIP}"

    wine_run_install "${AUTOINSTALL_EXE}" /q
    try_remove_file "${AUTOINSTALL_EXE}"

    WIN_FILE_EXEC="$DRIVE_C/Program Files/T-FLEX Viewer 17/Program/TFlexViewer.exe"
    create_desktop "$PROG_NAME" "$WIN_FILE_EXEC" "$PROG_ICON"
fi
