#!/bin/bash

# Color definitions for output formatting
RED='\033[0;31m'
GRN='\033[0;32m'
BLU='\033[0;34m'
NC='\033[0m' # No Color

echo ""
echo -e "${GRN}MDM Bypass Tool for MacOS on Apple Silicon${NC}"
echo -e "${BLU}Running Bypass on Recovery Mode...${NC}"
echo ""

# Rename disk if needed
if [ -d "/Volumes/Macintosh HD - Data" ]; then
    diskutil rename "Macintosh HD - Data" "Data"
fi

echo -e "${GRN}Preparing Disk...${NC}"
echo -e "${BLU}Creating New User Account${NC}"

# Set default account credentials (no user input required)
realName="MAC"
username="MAC"
passw="1234"

# Set path for user database
dscl_path='/Volumes/Data/private/var/db/dslocal/nodes/Default'

echo -e "${GRN}Creating user account: $username${NC}"

# Create the user account with all necessary attributes
dscl -f "$dscl_path" localhost -create "/Local/Default/Users/$username"
dscl -f "$dscl_path" localhost -create "/Local/Default/Users/$username" UserShell "/bin/zsh"
dscl -f "$dscl_path" localhost -create "/Local/Default/Users/$username" RealName "$realName"
dscl -f "$dscl_path" localhost -create "/Local/Default/Users/$username" UniqueID "501"
dscl -f "$dscl_path" localhost -create "/Local/Default/Users/$username" PrimaryGroupID "20"

# Create user home directory
mkdir "/Volumes/Data/Users/$username"
dscl -f "$dscl_path" localhost -create "/Local/Default/Users/$username" NFSHomeDirectory "/Users/$username"

# Set user password
dscl -f "$dscl_path" localhost -passwd "/Local/Default/Users/$username" "$passw"

# Add user to admin group
dscl -f "$dscl_path" localhost -append "/Local/Default/Groups/admin" GroupMembership "$username"

echo -e "${GRN}User account created successfully${NC}"

# Block Apple's MDM enrollment servers by adding entries to hosts file
echo -e "${BLU}Blocking MDM enrollment servers...${NC}"
echo "0.0.0.0 deviceenrollment.apple.com" >> /Volumes/Macintosh\ HD/etc/hosts
echo "0.0.0.0 mdmenrollment.apple.com" >> /Volumes/Macintosh\ HD/etc/hosts
echo "0.0.0.0 iprofiles.apple.com" >> /Volumes/Macintosh\ HD/etc/hosts
echo -e "${GRN}MDM servers blocked successfully${NC}"

# Mark setup as complete and remove/create MDM configuration files
echo -e "${BLU}Configuring system settings...${NC}"
touch /Volumes/Data/private/var/db/.AppleSetupDone

# Remove existing MDM configuration records
rm -rf /Volumes/Macintosh\ HD/var/db/ConfigurationProfiles/Settings/.cloudConfigHasActivationRecord
rm -rf /Volumes/Macintosh\ HD/var/db/ConfigurationProfiles/Settings/.cloudConfigRecordFound

# Create files to indicate MDM bypass
touch /Volumes/Macintosh\ HD/var/db/ConfigurationProfiles/Settings/.cloudConfigProfileInstalled
touch /Volumes/Macintosh\ HD/var/db/ConfigurationProfiles/Settings/.cloudConfigRecordNotFound

echo -e "${GRN}System configuration complete${NC}"
echo ""

# Disable startup chime
echo -e "${BLU}Disabling startup chime...${NC}"
nvram StartupMute=%01
echo -e "${GRN}Startup chime disabled${NC}"
echo ""

echo -e "${GRN}=== MDM Bypass Complete ===${NC}"
echo -e "${GRN}User: $username${NC}"
echo -e "${GRN}Password: $passw${NC}"
echo ""
reboot