diff --git a/home/.chezmoiscripts/universal/run_onchange_before_20-ensure-user-group.sh.tmpl b/home/.chezmoiscripts/universal/run_onchange_before_20-ensure-user-group.sh.tmpl index caadc0cc..7b19c39a 100644 --- a/home/.chezmoiscripts/universal/run_onchange_before_20-ensure-user-group.sh.tmpl +++ b/home/.chezmoiscripts/universal/run_onchange_before_20-ensure-user-group.sh.tmpl @@ -8,37 +8,42 @@ {{ includeTemplate "universal/profile-before" }} {{ includeTemplate "universal/logg-before" }} -### Ensure user has group of same name (required for Macports) -logg info "Ensuring user ($USER) has a group with the same name ($USER) and that it is a member. Sudo privileges may be required" +if [ -n "$USER" ]; then + logg info "Adding the $USER user to the $USER group" + ### Ensure user has group of same name (required for Macports) + logg info "Ensuring user ($USER) has a group with the same name ($USER) and that it is a member. Sudo privileges may be required" -GROUP="$USER" -USERNAME="$USER" + GROUP="$USER" + USERNAME="$USER" -### Add group -sudo dscl . create /Groups/$GROUP + ### Add group + sudo dscl . create /Groups/$GROUP -### Add GroupID to group -if [[ "$(sudo dscl . read /Groups/$GROUP gid 2>&1)" == *'No such key'* ]]; then - MAX_ID_GROUP="$(dscl . -list /Groups gid | awk '{print $2}' | sort -ug | tail -1)" - GROUP_ID="$((MAX_ID_GROUP+1))" - sudo dscl . create /Groups/$GROUP gid "$GROUP_ID" + ### Add GroupID to group + if [[ "$(sudo dscl . read /Groups/$GROUP gid 2>&1)" == *'No such key'* ]]; then + MAX_ID_GROUP="$(dscl . -list /Groups gid | awk '{print $2}' | sort -ug | tail -1)" + GROUP_ID="$((MAX_ID_GROUP+1))" + sudo dscl . create /Groups/$GROUP gid "$GROUP_ID" + fi + + ### Add user + sudo dscl . create /Users/$USERNAME + + ### Add PrimaryGroupID to user + if [[ "$(sudo dscl . read /Users/$USERNAME PrimaryGroupID 2>&1)" == *'No such key'* ]]; then + sudo dscl . create /Users/$USERNAME PrimaryGroupID 20 + fi + + ### Add UniqueID to user + if [[ "$(sudo dscl . read /Users/$USERNAME UniqueID 2>&1)" == *'No such key'* ]]; then + MAX_ID_USER="$(dscl . -list /Users UniqueID | awk '{print $2}' | sort -ug | tail -1)" + USER_ID="$((MAX_ID_USER+1))" + sudo dscl . create /Users/$USERNAME UniqueID "$USERID" + fi + + ### Add user to group + sudo dseditgroup -o edit -t user -a $USERNAME $GROUP +else + logg warn 'The USER environment variable is unavailable' fi - -### Add user -sudo dscl . create /Users/$USERNAME - -### Add PrimaryGroupID to user -if [[ "$(sudo dscl . read /Users/$USERNAME PrimaryGroupID 2>&1)" == *'No such key'* ]]; then - sudo dscl . create /Users/$USERNAME PrimaryGroupID 20 -fi - -### Add UniqueID to user -if [[ "$(sudo dscl . read /Users/$USERNAME UniqueID 2>&1)" == *'No such key'* ]]; then - MAX_ID_USER="$(dscl . -list /Users UniqueID | awk '{print $2}' | sort -ug | tail -1)" - USER_ID="$((MAX_ID_USER+1))" - sudo dscl . create /Users/$USERNAME UniqueID "$USERID" -fi - -### Add user to group -sudo dseditgroup -o edit -t user -a $USERNAME $GROUP {{ end -}}