{pkgs', ...}: let
  bashFktScript = import ./bash-fkt.nix {
    inherit pkgs';
  };
in
  pkgs'.writeShellScriptBin "update-changelog" ''

    source ${pkgs'.common}/bin/common
    ${bashFktScript}

    echo_section "Update Changelog"
    echo_hint "This script will update the changelog."
    echo_hint "The command is executed in the current working directory and not in a nix derivation."
    cd_working_dir

    echo_step "Updating changelog"

    CHANGELOG_PATH="./CHANGELOG.md"
    latestDocumentedVersion=$(${pkgs'.gnugrep}/bin/grep '## \['  $CHANGELOG_PATH |  sed -E 's/## \[([0-9]+\.[0-9]+\.[0-9]+).*/\1/' | head -1)
    if [ -z "$latestDocumentedVersion" ]; then
         echo_fail "Could not find latest documented version in changelog"
         echo_hint "Please add a version entry in the file $CHANGELOG_PATH"
         exit 1
    fi

    currentVersion=$(${pkgs'.version}/bin/version print -g)
    if [ "$latestDocumentedVersion" = "$currentVersion" ]; then
      echo_ok "changelog already up to date"
    else

        tags=$(${pkgs'.git}/bin/git tag --sort=creatordate)
        tags_array=($tags)
        position=$(${pkgs'.coreutils}/bin/printf "%s\n" "''${tags_array[@]}" | ${pkgs'.gnugrep}/bin/grep -n "^$latestDocumentedVersion$" | ${pkgs'.coreutils}/bin/cut -d: -f1)
        if [ -z "$position" ]; then
            echo_fail "Could not find latest documented version in git tags"
            echo_hint "Please check the git tags"
            exit 1
        fi

        next_position=$((position + 1))

        if [ $next_position -le ''${#tags_array[@]} ]; then
            next_tag=''${tags_array[''$next_position - 1]}
        else
            echo_fail "Could not find next tag"
            echo_hint "Please check the git tags"
            exit 1
        fi

        echo_step "Generating changelog"
        tmpChangelog=$(${pkgs'.mktemp}/bin/mktemp)
        if ! ${pkgs'.git-chglog}/bin/git-chglog --config ./.config/chglog/config.yml -o $tmpChangelog $next_tag..
        then
          echo_fail "git-chglog failed"
          ${pkgs'.coreutils}/bin/rm $tmpChangelog
          exit 1
        fi

        newChanges=$(${pkgs'.coreutils}/bin/cat $tmpChangelog)
        ${pkgs'.coreutils}/bin/rm $tmpChangelog

        if [ -n "$newChanges" ]; then
          ${pkgs'.gnused}/bin/sed -i '/^# Changelog/d' $CHANGELOG_PATH
          ${pkgs'.coreutils}/bin/mv $CHANGELOG_PATH ''${CHANGELOG_PATH}.old

          echo -e "# Changelog\n\n" > $CHANGELOG_PATH
          echo "$newChanges" >> $CHANGELOG_PATH

          ${pkgs'.coreutils}/bin/cat "''${CHANGELOG_PATH}.old" >> $CHANGELOG_PATH
          ${pkgs'.coreutils}/bin/rm ''${CHANGELOG_PATH}.old
          echo_ok "changelog updated"
        fi
    fi

    echo_ok "Change log updated"
  ''