Skip to content

Commit

Permalink
Use getopts
Browse files Browse the repository at this point in the history
  • Loading branch information
scaryrawr committed Dec 12, 2024
1 parent e9af739 commit b7119f4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
12 changes: 11 additions & 1 deletion setup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ param(
# Name to give the new user
[string]
$UserName,
# Password for the new user
[securestring]
$Password,
# Name to register as
[string]
$DistroName = 'Fedora',
Expand Down Expand Up @@ -32,7 +35,14 @@ $nixPath = wsl -e wslpath "$scriptDir"
$language = $(Get-WinSystemLocale).Name.Split('-')[0]

# Launch to configure
wsl -d "$DistroName" "$nixPath/setup.sh" $UserName $language
$arguments = @()
if ($UserName) { $arguments += "-u `"$UserName`"" }
if ($language) { $arguments += "-l `"$language`"" }
if ($Password) { $arguments += "-p `"$Password`"" }

$argumentsString = $arguments -join ' '

wsl -d "$DistroName" "$nixPath/setup.sh" $argumentsString

# Set default user as first non-root user
Get-ItemProperty Registry::HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss\*\ DistributionName | Where-Object -Property DistributionName -eq "$DistroName" | Set-ItemProperty -Name DefaultUid -Value 1000
Expand Down
19 changes: 15 additions & 4 deletions setup.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
#!/usr/bin/env sh

username=$1
language=$2
while getopts u:p:l: flag
do
case "${flag}" in
u) username=${OPTARG};;
p) password=${OPTARG};;
l) language=${OPTARG};;
esac
done

if [ -z "$username" ]; then
echo "Please enter a new user name: "
read username
fi

useradd -m -G adm,wheel,dialout,cdrom,floppy,audio,video $username
passwd $username

if [ -z "$language" ]; then
if [ -z "$password" ]; then
passwd $username
else
echo "$username:$password" | chpasswd
fi

if [ -n "$language" ]; then
sudo dnf install -y "glibc-langpack-$language"
fi

0 comments on commit b7119f4

Please sign in to comment.