#!/bin/bash

#  It is recommended to test the script on a local machine for its purpose and effects.
#  ManageEngine Desktop Central will not be responsible for any
#  damage/loss to the data/setup based on the behavior of the script.
#  Description - Script for Big Sur Upgrade

#  Parameters - "<SERVERPATH> - Path to the network share(where the file or folder is present)>"
#               Example with username and password - "smb://administrator:password@172.21.201.118/D/" 
#.              Example without username and password - "smb://172.21.201.118/D/" 

#  Configuration Type - COMPUTER

# MacOS Big Sur is copyed to the agent Mac machine

SERVERPATH="smb://Administrator:admin@192.168.195.147/D/Srinath/"

mkdir "/private/tmp/sharePoint"
MOUNTPATH="/private/tmp/sharePoint"

COPYPATH="/private/tmp"

FILEORFOLDERNAME="$MOUNTPATH/installBigSur.tar.gz"

mount -t smbfs "$SERVERPATH" "$MOUNTPATH"

cp -R "$FILEORFOLDERNAME" "$COPYPATH"

# MacOS Big Sur system comptability check

osvers_minor=$(sw_vers -productVersion | awk -F. '{print $2}')
osvers_max=$(sw_vers -productVersion | awk -F. '{print $1}')

machine_model=`sysctl hw.model`

machine_model=$(echo $machine_model| cut -d' ' -f 2)

if [[ ${osvers_minor} -gt 8 ]];then #10.9 or later (Mavericks)
    
    if [[ ${osvers_max} -lt 12 ]];then 
        
        if [[ $machine_model = *"MacBookPro"* ]];then
            
            machine_model_number=$(echo $machine_model| cut -d',' -f 1)
            
            machine_model_number=${machine_model_number:10:2}
            
            if [ $machine_model_number -lt "11" ];then #Late 2013
            
                echo "Big Sur is not compatible to your machine. As your MacBookPro model is $machine_model and it should be minimum of MacBookPro11,1"
                
                exit 1;
                
            fi
            
        elif [[ $machine_model = *"MacBookAir"* ]];then
            
            machine_model_number=$(echo $machine_model| cut -d',' -f 1)
            
            machine_model_number=${machine_model_number:10:2}
            
            if [ $machine_model_number -lt "6" ];then #2013 and later
                
                echo "Big Sur is not compatible to your machine. As your MacBookAir model is $machine_model and it should be minimum of MacBookAir6,1"
                
                exit 1;
                
            fi

        elif [[ $machine_model = *"MacBook"* ]];then

            machine_model_number=$(echo $machine_model| cut -d',' -f 1)

            machine_model_number=${machine_model_number:7:2}

            if [ $machine_model_number -lt "8" ];then #Early 2015
            
                echo "Big Sur is not compatible to your machine. As your MacBook model is $machine_model and it should be minimum of MacBook8,1"

                exit 1;

            fi

            
        elif [[ $machine_model = *"iMacPro"* ]];then
            
            machine_model_number=$(echo $machine_model| cut -d',' -f 1)
            
            machine_model_number=${machine_model_number:7:2}
            
            if [ $machine_model_number -lt "1" ];then # 2017 and later
            
                echo "Big Sur is not compatible to your machine. As your iMacPro model is $machine_model and it should be minimum of imacPro1,1"
                
                exit 1;
                
            fi

        elif [[ $machine_model = *"iMac"* ]];then

            machine_model_number=$(echo $machine_model| cut -d',' -f 1)

            machine_model_number=${machine_model_number:4:2}

            if [ $machine_model_number -lt "15" ];then # 2014 and later
            
                echo "Big Sur is not compatible to your machine. As your iMac model is $machine_model and it should be minimum of iMac15,1"

                exit 1;

            fi

        elif [[ $machine_model = *"Macmini"* ]];then
            
            machine_model_number=$(echo $machine_model| cut -d',' -f 1)
            
            machine_model_number=${machine_model_number:7:2}
            
            if [ $machine_model_number -lt "7" ];then # 2014 and later
            
                echo "Big Sur is not compatible to your machine. As your MacMini model is $machine_model and it should be minimum of MacMini7,1"
                
                exit 1;
                
            fi
            
        elif [[ $machine_model = *"MacPro"* ]];then
            
            machine_model_number=$(echo $machine_model| cut -d',' -f 1)
            
            machine_model_number=${machine_model_number:6:2}
            
            if [ $machine_model_number -lt "6" ];then # 2013 and later
            
                echo "Big Sur is not compatible to your machine. As your MacPro model is $machine_model and it should be minimum of MacPro6,1"
                
                exit 1;
                
            fi
            
        fi
        
    else
    
        echo "macOS Big Sur is already installed"
        
        exit 0;
        
    fi
    
else
    
    echo "macOS Big Sur can't be installed in this machine. The minimum OS requred is 10.9"
    exit 1;
    
fi

# MacOS Big Sur Installation

tar -xzf /private/tmp/installBigSur.tar.gz -C /private/tmp

osascript -e 'tell app "Finder" to display dialog "Your OS will be upgraded to new Mac OS Big Sur. System restarts in 5 minutes. Kindly save your data."'

filename=/private/tmp/Install\ macOS\ Big\ Sur.app

filesize=`du -hs "$filename"`

echo $filesize

if [ -e /private/tmp/Install\ macOS\ Big\ Sur.app/Contents/Resources/startosinstall ];then

    echo "./Install macOS Big Sur.app/Contents/Resources/startosinstall executable exists"

else
    echo "./Install macOS Big Sur.app/Contents/Resources/startosinstall executable doesnot exists"
    
    exit 1;

fi
# ./Install\ macOS\ Big\ Sur.app/Contents/Resources/startosinstall --volume / --rebootdelay 300 --nointeraction
/private/tmp/Install\ macOS\ Big\ Sur.app/Contents/Resources/startosinstall --volume / --rebootdelay 300 --nointeraction
errcode=$?
if [ "$errcode" -ne 0 ];
then
echo ""
echo "Failed to Upgrade with errorcode $errcode" 1>&2
echo ""
exit 1
fi
echo "Big Sur upgraded successfully" 1>&2

umount "$MOUNTPATH"
rm -R "$MOUNTPATH"

exit 0
