11#! /bin/bash
22ROS_DISTRO=jazzy
33
4-
54set -e
6- mkdir -p deps
75
8- cd deps
6+ # Set default UID and GID if not provided
7+ USER_ID=${UID:- 1000}
8+ GROUP_ID=${GID:- 1000}
9+ USERNAME=${UNAME:- ubuntu}
10+ GROUPNAME=${UNAME:- ubuntu}
11+ user_removed=false
12+
13+
14+ # Check if the user already exists
15+ EXISTING_USERNAME=$( getent passwd " $USER_ID " | cut -d: -f1)
16+
17+ # If the user exists and the username is different, remove the existing user (we need to free the UID)
18+ if [ -n " $EXISTING_USERNAME " ] && [ " $EXISTING_USERNAME " != " $USERNAME " ]; then
19+ echo " Removing user $EXISTING_USERNAME "
20+ deluser --remove-home $EXISTING_USERNAME
21+ user_removed=true
22+ fi
23+
24+ # The development folder has shared in the docker-compose file as volume
25+ # It is not a user folder, so we need to add the initial files like .bashrc and .profile
26+ if [ " $user_removed " = true ]; then
27+ cp -r /etc/skel/. /home/$USERNAME / 2> /dev/null
28+ fi
929
10- vcs import . < /deps.repos
11- vcs pull .
30+ # Change the ownership of the home directory to the specified user and group
31+ chown $USER_ID : $GROUP_ID /home/ $USERNAME /
1232
33+ # ## Create group if it doesn't exist
34+ if ! getent group $GROUP_ID > /dev/null; then
35+ groupadd -g $GROUP_ID $USERNAME
36+ fi
37+
38+ # Now, we can create the user with the specified UID and GID
39+ if ! id -u $USER_ID > /dev/null 2>&1 ; then
40+ echo " Creating user $USERNAME with UID $USER_ID and GID $GROUP_ID "
41+ useradd -m -u $USER_ID -g $GROUP_ID -s /bin/bash $USERNAME
42+ fi
43+
44+ # If not present, request to load the ROS environment in the .bashrc file
1345ROS_SETUP=" source /opt/ros/${ROS_DISTRO} /setup.bash"
14- if ! grep -Fxq " $ROS_SETUP " ~ /.bashrc; then
46+ if ! grep -Fxq " $ROS_SETUP " /home/ $USERNAME /.bashrc; then
1547 echo " $ROS_SETUP " >> ~ /.bashrc
1648fi
17- #
18- OVERLAY_SETUP=" source /home/ubuntu/eut_bt_ros2_ws/install/setup.bash"
19- if ! grep -Fxq " $OVERLAY_SETUP " ~ /.bashrc; then
20- echo " $OVERLAY_SETUP " >> ~ /.bashrc
49+
50+ # If not present, request to load the workspace environment in the .bashrc file
51+ OVERLAY_SETUP=" source /home/${USERNAME} /eut_bt_ros2_ws/install/setup.bash"
52+ if ! grep -Fxq " $OVERLAY_SETUP " /home/$USERNAME /.bashrc; then
53+ echo " $OVERLAY_SETUP " >> /home/$USERNAME /.bashrc
2154fi
22- #
55+
56+ # If not present, request to load the colcon_argcomplete environment in the .bashrc file
2357AUTOCOMPLETE_SETUP=" source /usr/share/colcon_argcomplete/hook/colcon-argcomplete.bash"
24- if ! grep -Fxq " $AUTOCOMPLETE_SETUP " ~ /.bashrc; then
25- echo " $AUTOCOMPLETE_SETUP " >> ~ /.bashrc
58+ if ! grep -Fxq " $AUTOCOMPLETE_SETUP " /home/ $USERNAME /.bashrc; then
59+ echo " $AUTOCOMPLETE_SETUP " >> /home/ $USERNAME /.bashrc
2660fi
2761
28- WORKING_DIR=" cd /home/ubuntu/eut_bt_ros2_ws"
29- if ! grep -Fxq " $WORKING_DIR " ~ /.bashrc; then
30- echo " $WORKING_DIR " >> ~ /.bashrc
31- fi
3262
33- cd ../../
63+ # WORKING_DIR="cd /home/${USERNAME}/eut_bt_ros2_ws"
64+ # if ! grep -Fxq "$WORKING_DIR" /home/$USERNAME/.bashrc; then
65+ # echo "$WORKING_DIR" >> /home/$USERNAME/.bashrc
66+ # fi
67+
68+
69+ # Set the ownership of the home directory and recurisvely the volumes set in the docker-compose file
70+ chown $USERNAME :$GROUPNAME /home/$USERNAME /* -R
71+
72+
73+ # Switch to the new user and run the main command
74+ exec gosu " $USERNAME " bash -c "
75+ echo 'Running as $USERNAME ...';
76+
77+ # Load the environment variables
78+ source /home/$USERNAME /.bashrc;
79+
80+ # Open the workspace and join the dependencies folder after created
81+ cd /home/$USERNAME /eut_bt_ros2_ws/src/;
82+ mkdir deps;
83+ cd deps;
3484
35- source /opt/ros/ ${ROS_DISTRO} /setup.bash
36- colcon build --symlink-install
37- source ~ /.bashrc
85+ # Download the dependencies from the repository
86+ vcs import . < /deps.repos;
87+ vcs pull .;
3888
39- echo " SYSTEM READY"
40- exec " $@ "
89+ # Compile the workspace
90+ cd ../../;
91+ source /opt/ros/${ROS_DISTRO} /setup.bash;
92+ colcon build --symlink-install;
93+
94+ exec \" $@ \" ;
95+ "
0 commit comments