{ pkgs ? import <nixpkgs> {}, stdenv, fetchurl, lib }:

# pkgs.buildFHSUserEnv {
stdenv.mkDerivation rec {
  name = "PenTablet";
  targetPkgs = pkgs: [];
  src = pkgs.fetchurl {
    # Replace the URL with the actual download link of the driver
    url = "https://www.xp-pen.com/download/file.html?id=2901&pid=977&ext=gz";

    sha256 = "1wyl96lpw8wvzfq3q5q0njp2ca8j5h89psi8j0lmrhymxhp94ml7";  # Replace with the actual sha256
  };
  pname = "pentablet";
  version = "3.2.1.210426";  # Replace with the actual version

  extraBuildCommands = ''
    mkdir -p $out/etc/xdg/autostart
    mkdir -p $out/lib/udev/rules.d
    mkdir -p $out/usr/lib/pentablet/{conf/xppen,doc,lib,platforms}
    mkdir -p $out/usr/share/{applications,icons/hicolor/256x256/apps}
    mkdir -p $PWD/tmp

    # Extract and install to FHS directories under $out
    tar -xzf /path/to/XPPenLinux3.4.9-240607.tar.gz -C $PWD/tmp

    cp $PWD/tmp/App/etc/xdg/autostart/xppentablet.desktop $out/etc/xdg/autostart/
    cp $PWD/tmp/App/lib/udev/rules.d/10-xp-pen.rules $out/lib/udev/rules.d/
    cp -r $PWD/tmp/App/usr/lib/pentablet/conf/xppen/* $out/usr/lib/pentablet/conf/xppen/
    cp -r $PWD/tmp/App/usr/lib/pentablet/doc/* $out/usr/lib/pentablet/doc/
    cp -r $PWD/tmp/App/usr/lib/pentablet/lib/* $out/usr/lib/pentablet/lib/
    cp $PWD/tmp/App/usr/lib/pentablet/platforms/libqxcb.so $out/usr/lib/pentablet/platforms/
    cp $PWD/tmp/App/usr/lib/pentablet/PenTablet $out/usr/lib/pentablet/
    cp $PWD/tmp/App/usr/lib/pentablet/PenTablet.sh $out/usr/lib/pentablet/
    cp $PWD/tmp/App/usr/lib/pentablet/resource.rcc $out/usr/lib/pentablet/
    cp $PWD/tmp/App/usr/share/applications/xppentablet.desktop $out/usr/share/applications/
    cp $PWD/tmp/App/usr/share/icons/hicolor/256x256/apps/xppentablet.png $out/usr/share/icons/hicolor/256x256/apps/

    # Set appropriate permissions based on the script
    chmod 0555 $out/usr/lib/pentablet/PenTablet
    chmod 0555 $out/usr/lib/pentablet/PenTablet.sh
    chmod 0777 $out/usr/lib/pentablet/conf/xppen
    chmod 0666 $out/usr/lib/pentablet/conf/xppen/config.xml
    chmod 0666 $out/usr/lib/pentablet/conf/xppen/language.ini
    chmod 0666 $out/usr/lib/pentablet/conf/xppen/name_config.ini
    chmod 0666 $out/usr/lib/pentablet/resource.rcc

    # Ensure autostart and application icons have the correct permissions
    chmod 0444 $out/usr/share/applications/xppentablet.desktop
    chmod 0555 $out/usr/share/icons/hicolor/256x256/apps/xppentablet.png

    # Create the lockfile
    touch $out/tmp/qtsingleapp-Pentab-9c9b-lockfile
    chmod 0666 $out/tmp/qtsingleapp-Pentab-9c9b-lockfile
  '';

  # Use bind mounts to mirror the required paths
  bindPaths = [
    { hostPath = "$out/etc/xdg"; containerPath = "/etc/xdg"; }
    { hostPath = "$out/lib/udev"; containerPath = "/lib/udev"; }
    { hostPath = "$out/usr/lib/pentablet"; containerPath = "/usr/lib/pentablet"; }
    { hostPath = "$out/usr/share"; containerPath = "/usr/share"; }
    { hostPath = "$out/tmp"; containerPath = "/tmp"; }
  ];

  runScript = ''
    export LD_LIBRARY_PATH=/usr/lib/pentablet/lib
    export QT_PLUGIN_PATH=/usr/lib/pentablet
    export XPPEN_CONFIG_DIR=/usr/lib/pentablet/conf/xppen

    # Launch the application
    /usr/lib/pentablet/PenTablet.sh
  '';

  meta = with lib; {
    description = "Remote Controll Drivers.";
    license = licenses.mit;  # Replace with correct license
    maintainers = with maintainers; [ "anonymous" ];
  };
}
