modified makefile to produce same home dir in sudo mode

This commit is contained in:
asus
2023-09-12 10:31:45 +02:00
parent 0c8d52ca2f
commit 9b9c3c1357
2 changed files with 17 additions and 3 deletions

View File

@@ -24,12 +24,26 @@ RESET = "\e[0m"
COMPOSE = ./srcs/docker-compose.yml
# in makefile you can use an env variable directly as a make variable :
# -> https://ftp.gnu.org/old-gnu/Manuals/make-3.79.1/html_chapter/make_6.html#SEC68
# so if you want to get the home directory you can use $(HOME)
# however, this will not give the same result if you run make in sudo, ex :
# make : /home/asususus
# sudo make : /root
# but you can use this command `eval echo "~$SUDO_USER"` to get
# the home directory of the user using sudo, it works in non-sudo also :
# - $SUDO_USER :
# - in normal mode its value is ""
# - in sudo mode its value is the user (same as $USER in normal mode)
# - ~$SUDO_USER :
# - in linux "~USER" print the home directory of a user
USER_HOME := $(shell echo ~$$SUDO_USER)
# extract env variables in .env file
# then expend the home path
# then expend the pwd path
# and finally remove the leading "EXPEND_" word
EXPENDED_ENV_VAR := $(shell grep "^#EXPEND_" ./srcs/.env)
EXPENDED_ENV_VAR := $(subst $$HOME_PATH,$(HOME),$(EXPENDED_ENV_VAR))
EXPENDED_ENV_VAR := $(subst $$HOME_PATH,$(USER_HOME),$(EXPENDED_ENV_VAR))
EXPENDED_ENV_VAR := $(subst $$PWD_PATH,$(shell pwd),$(EXPENDED_ENV_VAR))
EXPENDED_ENV_VAR := $(EXPENDED_ENV_VAR:#EXPEND_%=%)