env_username:   .ascii "USER="
env_shell:      .ascii "SHELL="
env_desktop:    .ascii "XDG_CURRENT_DESKTOP="
env_session:    .ascii "XDG_SESSION_TYPE="

.lcomm username, 65
.lcomm username_length, 8

.lcomm shell_path, 65
.lcomm shell_path_length, 8

.lcomm desktop, 65
.lcomm desktop_length, 8

.lcomm session, 65
.lcomm session_length, 8

parse_envs:
	mov  rsi, 5
	mov  rdx, offset env_username
	mov  rdi, offset username
	call find_env_var
	mov  username_length, rcx
	xor  rcx, rcx

	mov  rsi, 6
	mov  rdx, offset env_shell
	mov  rdi, offset shell_path
	call find_env_var
	mov  shell_path_length, rcx
	xor  rcx, rcx

	mov  rsi, 20
	mov  rdx, offset env_desktop
	mov  rdi, offset desktop
	call find_env_var
	mov  desktop_length, rcx
	xor  rcx, rcx

	mov  rsi, 17
	mov  rdx, offset env_session
	mov  rdi, offset session
	call find_env_var
	mov  session_length, rcx

	ret

find_env_var:
	mov rax, [rbp]
	xor r8, r8
	xor r9, r9

env_match_loop:
	test rax, rax
	je   find_env_var_done

	mov bl, [rax + r9]
	cmp bl, [rdx + r9]
	je  env_char_matched
	xor r9, r9

	inc r8
	mov rax, [rbp + r8 * 8]
	jmp env_match_loop

env_char_matched:
	inc r9
	cmp r9, rsi
	jne env_match_loop

	mov  rsi, rax
	add  rsi, r9
	call copy

find_env_var_done:
	ret
