#! /usr/bin/perl
#  VERSION 1.3
# __________________________________________  P Fan - 12/2021
#
# NAME
#
#   screenshare [ start | restart | stop | status ]
#   
# SYNOPSIS
#
#   This will attempt to do the above for the screenshare service.
#   It is for Yosemite and forward.  
#
#   This requires root except for status.  If no argument is given, then
#   it will report its status.
#   
#   It checks the status of screenshare via
# launchctl print-disabled system | grep com.apple.screensharing | grep true
# launchctl print system/com.apple.screensharing
#
#   For screenshare to work, you need it to be enabled and booted up.
#   Sometimes even when both are up, it still will not work - most likely
#   one of them is wedged.  Almost always restarting both will fix it
#      screenshare restart
#
#   start
#      We start up whatever of the above two is not up.
#
#   stop 
#      We stop whatever of the above two is not up.
# 
#   restart
#      Basically it does what is in stop.  Then it is enabled and 
#      bootstrap up.
#
#   status
#      This tells you if screenshare is disable/enabled and booted out/up.  
#
#   
#   If screenshare is wedged then you should do  screenshare restart
#
#   Note: launchctl output may depend on the user invoking it.
# ____________________________________________________________________


    # *********************************
    # PRELIMINARIES
    # *********************************

    # what do you want to do

$restart = $start = $stop = $status = "";
if (@ARGV > 0) {
    if ($ARGV[0] eq "start") {
	print "\nYou have requested screenshare be started.\n";
	$start = "y";
    }
    elsif ($ARGV[0] eq "restart") {
	print "\nYou have requested screenshare be restarted.\n";
	$restart = "y";
    }
    elsif ($ARGV[0] eq "stop") {
	print "\nYou have requested screenshare be stopped.\n";
	$stop = "y";
    }
    elsif ($ARGV[0] eq "status") {
	print "\nYou have requested the status of screenshare.\n";
	$status = "y";
    }

    else {
 	die "\nUsage: screenshare [ start | restart | stop | status ]\n\n";
    }
}
else {
    print "\nYou have requested the status of screenshare.\n";
    $status = "y";
}


    # will do status report for any user but other tasks require root

if ($status ne "y") {
    $ENV{USER} eq "root" || 
       die "\nYou must be root to use this program.\n\n";
}



    # Get a single value for $ver
    #
    # Let  ($v1, $v2)  be the first two numbers of sw_vers.
    # If $v1 > 10, then it is big sur or above and we set $ver be $v1 + 5
    # Otherwise, $ver is set to $v2.

chomp ($osversion = `sw_vers -productVersion`);
($v1, $v2) = (split (/\./, $osversion)) [0,1];

if ($v1 > 10) {
    $ver = $v1 + 5; 	# translate version to a single value
}
else {
    $ver = $v2;
}


$ver > 9 || die "\nThis screenshare program is for Yosemite forward.\n\n";





    # *********************************
    # START WORK
    # *********************************

    # Get screenshare status.

$id = "com.apple.screensharing";
$sdisable = $sout = "";
$screendisable = $screenout = "";

   # Is screenshare disabled or booted out?

$sdisable = 
        `launchctl print-disabled system | grep $id | grep "[true|disabled]"`;
$sout = `launchctl print system/$id 2> /dev/null`;

if ($sdisable ne "") {
    $screendisable = "y";
}
if ($sout eq "") {
    $screenout = "y";
}



    # =====================
    # Start up screenshare
    # =====================

if ($start eq "y") {
    if ($screendisable eq "y") {
	print "\nScreenshare appear to be disabled.\n".
              "I will try to enable it.\n";
	`launchctl enable system/com.apple.screensharing`;
    }
    if ($screenout eq "y") {
	print "\nScreenshare appear to be booted out.\n".
              "I will try to boot it up.\n";
	`launchctl bootstrap system /System/Library/LaunchDaemons/com.apple.screensharing.plist`;
    }

    print "\nScreenshare should now be enabled and booted up.\n".
	"If screenshare still does not work, it may be wedged.  Try doing\n".
	"screenshare restart  to fix it.\n\n";
}


    # =====================
    # Restart screenshare
    # =====================

elsif ($restart eq "y") {
    print "\nI will bootout and/or disable screenshare when appropriate.\n";

    if ($screenout ne "y") {
        `launchctl bootout system /System/Library/LaunchDaemons/com.apple.screensharing.plist`;
    }
    if ($screendisable ne "y") {
	`launchctl disable system/com.apple.screensharing`;
    }

    print "\nScreenshare is now booted out and disabled.\n";

    `launchctl enable system/com.apple.screensharing`;
    `launchctl bootstrap system /System/Library/LaunchDaemons/com.apple.screensharing.plist`;

    print "I have now enable screenshare and booted it up.\n";
}



    # =====================
    # Stop screenshare
    # =====================

elsif ($stop eq "y") {
    print "\nI am going to bootout and disable screenshare.\n";
    if ($screenout ne "y") {
        `launchctl bootout system /System/Library/LaunchDaemons/com.apple.screensharing.plist`;
    }
    if ($screendisable ne "y") {
	`launchctl disable system/com.apple.screensharing`;
    }
}



    # =====================
    # Status of screenshare
    # =====================

elsif ($status eq "y") {
    print "\nHere are the status of screenshare.\n";

    if ($screendisable eq "y") {
	print "   Screenshare is disabled.\n";
    }
    else {
	print "   Screenshare is enabled.\n";
    }

    if ($screenout eq "y") {
	print "   Screenshare is booted out.\n";
    }
    else {
	print "   Screenshare is booted up.\n";
    }
}




print "\n";





    # *********************************
    # SPECIAL CASE
    # *********************************
    # For Yosemite, we print a note.

if ($ver < 11) {
    print "For Yosemite and lower, you may need to use the following.\n".
     "launchctl unload /System/Library/LaunchDaemons/com.apple.screensharing.plist\n".
     "launchctl load /System/Library/LaunchDaemons/com.apple.screensharing.plist\n\n";
}

