echo_section "Build and publish"

    echo_step "Update auto-generated files"
    #update-files
    ${script-build-stylesheets}/bin/build-stylesheets
    #build-monster-file

    echo_step "Check if git is clean"
    if [[ -n "$(git status --porcelain)" ]]; then
        echo_fail "Git is not clean. Exiting."
        echo_hint "You must commit all changes before you can publish a new version."
      #  exit 1
    fi
set -x
    echo_step "Get next version"
    NEXTVERSION="$(${versionBin} predict --git -0)"
    EXITCODE=$?
    if [ $EXITCODE -ne 0 ]; then
        echo_fail "Version prediction failed. Exiting."
        echo_hint "You must add a new commit with feat: or fix: in the commit message to trigger a new version."
        echo_hint "You can also run the version script manually to see the error message."
        exit 1
    fi

    echo_ok "New version: $NEXTVERSION"
    
    echo_step "Update package.json and version.mjs"
    LASTVERSION=${packageData.version}
    #jq ".version = \"$NEXTVERSION\"" "${config.devenv.root}/package.json" | ${pkgs.moreutils}/bin/sponge "${config.devenv.root}/package.json"
    #update-versions
    
    echo_ok "Package.json and version.mjs updated"

    PROJECT_ROOT="/"
    TEST_PATH="/test/"
    BUILD_PATH="/dist/"

    if [ -d "$BUILD_PATH" ]; then
        echo_step "Remove old build directory"
        rm -rf "$BUILD_PATH"
    fi
pwd
ls -lah

    echo_step "Create new build directory"
    mkdir -p "''${BUILD_PATH}"

    echo_step "Build changelog"
    ${pkgs.git-chglog}/bin/git-chglog --next-tag ''$NEXTVERSION --config "${config.devenv.root}/.chglog/config.yml" \
        -o "${config.devenv.root}/CHANGELOG.md"

    echo_step "Copy files to build directory"
    ${pkgs.rsync}/bin/rsync -a --exclude-from="${config.devenv.root}/.gitignore" "${config.devenv.root}/source" "''${BUILD_PATH}"
    ${pkgs.rsync}/bin/rsync -a --exclude-from="${config.devenv.root}/.gitignore" "${config.devenv.root}/test" "''${BUILD_PATH}"
    ${pkgs.rsync}/bin/rsync -a --exclude-from="${config.devenv.root}/.gitignore" "${config.devenv.root}/example" "''${BUILD_PATH}"

    cp ${config.devenv.root}/README.md "''${BUILD_PATH}"
    cp ${config.devenv.root}/LICENSE "''${BUILD_PATH}"
    cp ${config.devenv.root}/CHANGELOG.md "''${BUILD_PATH}"

    jq 'del(.devDependencies)' ${config.devenv.root}/package.json >  "''${BUILD_PATH}/package.json"

    git add ${config.devenv.root}/package.json
    git add ${config.devenv.root}/source/types/version.mjs
    git add ${config.devenv.root}/CHANGELOG.md
    git add ${config.devenv.root}/test/cases/monster.mjs
    if ! git commit -m "chore: release and publish to npm new version $NEXTVERSION" ; then
      echo -e "''${RED}✖ Commit failed. Exiting.''${RESET}"
      exit 1
    fi

    cd ''${BUILD_PATH} || exit 1
    
    echo_ok "Files copied to build directory"

    OPTIONS=""
    if [ ! -f "${config.devenv.root}/.npmrc" ]; then
      echo_fail "No .npmrc file found."
      echo_hint "You must create a .npmrc file in the root of the project to publish to npm."
      echo_hint "You can find the token in the npm account settings."
      echo_hint "If you want to publish to npm, you need to create a .npmrc file in the root of the project."
      echo_hint "  //registry.npmjs.org/:_authToken=$NPM_TOKEN"
      echo_hint "You can find the token in the npm account settings."
      
      echo_step "We run in dry-run mode now."
      OPTIONS="--dry-run --no-git-checks"
    fi

    echo_section "Publish to npm"
    pnpm publish $OPTIONS --access public
    exitcode=$?
    cd - || exit 1

    if [ -n "$OPTIONS" ] || [ $exitcode -ne 0 ]; then

      ## reset to last version
      jq ".version = \"$LASTVERSION\"" "${config.devenv.root}/package.json" | ${pkgs.moreutils}/bin/sponge "${config.devenv.root}/package.json"
      update-versions
      echo_fail "Publishing failed. Exiting."
      echo_step "Reset to last version"
      git reset --hard HEAD~1
      exit 1
    fi

    echo_step "Tag new version"
    if ! git tag -a ''$NEXTVERSION -m "chore tag new version $NEXTVERSION"
    then
      echo_fail "Tag creation failed. Exiting."
      exit 1
    fi
    echo_ok "Tag created"
    echo_ok "Publish successful"
    
    
    *********************  
  
  
  
  
  

          -run-dev:
            silent: true
            desc: Start and run the development server
            cmds:
              - run-development

          -run-showroom:
            silent: true
            desc: Start and run the showroom server
            cmds:
              - run-showroom

          -build-and-publish-showroom:
            silent: true
            desc: Build the showroom to dist/showroom and publish it to the s3 bucket
            cmds:
              - build-and-publish-showroom

          -create-new-showroom-page:
            silent: true
            desc: Create a new showroom page
            cmds:
              - create-new-showroom-page {{.CLI_ARGS}}

          -run-theme-generator:
            silent: true
            desc: Start and run the theme generator
            cmds:
              - run-theme-generator

          -create-issue:
            silent: true
            desc: open an issue in gitlab and create a file in the issues folder
            cmds:
              - create-issue

          -build-doc:
            silent: true
            desc: Build the documentation to dist/doc
            aliases:
              - bd
            cmds:
              - build-doc

          -publish-doc:
            silent: true
            desc: Publish the documentation to dist/doc
            aliases:
              - bpd
            cmds:
              - publish-doc


          -run-web-tests:
            silent: true
            desc: Run the tests
            aliases:
              - rwt
            cmds:
              - run-web-tests

          -format-and-lint-code:
            silent: true
            desc: Format and lint the code
            aliases:
              - flc
            cmds:
              - format-and-lint-code

          -create-new-component-class:
            silent: true
            desc: Create a new component class
            cmds:
              - create-new-component-class {{.CLI_ARGS}}

          -build-and-publish:
            silent: true
            desc: Build the app and publish it to the npm registry
            cmds:
              - build-and-publish


