#!/usr/bin/env bash
set -euo pipefail

# Close any open System Preferences panes, to prevent them from overriding
# settings we’re about to change
osascript -e 'tell application "System Preferences" to quit'

# Ask for the administrator password upfront
sudo -v

# Keep-alive: update existing `sudo` time stamp until `.macos` has finished
while true; do
  sudo -n true
  sleep 60
  kill -0 "$$" || exit
done 2>/dev/null &

# Save to disk (not to iCloud) by default
defaults write NSGlobalDomain NSDocumentSaveNewDocumentsToCloud -bool false

# Disable the “Are you sure you want to open this application?” dialog
defaults write com.apple.LaunchServices LSQuarantine -bool false

# Disable Typing features
defaults write NSGlobalDomain NSAutomaticCapitalizationEnabled -bool false
defaults write NSGlobalDomain NSAutomaticDashSubstitutionEnabled -bool false
defaults write NSGlobalDomain NSAutomaticPeriodSubstitutionEnabled -bool false
defaults write NSGlobalDomain NSAutomaticQuoteSubstitutionEnabled -bool false
defaults write NSGlobalDomain NSAutomaticSpellingCorrectionEnabled -bool false

# Disable press-and-hold for keys in favor of key repeat
defaults write NSGlobalDomain ApplePressAndHoldEnabled -bool false
defaults write NSGlobalDomain KeyRepeat -int 2
defaults write NSGlobalDomain InitialKeyRepeat -int 15

defaults write NSGlobalDomain AppleLanguages -array "en" "es" "bg"
defaults write NSGlobalDomain AppleLocale -string "en_US@rg=eszzzz"

## Finder

# Screenshots/captures
defaults write com.apple.screencapture location -string "${HOME}/Pictures/Screenshots"
defaults write com.apple.screencapture style -string "display"
defaults write com.apple.screencapture target -string "file"
defaults write com.apple.screencapture video -int 1

# Finder
# Interface elements
defaults write com.apple.finder ShowPathbar -bool true
defaults write com.apple.finder ShowStatusBar -bool true
defaults write com.apple.finder ShowSidebar -bool true
defaults write com.apple.finder ShowRecentTags -bool false

# View and sorting
defaults write com.apple.finder FXPreferredViewStyle -string "Nlsv"
defaults write com.apple.finder FXPreferredSearchViewStyle -string "Nlsv"
defaults write com.apple.finder _FXSortFoldersFirst -bool true
defaults write com.apple.finder FXPreferredGroupBy -string "None"
defaults write com.apple.finder FXDefaultSearchScope -string "SCcf"

# Behavior
defaults write com.apple.finder NewWindowTarget -string "PfHm"
defaults write com.apple.finder FXOpenFoldersInTabs -bool true
defaults write com.apple.finder FXRemoveOldTrashItems -bool false
defaults write com.apple.finder FXShowAllExtensions -bool true
defaults write com.apple.finder FXEnableExtensionChangeWarning -bool true
defaults write com.apple.finder FXRemoveICloudDriveWarning -bool true
defaults write com.apple.finder FXWarnBeforeEmptyingTrash -bool true

# Desktop icons (none)
defaults write com.apple.finder ShowHardDrivesOnDesktop -bool false
defaults write com.apple.finder ShowExternalHardDrivesOnDesktop -bool false
defaults write com.apple.finder ShowRemovableMediaOnDesktop -bool false
defaults write com.apple.finder ShowConnectedServersOnDesktop -bool false

# Tags
defaults write com.apple.finder FavoriteTagNames -array

# iCloud
defaults write com.apple.finder FXICloudDriveEnabled -bool false

# Finder: show all filename extensions
defaults write NSGlobalDomain AppleShowAllExtensions -bool true

# Avoid creating .DS_Store files on network or USB volumes
defaults write com.apple.desktopservices DSDontWriteNetworkStores -bool true
defaults write com.apple.desktopservices DSDontWriteUSBStores -bool true

## Dock

#!/bin/bash
set -euo pipefail

# Reset preferences
# rm -f ~/Library/Preferences/com.apple.dock.plist

# Reset Dock layout
defaults write com.apple.dock persistent-apps -array
defaults write com.apple.dock persistent-others -array

# Basic Dock preferences
defaults write com.apple.dock autohide -bool true
defaults write com.apple.dock autohide-delay -float 0
defaults write com.apple.dock autohide-time-modifier -float 0.4
defaults write com.apple.dock enterMissionControlByTopWindowDrag -bool false
defaults write com.apple.dock expose-group-apps -bool true
defaults write com.apple.dock mineffect -string "scale"
defaults write com.apple.dock minimize-to-application -bool false
defaults write com.apple.dock orientation -string "bottom"
defaults write com.apple.dock show-process-indicators -bool false
defaults write com.apple.dock show-recents -bool false
defaults write com.apple.dock showAppExposeGestureEnabled -bool true
defaults write com.apple.dock showDesktopGestureEnabled -bool false
defaults write com.apple.dock showLaunchpadGestureEnabled -bool false
defaults write com.apple.dock tilesize -int 38

# Add Brave Browser
defaults write com.apple.dock persistent-apps -array-add \
  "<dict>
  <key>tile-data</key>
  <dict>
    <key>file-data</key>
    <dict>
      <key>_CFURLString</key>
      <string>/Applications/Brave Browser.app</string>
      <key>_CFURLStringType</key>
      <integer>0</integer>
    </dict>
  </dict>
  <key>tile-type</key>
  <string>file-tile</string>
</dict>"

# Add Ghostty
defaults write com.apple.dock persistent-apps -array-add \
  "<dict>
  <key>tile-data</key>
  <dict>
    <key>file-data</key>
    <dict>
      <key>_CFURLString</key>
      <string>/Applications/Ghostty.app</string>
      <key>_CFURLStringType</key>
      <integer>0</integer>
    </dict>
  </dict>
  <key>tile-type</key>
  <string>file-tile</string>
</dict>"

# Add Screenshots directory (display as folder, show as grid)
defaults write com.apple.dock persistent-others -array-add \
  "<dict>
  <key>tile-data</key>
  <dict>
    <key>displayas</key>
    <integer>1</integer>
    <key>showas</key>
    <integer>2</integer>
    <key>file-data</key>
    <dict>
      <key>_CFURLString</key>
      <string>/Users/tomas/Pictures/Screenshots</string>
      <key>_CFURLStringType</key>
      <integer>0</integer>
    </dict>
  </dict>
  <key>tile-type</key>
  <string>directory-tile</string>
</dict>"

# Add Downloads directory (display as folder, show as grid)
defaults write com.apple.dock persistent-others -array-add \
  "<dict>
  <key>tile-data</key>
  <dict>
    <key>displayas</key>
    <integer>1</integer>
    <key>showas</key>
    <integer>2</integer>
    <key>file-data</key>
    <dict>
      <key>_CFURLString</key>
      <string>/Users/tomas/Downloads</string>
      <key>_CFURLStringType</key>
      <integer>0</integer>
    </dict>
  </dict>
  <key>tile-type</key>
  <string>directory-tile</string>
</dict>"

# Apply changes
killall Finder &>/dev/null
killall Dock &>/dev/null
echo "Setup complete."
