final: prev:
{
  hoffice = prev.stdenv.mkDerivation rec {
    pname = "hoffice";
    version = "11.20.0.1520";

    src = prev.fetchurl {
      url = "https://dl.dropbox.com/scl/fi/ia3ub05nti01h8lzb3vwr/1732118678_hoffice_11.20.0.1520_amd64.deb?rlkey=8bnxl9chpm7rt6sr6nc4eoqp0&st=yaxlb481&dl=0";
      sha256 = "1ecb2f82e915b49706d1f5f6d206f8bd4a9384fda2bd56798c94046865fe5730";
      name = "hoffice-${version}.deb";
    };

    kimeLib = prev.fetchurl {
      url = "https://github.com/Riey/kime/releases/latest/download/libkime-qt-5.11.3.so";
      sha256 = "sha256-26udhVyqW+z5Calp3x4uSPM34/u1kSWViJv3LGWeEG0=";
    };

    nativeBuildInputs = with prev; [
      wget
      libarchive
      xdg-utils
      makeWrapper
      autoPatchelfHook
    ];

    dontWrapQtApps = true;

    # dontPatchELF = true;

    buildInputs = with prev; [
      cairo
      fontconfig
      freetype
      glibc
      libGLU
      (harfbuzz.override { withIcu = true; })
      cups
      curl
      curlWithGnuTls
      icu63
      openssl_1_1
      libxcb
      libsForQt5.qtbase
      libsForQt5.qtx11extras
      libsForQt5.qtwayland
      libsForQt5.qtmultimedia
      libsForQt5.qtwebsockets
      zlib
      pulseaudio
      gst_all_1.gstreamer
      gst_all_1.gst-plugins-base
      gst_all_1.gst-plugins-good
      alsa-lib
      nss
      nspr
      gtk3
      atk
      pango
      gdk-pixbuf
      xorg.libXcursor
      xorg.libXdamage
      xorg.libXtst
      xorg.libXrandr
      xorg.libX11
      xorg.libXext
      xorg.libXrender
      xorg.libXi
      xorg.libXfixes
      xorg.libXcomposite
      xorg.libXxf86vm
      libdrm
      mesa
      libglvnd
      kime
    ];

    unpackPhase = ''
      mkdir -p $out/tmp
      cd $out/tmp
      bsdtar -xf ${src}
      bsdtar -xf data.tar.xz
      cd -
    '';

    installPhase = ''
      mkdir -p $out/opt
      cp -r $out/tmp/opt/hnc $out/opt/

      mkdir -p $out/usr/share/licenses/hoffice
      cp ${./LICENSE} $out/usr/share/licenses/hoffice/LICENSE

      # Install input method libraries
      mkdir -p "$out/opt/hnc/hoffice11/Bin/qt/plugins/platforminputcontexts"
      cp ${kimeLib} "$out/opt/hnc/hoffice11/Bin/qt/plugins/platforminputcontexts/libkime-qt-5.11.3.so"

      # Create wrapper scripts
      mkdir -p $out/bin
      libPath="${prev.lib.makeLibraryPath [
        prev.glibc
        prev.libxcb
        prev.libsForQt5.qtbase
        prev.libsForQt5.qtx11extras
        prev.libsForQt5.qtwayland
        prev.libsForQt5.qtmultimedia
        prev.libsForQt5.qtwebsockets
        prev.cairo
        prev.fontconfig
        prev.freetype
        (prev.harfbuzz.override { withIcu = true; })
        prev.icu63
        prev.cups
        prev.curl
        prev.curlWithGnuTls
        prev.openssl_1_1
        prev.libGLU
        prev.zlib
        prev.pulseaudio
        prev.gst_all_1.gstreamer
        prev.gst_all_1.gst-plugins-base
        prev.gst_all_1.gst-plugins-good
        prev.alsa-lib
        prev.nss
        prev.nspr
        prev.gtk3
        prev.atk
        prev.pango
        prev.gdk-pixbuf
        prev.xorg.libXcursor
        prev.xorg.libXdamage
        prev.xorg.libXtst
        prev.xorg.libXrandr
        prev.xorg.libX11
        prev.xorg.libXext
        prev.xorg.libXrender
        prev.xorg.libXi
        prev.xorg.libXfixes
        prev.xorg.libXcomposite
        prev.xorg.libXxf86vm
        prev.libdrm
        prev.mesa
        prev.libglvnd
        prev.kime
      ]}"
      for cmd in hwp hsl hword hcl; do
        mv "$out/opt/hnc/hoffice11/Bin/$cmd" "$out/opt/hnc/hoffice11/Bin/$cmd.real"
        makeWrapper "$out/opt/hnc/hoffice11/Bin/$cmd.real" "$out/opt/hnc/hoffice11/Bin/$cmd" \
          --set LD_LIBRARY_PATH "$libPath" \
          --set QT_QPA_PLATFORM xcb \
          --set QT_XCB_GL_INTEGRATION none \
          --set QT_OPENGL software \
          --set QT_QUICK_BACKEND software \
          --set LIBGL_ALWAYS_SOFTWARE 1
        makeWrapper "$out/opt/hnc/hoffice11/Bin/$cmd.real" "$out/bin/$cmd" \
          --set LD_LIBRARY_PATH "$libPath" \
          --set QT_QPA_PLATFORM xcb \
          --set QT_XCB_GL_INTEGRATION none \
          --set QT_OPENGL software \
          --set QT_QUICK_BACKEND software \
          --set LIBGL_ALWAYS_SOFTWARE 1
      done

      rm -rf $out/tmp
    '';

    postInstall = ''
      xdg-icon-resource forceupdate --theme hicolor &>/dev/null || true
      update-desktop-database -q || true
    '';

    meta = with prev.lib; {
      description = "Office document editor for Linux - Hancom Office Editor";
      longDescription = ''
        본 어플리케이션은 리눅스용 문서 편집 프로그램으로, 
        한컴에 의해 개발되고 배포된 형식의 문서들을 편집할 수 있도록 해주는 프로그램입니다.
      '';
      homepage = "https://www.hancom.com";
      license = licenses.unfree;
      platforms = [ "x86_64-linux" ];
      maintainers = [{
        name = "imnyang";
        email = "me@imnya.ng";
      }];
    };
  };
}
