#!/bin/bash

rm -f /tmp/id_rsa || true
ssh-keygen -b 4096 -t rsa -f /tmp/id_rsa -q -N ""
chown sandbox /tmp/id_rsa
chmod 400 /tmp/id_rsa
echo "$(ssh-keygen -y -f /tmp/id_rsa) sandbox@localhoost" > /mnt/c/ProgramData/ssh/sb_key

/mnt/c/Windows/System32/icacls.exe 'c:/ProgramData/ssh/sb_key' '/reset'
/mnt/c/Windows/System32/icacls.exe 'c:/ProgramData/ssh/sb_key' '/inheritance:r'
/mnt/c/Windows/System32/icacls.exe 'C:/ProgramData/ssh/sb_key' '/grant' 'sandbox:(F)'
/mnt/c/Windows/System32/icacls.exe 'C:/ProgramData/ssh/sb_key' '/grant' 'system:(F)'
/mnt/c/Windows/System32/icacls.exe 'C:/ProgramData/ssh/sb_key' '/grant' 'Administrators:(F)'

if [[ "$@" == '' ]]; then
    ssh -i /tmp/id_rsa -lsandbox localhost -p222 -t "cd C:$( echo $TASK_DIR | sed 's@/mnt/c@@g' | sed 's@/@\\@g') & powershell.exe"
else
    ssh -i /tmp/id_rsa -lsandbox localhost -p222 $@
fi
