copyEnv() {
	cat .env | grep "$1=\"" | sed "s/.*$1=\"\([^\"]*\)\".*/\1/" | pbcopy && sleep 1
	echo "Copied $1"
}

todoSearch() {
	rg "ltodo" -g "!*.sh" || true # ignore error code and bypass set -e
}

if [ "$1" = "--db" ]; then
	# Proxy database to localhost
	copyEnv DB_DATABASE
	copyEnv DB_PASSWORD
	copyEnv DB_USERNAME

    echo "Local port 3306 -> Remote MySQL port 3306"
    sshpass -p "$SERVER_PASSWORD" \
        ssh -L 3306:localhost:3306 $SERVER_USER@$SERVER_HOST
elif [ "$1" = "--git" ]; then
	todoSearch

	# Run lazygit on server
    sshpass -p "$SERVER_PASSWORD" \
        ssh -t $SERVER_USER@$SERVER_HOST \
        "cd $SERVER_PATH && ./lazygit"

    # After that sync remote with --delete flag
    bash remote-local.sh --delete
elif [ "$1" = "--todo" ]; then
	todoSearch
else
    sshpass -p "$SERVER_PASSWORD" \
        ssh -t $SERVER_USER@$SERVER_HOST \
        "cd $SERVER_PATH && exec \$SHELL"
fi
