#!/usr/bin/env bash
# info_ru: SCAD Office — это программный комплекс для расчёта строительных конструкций, с дополнением Apache OpenOffice. Apache OpenOffice - пакет офисного программного обеспечения для обработки текстов, электронных таблиц, презентаций, графики, баз данных и многого другого.
########################################################################
export WH_WINE_USE="wine_x_tkg_10-0_amd64"
export WINEPREFIX="scadoffice"
export BASE_PFX="scadaoffice_pfx_x64_v03"
export WH_WINDOWS_VER="10"
export WINEARCH="win64"
export INSTALL_DLL="dotnet20 dotnet472 dotnet48 gdiplus vcrun6sp6 vcrun2005 vcrun2019 d3dx11_42 d3dx11_43 d3dx9 d3dcompiler_42 d3dcompiler_43 d3dcompiler_46 d3dcompiler_47 richtx32 riched30 riched20 msxml6"
export WH_XDG_OPEN="rtf"
AUTOINSTALL_EXE="${WH_TMP_DIR}/SCADOffice_installer.exe"
SCADOFFICE_ADDONS_URL="https://cloud.linux-gaming.ru/portproton/scadoffice_addons_v02.tar.xz"

prepair_wine

DRIVE_C_SCADOFFICE="$DRIVE_C/SCAD Soft/SCADOffice/64"

if [[ -n $2 ]] ; then
    if [[ ! -d "$DRIVE_C_SCADOFFICE" ]] ; then
        fatal "Перезапустите скрипт без дополнительных аргументов для установки SCAD Office:
         winehelper install scadoffice"
    elif [[ ! -f "$2" ]] ; then
        fatal "Перезапустите скрипт с правильным путем до файла лицензии SCAD Office:
         winehelper install scadoffice \"/путь/до/файла.exe\""
    else
        print_info "Устанавливаем файл лицензии $2"
        wine_run "$2"
        exit 0
    fi
fi

if [[ $BASE_PFX == "none" ]] ; then
    print_info "Установка дополнительных компонентов..."

    ADDONS_PACK="${WH_TMP_DIR}/$(basename "$SCADOFFICE_ADDONS_URL")"
    ADDONS_PATH="${WH_TMP_DIR}/scadoffice_addons"

    ADDONS_PATH_REG="${ADDONS_PATH}/REG"
    ADDONS_PATH_MDAC="${ADDONS_PATH}/mdac64"
    ADDONS_PATH_OPENSSH="${ADDONS_PATH}/OpenSSH"

    if try_download "$SCADOFFICE_ADDONS_URL" "${ADDONS_PACK}" ; then
        create_new_dir "${ADDONS_PATH}"
        unpack "${ADDONS_PACK}" "${ADDONS_PATH}"
        wine_run regedit "${ADDONS_PATH_REG}"/*.reg

        # Установка ODBC
        rm -fR "$DRIVE_C/Program Files (x86)/Common Files/System"
        cp -r "${ADDONS_PATH_MDAC}/System" "$DRIVE_C/Program Files (x86)/Common Files/System"
        cp -r "${ADDONS_PATH_MDAC}"/*.* "$DRIVE_C/windows/system32/"
        wine_run regedit "${ADDONS_PATH_MDAC}"/*.reg

        # Установка SSH
        cp -r "${ADDONS_PATH_OPENSSH}" "$DRIVE_C/windows/system32/"

        try_remove_dir "$ADDONS_PATH"
        try_remove_file "$ADDONS_PACK"
    fi
fi

if try_download "https://scadhelp.ru/files/10/download" "${AUTOINSTALL_EXE}" ; then
    create_new_dir "$DRIVE_C/SDATA"
    create_new_dir "$DRIVE_C/SWORK"
    wine_run_install "${AUTOINSTALL_EXE}" /auto /hide
    try_remove_file "$AUTOINSTALL_EXE"

    # Определение всех программ, значков и исполняемых файлов
    declare -a PROG_NAME=("Арбат" "Вест" "Глобальные настройки" "Декор" "Дискретная арматура" "Запрос" "Камин" "КоКон" "Комета" "Конструктор сечений" "Консул" "Конфигуратор лицензий" "Кристалл" "Кросс" "Куст" "Магнум" "Монолит" "Откос" "Пастернак" "Преобразование ед. измерений" "Cортамент металлопроката" "Расчет по формуле" "Редактор акселерограмм" "Редактор динамичности" "Редактор материалов" "Тонус" "Эквивалентное сечение" "SCAD++")

    declare -a PROG_ICON=("arbat" "west" "globalsettings" "decor" "rebar" "underground" "comein" "cocon" "comet" "section" "consul" "configprotection" "kristall" "cross" "kust" "magnum" "monolit" "slope" "pasternak" "ucalc" "viewprof" "scalc" "acceledit" "betaedit" "materialsedit" "tonus" "sezam" "scadx")

    declare -a WIN_FILE_EXEC=("${DRIVE_C_SCADOFFICE}/Arbat.exe" "${DRIVE_C_SCADOFFICE}/West.exe" "${DRIVE_C_SCADOFFICE}/GlobalSettings.exe" "${DRIVE_C_SCADOFFICE}/Decor.exe" "${DRIVE_C_SCADOFFICE}/ReBar.exe" "${DRIVE_C_SCADOFFICE}/UnderGround.exe" "${DRIVE_C_SCADOFFICE}/ComeIn.exe" "${DRIVE_C_SCADOFFICE}/CoCon.exe" "${DRIVE_C_SCADOFFICE}/Comet2.exe" "${DRIVE_C_SCADOFFICE}/Section.exe" "${DRIVE_C_SCADOFFICE}/Consul.exe" "${DRIVE_C_SCADOFFICE}/ConfigProtection.exe" "${DRIVE_C_SCADOFFICE}/Kristall.exe" "${DRIVE_C_SCADOFFICE}/Cross.exe" "${DRIVE_C_SCADOFFICE}/Kust2.exe" "${DRIVE_C_SCADOFFICE}/Magnum.exe" "${DRIVE_C_SCADOFFICE}/Monolit.exe" "${DRIVE_C_SCADOFFICE}/Slope.exe" "${DRIVE_C_SCADOFFICE}/Pasternak.exe" "${DRIVE_C_SCADOFFICE}/UCalc.exe" "${DRIVE_C_SCADOFFICE}/ViewProf.exe" "${DRIVE_C_SCADOFFICE}/SCalc.exe" "${DRIVE_C_SCADOFFICE}/AccelEdit.exe" "${DRIVE_C_SCADOFFICE}/BetaEdit.exe" "${DRIVE_C_SCADOFFICE}/MaterialsEdit.exe" "${DRIVE_C_SCADOFFICE}/Tonus.exe" "${DRIVE_C_SCADOFFICE}/Sezam.exe" "${DRIVE_C_SCADOFFICE}/SCADX.exe")

    # Цикл создания десктоп файлов для scad office
    for i in "${!PROG_NAME[@]}"; do
        create_desktop "${PROG_NAME[i]}" "${WIN_FILE_EXEC[i]}" "${PROG_ICON[i]}" "nocopy"
    done
fi
