#!/bin/bash

source /etc/environment

wsl2_type_env=${WSL2:=0}
wsl2_type=$1; shift

# Fix for older versions
apt-mark unhold libc6 >/dev/null 2>&1
sed -i 's/^DISPLAY=.*$//g' /etc/environment

if [ ! $wsl2_type_env -eq "$wsl2_type" ]; then
  if [ "$wsl2_type" -eq 0 ]; then
    # if destination is WSL1
    # remove WSL2 env variable and cleanup empty row
    sed -i -e '/^WSL2=.*$/d' -e '/^$/d' /etc/environment
  elif [ $wsl2_type_env -eq 0 ]; then
    # if origin is WSL1
    # append WSL2 env variable
    echo "WSL2=$wsl2_type" >> /etc/environment
  else
    sed -i 's/^WSL2=.*$/WSL2='$wsl2_type'/g' /etc/environment
  fi
fi


# cleanup
unset wsl2_type
