name: 'Get iTunes Headers & Kbsync'
inputs:
  apple_id:
    required: true
  apple_id_pwd:
    required: true
  ngrok_token:
    description: "Token for RDP debugging"
    required: false

runs:
  using: "composite"
  steps:
    - name: Check if initialized
      id: check
      run: |
        echo "Checking if we need to initialize iTunes again"
        if [[ "$(cat /c/.iTunesInitialized)" == "${{ inputs.ngrok-token }}" ]]; then
          echo "  Need to re-init iTunes"
          echo "NEED_INIT=1" >> $GITHUB_ENV
        else
          echo "  Needn't to do anything"
          echo "NEED_INIT=0" >> $GITHUB_ENV
        fi
      working-directory: ${{ github.action_path }}
      shell: bash
    - name: Setup iTunes
      if: ${{ env.NEED_INIT == 1 }}
      run: |
        echo Setup iTunes...
        start /wait taskkill /f /im iTunes* python*
        workflow_helper\iTunesInstall\install_itunes.bat
      working-directory: ${{ github.action_path }}
      shell: cmd
    
    - name: Setup Python Dependencies
      if: ${{ env.NEED_INIT == 1 }}
      run: |
        echo Setup Python Dependencies...
        pip3 install pywinauto frida Flask
      working-directory: ${{ github.action_path }}
      shell: cmd

    #- uses: NyaMisty/reverse-rdp-windows-github-actions-ng@master
    #  if: ${{ always() && github.event_name == 'workflow_dispatch' && github.event.inputs.itunes_debug_enabled }}
    #  with:
    #    ngrok-token: ${{ inputs.ngrok_token }}
    #    password: Aa123456
    #    foreground: false

    - name: Login iTunes
      if: ${{ env.NEED_INIT == 1 }}
      env:
        APPLEID: ${{ inputs.apple_id }}
        APPLEID_PWD: ${{ inputs.apple_id_pwd }}
      run: |
          echo Login iTunes...
          python3 workflow_helper/itunes_auto_login.py %APPLEID% %APPLEID_PWD%
      working-directory: ${{ github.action_path }}
      shell: cmd
    
    - name: Finish Initialization
      if: ${{ env.NEED_INIT == 1 }}
      env:
        APPLEID: ${{ inputs.apple_id }}
      run: |
        echo "Finish Initialization..."
        echo "$APPLEID" > /c/.iTunesInitialized
      working-directory: ${{ github.action_path }}
      shell: bash
    
    - name: Start Frida Header Server
      run: |
        echo Start Frida Header Server!
        curl -Lo psexec64.exe https://github.com/ComputerGuyYash/psexec/raw/main/PsExec64.exe
        psexec64.exe -accepteula -nobanner -i -d cmd /c "python3.exe workflow_helper/iTunesDownload/get_header.py > C:\get_header.log 2>&1"
        exit 0
      working-directory: ${{ github.action_path }}
      shell: cmd
    
    - name: Test Frida Header Server
      run: |
        sleep 5
        ret=0
        echo "---------------- Before query headers ----------------"
        cat /c/get_header.log
        curl --fail-with-body -vv 127.0.0.1:9000 || ret=$?
        echo "---------------- After query headers ----------------"
        cat /c/get_header.log
        exit $ret
      working-directory: ${{ github.action_path }}
      shell: bash
