#!/bin/sh
#################################################################
# Created by Yuriy Kuzin
# Use it an modify it whatever and ehenever you want.
# You can remove who create this script.
# Modified: 30/08/2014
################################################################
echo "Configuring daemon and vnc";
xrdppid=`ps -A|grep xrdp |grep -v grep|grep -v sesman|grep -v preinstall|grep -v postinstal|grep -v preupgrade|grep -v postupgrade | awk '{print $1;}'|head -n 1`;
if echo $xrdppid | egrep -q '^[0-9]+$'; then
    echo "killing xrdp";
    kill $xrdppid;
else
    echo "Ok, no xrdp process. Continue...";
fi
xrdpsesmanpid=`ps -A|grep xrdp-sesman |grep -v grep|grep -v preinstall|grep -v postinstal|grep -v preupgrade|grep -v postupgrade | awk '{print $1;}'|head -n 1`;
if echo $xrdpsesmanpid | egrep -q '^[0-9]+$'; then
    echo "kiling xrdp-sesman";
    kill $xrdpsesmanpid;
else
    echo "Ok, no xrdp-sesman proces. Continue...";
fi
if [ -f "/Library/LaunchDaemons/my.xrdp.plist" ]; then
    echo "loading daemon";
    launchctl load -F /Library/LaunchDaemons/my.xrdp.plist
    sleep 10
else
    echo "Error! Nothing o load, file doesn't exist: /Library/LaunchDaemons/my.xrdp.plist";
    exit 1;
fi
xrdpservicepid=`launchctl list|grep xrdp|awk '{print $1;}'|head -n 1`;
if [ "$xrdpservicepid" == "-" ]; then
    echo "starting daemon";
    sleep 10
    launchctl start my.xrdp;
else
    echo "Impossible start my.xrdp daemon because it wasn't installed.";
    exit 1;
fi
echo "Configuring VNC...";
/System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -activate -configure -access -on -clientopts -setvnclegacy -vnclegacy yes -clientopts -setvncpw -vncpw 12345678 -restart -agent -privs -all;
echo "Configuration finished.";
exit 0;