*****************
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  scripts.update-versions.exec = ''

    VERSION=$(${pkgs.coreutils}/bin/cat "${config.devenv.root}/package.json" | jq -r '.version')

    ${pkgs.jq}/bin/jq ".version = \"$VERSION\"" "${config.devenv.root}/package.json" | ${pkgs.moreutils}/bin/sponge "${config.devenv.root}/package.json"

    ${pkgs.gnused}/bin/sed -i -E "s_(\"[0-9]+\.[0-9]+\.[0-9]+\")_\"''${VERSION}\"_g" "${config.devenv.root}/source/types/version.mjs"
    ${pkgs.gnused}/bin/sed -i -E "s_(\"[0-9]+\.[0-9]+\.[0-9]+\")_\"''${VERSION}\"_g" "${config.devenv.root}/test/cases/monster.mjs"


  '';

  scripts.run-development.exec = ''

    if ! ${pkgs.nodePackages.pnpm}/bin/pnpx vite --config "${config.devenv.root}/development/vite.config.js"; then
        echo "ERROR: Vite build failed, check your JS!"
        exit 1
    fi
  '';

  scripts.run-theme-generator.exec = ''
    if ! ${pkgs.nodePackages.pnpm}/bin/pnpx vite --config "${config.devenv.root}/theme-generator/vite.config.js"; then
        echo "ERROR: Vite build failed, check your JS!"
        exit 1
    fi
  '';

  scripts.run-showroom.exec = ''
    if ! ${pkgs.nodePackages.pnpm}/bin/pnpx vite --config "${config.devenv.root}/showroom/vite.config.js"; then
        echo "ERROR: Vite build failed, check your JS!"
        exit 1
    fi
  '';
  
  scripts.create-new-showroom-page.exec = ''
       #!${pkgs.bash}/bin/bash
       source ${commonFunctionsScript}
          
       echo_section "create new showroom page"
          
       if ! ${pkgs.nodejs_20}/bin/node ${config.devenv.root}/development/scripts/createNewShowroomPage.mjs "$@" 
       then
         echo_fail "script createNewClass.mjs failed, check your JS!"
         exit 1
       fi
       echo_ok "New showroom page created"
  '';
  
  scripts.update-showroom-navigation.exec = ''
       #!${pkgs.bash}/bin/bash
       source ${commonFunctionsScript}
          
       echo_section "update showroom navigation"
          
       if ! ${pkgs.nodejs_20}/bin/node ${config.devenv.root}/development/scripts/updateShowroomNavigation.mjs 
       then
         echo_fail "script updateShowroomNavigation.mjs failed, check your JS!"
         exit 1
       fi
       echo_ok "Showroom navigation updated"
  '';
  
  
  scripts.build-and-publish-showroom.exec = ''

    rm -rf "${config.devenv.root}/showroom/dist/showroom"

    if ! ${pkgs.nodePackages.pnpm}/bin/pnpx vite build --config "${config.devenv.root}/showroom/vite.config.js"; then
        echo "ERROR: Vite build failed, check your JS!"
        exit 1
    fi
    
    mv "${config.devenv.root}/showroom/dist/source" "${config.devenv.root}/showroom/dist/showroom"
    ${pkgs.awscli}/bin/aws --profile=shopcloud s3 --recursive --only-show-errors cp ${config.devenv.root}/showroom/dist/ s3://monsterjs.org/en/
    
  '';

  scripts.create-issue.exec = ''
       #!${pkgs.bash}/bin/bash
       source ${commonFunctionsScript}

       echo_section "create new issue"

       echo_step "enter issue title"
       issue_text=$(gum input --placeholder "Enter issue title")

       echo_step "enter issue description (End with Ctrl+D)"
       issue_description=$(gum write --placeholder "Enter issue description. End with Ctrl+D")

       if [[ -z "$issue_text" ]]; then
          log_fail "Issue title is empty. Exiting."
          exit 1
       fi

       escaped_issue_text=$(sed 's/"/\\"/g' <<< "$issue_text")
       escaped_issue_description=$(sed 's/"/\\"/g' <<< "$issue_description")

       echo $escaped_issue_text
       echo $escaped_issue_description

       issue_output=$(glab issue create -t"$issue_text" --no-editor --description "$issue_description")
       if [ $? -ne 0 ]; then
          echo_fail "Issue creation failed. Exiting."
          exit 1
       fi
       issue_id=$(echo "$issue_output" | grep -oP '(?<=/issues/)\d+')
       if [ -z "$issue_id" ]; then
          echo_fail "Issue creation failed. Exiting."
          exit 1
       fi

       echo_ok "Issue with id $issue_id created"

       echo_step "create new issue file in test/cases"
       issue_dir="${config.devenv.root}/development/issues/open"
       mkdir -p "$issue_dir"

       cat <<EOF > $issue_dir/''${issue_id}.mjs
    /**
    * @file development/issues/open/''${issue_id}.mjs
    * @url https://gitlab.schukai.com/oss/libraries/javascript/monster/-/issues/''${issue_id}
    * @description ''${escaped_issue_text}
    * @issue ''${issue_id}
    */

    import "../../../source/components/style/property.pcss";
    import "../../../source/components/style/normalize.pcss";
    import "../../../source/components/style/typography.pcss";

    EOF

        cat <<EOF > $issue_dir/''${issue_id}.html
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>''${escaped_issue_text} #''${issue_id}</title>
      <script src="./''${issue_id}.mjs" type="module"></script>
    </head>
    <body>
        <h1>''${escaped_issue_text} #''${issue_id}</h1>
        <p>''${escaped_issue_description}</p>
        <ul>
            <li><a href="https://gitlab.schukai.com/oss/libraries/javascript/monster/-/issues/''${issue_id}">Issue #''${issue_id}</a></li>
            <li><a href="/">Back to overview</a></li>
        </ul>
        <main>
              <!-- Write your code here -->
        </main>

    </body>
    </html>
    EOF

        echo_ok "Issue file created"
        echo_hint "You can find the issue file in ''${issue_dir}/''${issue_id}.mjs"
        echo_hint "You can find the issue html file in ''${issue_dir}/''${issue_id}.html"

  '';

  scripts.create-polyfill.exec = ''

    TMPFILE=${config.devenv.root}/.devenv/monster.js
    touch $TMPFILE
    ##trap "rm -f $TMPFILE" 0 2 3 15

    ${config.devenv.root}/node_modules/.bin/esbuild --platform=browser --bundle ${config.devenv.root}/source/monster.mjs --outfile=''${TMPFILE}

    url="$(${pkgs.nodejs_20}/bin/npx create-polyfill-service-url analyse --file ".devenv/monster.js")"

    if [ ! -x {$url} ]
    then
      sed -i -E "/id=\"polyfill\"/s|.*|   <script id=\"polyfill\" src=\"''${url}\"|g"  ${config.devenv.root}/test/web/test.html
      sed -i -E "/id=\"polyfill\"/s|.*|<script id=\"polyfill\" src=\"''${url}\"|g"  ${config.devenv.root}/README.md
    fi

  '';


  scripts.build-monster-file.exec = ''
    #!${pkgs.bash}/bin/bash
    source ${commonFunctionsScript}
    
    echo_section "build monster file"
    
    if ! ${pkgs.nodejs_20}/bin/node ${config.devenv.root}/development/scripts/buildMonsterFile.mjs 
    then
      echo_fail "script buildMonsterFile.mjs failed, check your JS!"
      exit 1
    fi
    echo_ok "Monster file created"
  '';
  
  scripts.create-new-component-class.exec = ''
    #!${pkgs.bash}/bin/bash
    source ${commonFunctionsScript}
    
    echo_section "create new component class"
    
    if ! ${pkgs.nodejs_20}/bin/node ${config.devenv.root}/development/scripts/createNewComponentClass.mjs "$@" 
    then
      echo_fail "script createNewClass.mjs failed, check your JS!"
      exit 1
    fi
    echo_ok "New component class created"
  '';

  scripts.run-web-tests.exec = ''
    PROJECT_ROOT="${config.devenv.root}"
    TEST_PATH="${config.devenv.root}/test/"
    TEST_CASES_PATH="${config.devenv.root}/test/cases/"

    VERSION=$(cat "${config.devenv.root}/package.json" | jq -r '.version')

    update-versions
    if ! build-monster-file ; then
      echo "ERROR: build-monster-file failed, check your JS!"
      exit 1
    fi
    create-polyfill

    find ''${TEST_CASES_PATH} -type f | sed "s|^$TEST_CASES_PATH||" > ''${TEST_PATH}web/import.js
    sed -i 's|^|import "../cases/|' ''${TEST_PATH}web/import.js
    sed -i 's|$|";|' ''${TEST_PATH}web/import.js
    sed -i "1 i import \"./prepare.js\";"  ''${TEST_PATH}web/import.js
    sed -i "1 i /** this file was created automatically by the run-web-tests script */"  ''${TEST_PATH}web/import.js

    if ! npx esbuild --platform=browser --sourcemap=inline --external:ws --external:jsdom --external:fs --external:process --external:crypto --bundle ''${TEST_PATH}web/import.js --outfile=''${TEST_PATH}web/tests.js
    then
      echo "ERROR: esbuild failed, check your JS!"
      exit 1
    fi

    sed -i '1s/^/try {\n/' ''${TEST_PATH}web/tests.js
    echo "} catch (e) {" >> ''${TEST_PATH}web/tests.js
    echo "document.getElementById('mocha-errors').insertAdjacentHTML('afterbegin', e );" >> ''${TEST_PATH}web/tests.js
    echo "document.getElementById('mocha-stats').style.backgroundColor = 'red';" >> ''${TEST_PATH}web/tests.js
    echo "}" >> ''${TEST_PATH}web/tests.js

    sed -i -E "/<h1/s_.*_  <h1 style='margin-bottom: 0.1em;'>Monster ''${VERSION}</h1>_" ''${TEST_PATH}web/test.html
    sed -i -E "/id=\"lastupdate\"/s_.*_  <div id=\"lastupdate\" style='font-size:0.7em'>last update $(date)</div>_" ''${TEST_PATH}web/test.html
    sed -i -E "s_src=\"([\"]*)\.js.*\"_src=\"\1.js?r=$(date +"%T")\"_" ''${TEST_PATH}web/test.html


    ${pkgs.xdg-utils}/bin/xdg-open ''${TEST_PATH}web/test.html

  '';

  scripts.publish-doc.exec = ''
    #!${pkgs.bash}/bin/bash

    if [ -t 1 ]; then
      RED='\033[0;31m'
      GREEN='\033[0;32m'
      RESET='\033[0m'
      BOLD='\033[1m'
    else
      RED=""
      GREEN=""
      RESET=""
    fi

    profileExists=$(aws configure list-profiles | grep shopcloud | wc -l)
    if [ "$profileExists" -eq "0" ]; then
      echo "No shopcloud profile found. Exiting."
      echo "Please run: aws configure --profile=shopcloud"
      exit 1
    fi

    if [ ! -d ${config.devenv.root}/dist/doc ]; then
        echo -e "''${RED}✖ No doc found.''${RESET}"
        exit 1
    fi

    ${pkgs.awscli2}/bin/aws --profile=shopcloud s3 --recursive --only-show-errors cp ${config.devenv.root}/dist/doc/ s3://monsterjs.org/en/api/
    code=$?

    if [ "$code" -ne "0" ]; then
        echo -e "''${RED}✖ Upload failed.''${RESET}"
        exit 1
    fi
    echo -e "''${GREEN}✔ Upload successful.''${RESET}"

  '';

  scripts.build-doc.exec = ''
    rm -rf ${config.devenv.root}/dist/doc

    update-versions
    create-polyfill

    if [ -d ${config.devenv.root}/dist/doc ]; then
        rm -rf ${config.devenv.root}/dist/doc
        mkdir -p ${config.devenv.root}/dist/doc
    fi

    ${config.devenv.root}/node_modules/.bin/jsdoc -c ${config.devenv.root}/doc/jsdoc.json
    ${pkgs.nodejs_20}/bin/node ${config.devenv.root}/development/scripts/replaceSkypack.cjs

  '';

  scripts.do-commit.exec = ''
    #!${pkgs.bash}/bin/bash

    # Define colors if the terminal supports it
    if [ -t 1 ]; then
      RED='\033[0;31m'
      GREEN='\033[0;32m'
      RESET='\033[0m'
      BOLD='\033[1m'
    else
      RED=""
      GREEN=""
      RESET=""
    fi

    step=1

    reset
    clear

    # create random log file
    LOGFILE="$(mktemp)"
    if [ $? -ne 0 ]; then
      echo -e "''${RED}✖ Could not create temporary log file. Exiting.''${RESET}"
      exit 1
    fi

    log_and_display() {
      echo -e "''${GREEN}==> $step. $1''${RESET}" | tee -a $LOGFILE
      step=$((step + 1))
    }

    log_error_and_display() {
      echo -e "''${RED}==> $step. $1''${RESET}" | tee -a $LOGFILE
    }

    printLogfileAndExit() {
      exit_code=$1
      echo -e "\n\n========================================\n\n\n"

      echo -e "\n\n''${BOLD}Git and GitLab Automation Script''${RESET}\n\nI have performed the following actions:\n\n"
      cat "$LOGFILE"

      # Optional: Remove log file
      rm -f "$LOGFILE"

      if [ $exit_code -eq 0 ]; then
        echo -e "\n''${GREEN}✔''${RESET} All actions were successful" | tee -a $LOGFILE
      elif [ $exit_code -eq -1 ]; then
        echo -e "\n''${RED}✖''${RESET} The script was manually cancelled" | tee -a $LOGFILE
        exit_code=0
      else
        echo -e "\n''${RED}✖''${RESET} Some actions failed" | tee -a $LOGFILE
      fi

      exit $exit_code
    }

    print_headline() {
      local title=$1
      local underline=$(printf '─%.0s' $(seq 1 ''${#title}))
      echo -e "\n\n''${BOLD}''${title}\n''${underline}''${RESET}\n"
    }

    do_cancel() {
      echo -e "''${RED}==> ✖ Cancelled.''${RESET}" | tee -a $LOGFILE
      printLogfileAndExit -1
    }

    # Function for unified logging and display
    log_action() {
      if [ $? -eq 0 ]; then
        echo -e "    ''${GREEN}✔''${RESET} $1: Successful" | tee -a $LOGFILE
      else
        echo -e "    ''${RED}✖''${RESET} $1: Failed" | tee -a $LOGFILE
        printLogfileAndExit 1
      fi
    }

    print_what_to_do() {
      echo -e "\n\nWhat do you want to do?\n"
    }

    git_status=$(git status --porcelain)
    if [[ -z "$git_status" ]]; then
      log_error_and_display "No changes to commit. Exiting."
      printLogfileAndExit 0
    fi

    print_headline "Choose commit type"
    selection=$(gum choose "feat: (new feature for the user, not a new feature for build script)" "fix: (bug fix for the user, not a fix to a build script)" "chore: (updating grunt tasks etc.; no production code change)" "docs: (changes to the documentation)" "style: (formatting, missing semi colons, etc; no production code change)" "refactor: (refactoring production code, eg. renaming a variable)" "test: (adding missing tests, refactoring tests; no production code change)" "Cancel")

    commit_type=$(echo "$selection" | awk -F':' '{print $1}')

    if [[ "$commit_type" == "Cancel" ]]; then
      do_cancel
    fi

    log_and_display "You chose the commit type: $commit_type"

    # NEXT STEP ISSUE HANDLING ############################################################################################################
    #log_and_display "Issue handling"

    gitlabIssues=()
    while IFS= read -r line; do
      if [[ $line =~ ^# ]]; then
        id=$(echo "$line" | awk '{print substr($1, 2)}')
        title=$(echo "$line" | awk -F'about' '{print $1}' | awk '{$1=$2=""; print substr($0, 3)}')
        gitlabIssues+=("$id > $title")
      fi
    done < <(gum spin --spinner dot --show-output --title "Ask gitlab ..." -- glab issue list --output-format=details)

    ## if issues are available, ask if user wants to use an existing issue or create a new one
    createOption="Create new issue"
    existingOption="Use existing issue"
    cancelOption="Cancel"

    print_headline "Choose issue handling"
    if [ ''${#gitlabIssues[@]} -eq 0 ]; then
      log_and_display "There are no issues available."

      print_what_to_do
      choice=$(gum choose "$createOption" "$cancelOption")

    else
      log_and_display "There are ''${#gitlabIssues[@]} issues available."
      print_what_to_do
      choice=$(gum choose "$createOption" "$existingOption" "$cancelOption")
    fi

    if [[ "$choice" == "$cancelOption" ]]; then
      do_cancel
    fi

    ## array of issue ids
    work_on_issue_ids=()

    issue_text=""

    if [[ "$choice" == "$createOption" ]]; then
      print_headline "Create new issue"
      issue_text=$(gum input --placeholder "Enter issue title")
      echo -e "Enter issue description. ''${RED}End with Ctrl+D''${RESET}\n"
      issue_description=$(gum write --placeholder "Enter issue description. End with Ctrl+D")

      if [[ -z "$issue_text" ]]; then
        log_error_and_display "Issue title is empty. Exiting."
        printLogfileAndExit 1
      fi

      log_and_display "You entered the issue title: $issue_text"
      log_and_display "You entered the issue description: $issue_description"
      echo -e "\n"

      gum confirm "Do you want to create this issue?"
      # gum confirm exits with status 0 if confirmed and status 1 if cancelled.
      if [ $? -eq 1 ]; then
        do_cancel
      fi

      issue_output=$(glab issue create -t"$issue_text" --no-editor --description "$issue_description")
      issue_id=$(echo "$issue_output" | grep -oP '(?<=/issues/)\d+')

      work_on_issue_ids+=("$issue_id")

      log_action "glab issue with id $issue_id created"

    else

      print_headline "Use existing issue"
      echo -e "Select issue with arrow keys and press tab or space to select. Press enter to confirm.\n"
      issue_ids=$(gum choose --no-limit "''${gitlabIssues[@]}")

      # assign issue_ids to work_on_issue_ids. iterate over lines and take integer from beginning of line
      while IFS= read -r line; do
        work_on_issue_ids+=($(echo "$line" | grep -oP '^\d+'))
      done <<<"$issue_ids"

    fi

    if [ ''${#work_on_issue_ids[@]} -eq 0 ]; then
      log_and_display "No issue selected. Exiting."
      printLogfileAndExit 0
    fi

    # NEXT STEP COMMIT MESSAGE ############################################################################################################
    # print work_on_issue_ids
    work_on_issue_ids_string=""
    for i in "''${work_on_issue_ids[@]}"; do
      work_on_issue_ids_string+="#$i "
    done

    log_and_display "You chose to work on the following issues: ''${work_on_issue_ids_string}"


    print_headline "Check for changes to commit"

    # ' ' = unmodified
    # M = modified
    # T = file type changed (regular file, symbolic link or submodule)
    # A = added
    # D = deleted
    # R = renamed
    # C = copied (if config option status.renames is set to "copies")
    # U = updated but unmerged
    # https://man.freebsd.org/cgi/man.cgi?query=git-status&sektion=1&manpath=freebsd-release-ports

    count_all_changes=$(echo "$git_status" | wc -l)
    count_staged_changes=$(echo "$git_status" | grep -c '^M')
    count_new_staged_files=$(echo "$git_status" | grep -c '^A')
    count_staged_changes=$((count_staged_changes + count_new_staged_files))



    git_options_all="All $count_all_changes changes"
    git_options_staged="Only the $count_staged_changes staged changes"
    git_options_select_files="Select files"
    git_options_cancel="Cancel"

    git_options_array=()
    if [[ $count_all_changes -gt 0 ]]; then
      git_options_array+=("$git_options_all")
    fi

    if [[ $count_staged_changes -gt 0 ]]; then
      git_options_array+=("$git_options_staged")
    fi

    git_options_array+=( "$git_options_select_files" )
    git_options_array+=( "$git_options_cancel" )


    selection=$(gum choose "''${git_options_array[@]}")
    if [[ "$selection" == "$git_options_cancel" ]]; then
      do_cancel
    fi

    if [[ "$selection" == "$git_options_all" ]]; then
      git add -A
      echo "1"
    elif [[ "$selection" == "$git_options_select_files" ]]; then

      files=()
      while IFS= read -r line; do
        files+=("$line")
      done <<<"$git_status"

      selected_files=$(gum choose --no-limit "''${files[@]}")

      # no files selected
      if [[ -z "$selected_files" ]]; then
        log_and_display "No files selected. Exiting."
        printLogfileAndExit 0
      fi

      # add selected files
      while IFS= read -r line; do
        ## git proclimne could have letter, ? or ! at the beginning of the line
        file=$(echo "$line" | awk '{print $2}')
        if [[ -z "$file" || ! -f "$file" ]]; then
          log_and_display "No file found in line: $line"
          continue
        fi

        git add "$file"
      done <<<"$selected_files"

    fi

    ## count staged changes again and print
    count_staged_changes=$(echo "$git_status" | grep -c '^M')
    count_new_staged_files=$(echo "$git_status" | grep -c '^A')
    count_staged_changes=$((count_staged_changes + count_new_staged_files))

    log_and_display "You have $count_staged_changes staged changes to commit."

    # NEXT STEP COMMIT MESSAGE ############################################################################################################

    print_headline "Enter commit message"
    commit_message=$(gum input --placeholder "Enter commit message" --value "$commit_type: $issue_text $work_on_issue_ids_string")

    if [[ -z "$commit_message" ]]; then
      log_error_and_display "Commit message is empty. Exiting."
      printLogfileAndExit 1
    fi

    log_and_display "You entered the commit message: $commit_message"

    gum confirm "Do you want to commit with this message?"
    if [ $? -eq 1 ]; then
      do_cancel
    fi

    # NEXT STEP COMMIT ####################################################################################################################
    print_headline "Committing changes"

    if ! git commit -m "$commit_message" ; then
      log_error_and_display "Commit failed. Exiting."
      printLogfileAndExit 1
    fi

    log_and_display "Commit successful."

    # NEXT STEP PUSH ######################################################################################################################

    print_headline "Pushing changes"

    if ! git push ; then
      log_error_and_display "Push failed. Exiting."
      printLogfileAndExit 1
    fi

    log_and_display "Push successful."

    # Close issue ######################################################################################################################

    print_headline "Closing issues"

    for issue_id in "''${work_on_issue_ids[@]}"; do

      gum confirm "Do you want to close issue #$issue_id?"
      if [ $? -eq 1 ]; then
        continue
      fi

      if ! glab issue close "$issue_id" ; then
        log_error_and_display "Closing issue $issue_id failed. Exiting."
      else
        log_and_display "Closing issue $issue_id successful."
      fi
    done

    printLogfileAndExit 0
  '';
}