2019-04-09 17:11:22 +00:00
|
|
|
#!/usr/bin/env bash
|
2019-01-25 17:28:29 +00:00
|
|
|
#
|
|
|
|
# Be careful what you are doing with this script. You may overwrite files.
|
|
|
|
# cd into the repository before executing the script!
|
|
|
|
set -e;
|
|
|
|
|
|
|
|
usage() {
|
2022-09-08 18:02:00 +00:00
|
|
|
echo "Script to synchronize dotfiles-system skel files";
|
2019-01-25 17:28:29 +00:00
|
|
|
echo "";
|
2022-09-08 18:02:00 +00:00
|
|
|
echo "(1) /etc/skel --> ~/... ALL users (with given shell)";
|
2019-01-25 17:28:29 +00:00
|
|
|
echo "";
|
2022-09-08 18:02:00 +00:00
|
|
|
echo "(2) /etc/skel --> ~/specific user";
|
2019-01-25 17:28:29 +00:00
|
|
|
echo "";
|
2022-09-08 18:02:00 +00:00
|
|
|
echo "(3) list line all users with given shell, e.g. zsh";
|
2019-01-25 17:28:29 +00:00
|
|
|
echo "";
|
2022-09-08 18:02:00 +00:00
|
|
|
echo "(0) or [CTRL+C] to exit";
|
2019-01-25 17:28:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
menu() {
|
|
|
|
usage;
|
|
|
|
read -n 1 -e -p ">" menuInput
|
|
|
|
echo "";
|
|
|
|
|
|
|
|
if [ "$menuInput" = "1" ]; then
|
|
|
|
# require root
|
|
|
|
[[ $UID -eq 0 ]] || (>&2 echo "ERROR: need to be root!" && exit 1)
|
|
|
|
|
|
|
|
# determine shell
|
|
|
|
echo "Which shell (shortname, e.g. zsh)?";
|
|
|
|
read -e -p ">" shellInput
|
|
|
|
echo "";
|
|
|
|
|
|
|
|
# shell users
|
|
|
|
users=$(cat /etc/passwd|grep $shellInput| awk -F':' '{ print $1 }');
|
|
|
|
|
|
|
|
# stage
|
|
|
|
tmpDir=/tmp/dotfiles-system-sync/;
|
|
|
|
mkdir -p $tmpDir;
|
2022-09-08 23:03:08 +00:00
|
|
|
srcDir=/etc/skel/.
|
2019-01-25 17:28:29 +00:00
|
|
|
cp -R $srcDir $tmpDir;
|
|
|
|
|
|
|
|
# copy
|
|
|
|
for user in $users; do
|
|
|
|
exists=$(id -u $user);
|
|
|
|
primaryGroup=$(id -gn $user);
|
|
|
|
chown -R $user:$primaryGroup $tmpDir;
|
|
|
|
trgDir=$(eval echo ~$user/);
|
|
|
|
echo "Copy from $tmpDir to $trgDir";
|
|
|
|
cp -Rp $tmpDir/. $trgDir/;
|
|
|
|
done
|
|
|
|
|
|
|
|
# remove stage
|
|
|
|
rm -rf $tmpDir;
|
2022-09-08 18:02:00 +00:00
|
|
|
elif [ "$menuInput" = "2" ]; then
|
2019-01-25 17:28:29 +00:00
|
|
|
# determine shell
|
|
|
|
echo "Which user?";
|
|
|
|
read -e -p ">" user
|
|
|
|
echo "";
|
|
|
|
|
|
|
|
# stage
|
|
|
|
tmpDir=/tmp/dotfiles-system-sync/;
|
|
|
|
mkdir -p $tmpDir;
|
2022-09-08 23:03:08 +00:00
|
|
|
srcDir=/etc/skel/.
|
2019-01-25 17:28:29 +00:00
|
|
|
cp -R $srcDir $tmpDir;
|
|
|
|
|
|
|
|
# copy
|
|
|
|
exists=$(id -u $user);
|
|
|
|
|
|
|
|
if [ $UID -ne $exists ] && [ $UID -ne 0 ]; then
|
|
|
|
echo "ERROR: need to be $user or root!";
|
|
|
|
exit 1;
|
|
|
|
fi
|
|
|
|
|
|
|
|
primaryGroup=$(id -gn $user);
|
|
|
|
chown -R $user:$primaryGroup $tmpDir;
|
|
|
|
trgDir=$(eval echo ~$user/);
|
|
|
|
echo "Copy from $tmpDir to $trgDir";
|
|
|
|
cp -Rp $tmpDir/. $trgDir/;
|
|
|
|
|
|
|
|
# remove stage
|
|
|
|
rm -rf $tmpDir;
|
|
|
|
|
2022-09-08 18:02:00 +00:00
|
|
|
elif [ "$menuInput" = "3" ]; then
|
2019-01-25 17:28:29 +00:00
|
|
|
echo "Which shell (shortname, e.g. zsh)?";
|
|
|
|
read -e -p ">" shellInput
|
|
|
|
echo "";
|
|
|
|
|
|
|
|
# shell users
|
|
|
|
users=$(cat /etc/passwd|grep $shellInput| awk -F':' '{ print $1 }');
|
|
|
|
echo "Users with shell $shellInput"
|
|
|
|
|
|
|
|
for u in $users; do
|
|
|
|
echo $u;
|
|
|
|
done
|
2019-01-25 17:36:42 +00:00
|
|
|
elif [ "$menuInput" = "0" ]; then
|
2019-01-25 17:28:29 +00:00
|
|
|
exit 0;
|
|
|
|
fi
|
|
|
|
|
|
|
|
menu;
|
|
|
|
}
|
|
|
|
|
|
|
|
menu;
|