@@ -6,13 +6,13 @@ PACKAGE_MANAGER=""
66DRY_RUN=false
77VERBOSE=false
88LOG_FILE=" $HOME /devsetup_$( date +%Y%m%d_%H%M%S) .log"
9- AUR_HELPER=" yay" # Default, can be overridden by config
9+ AUR_HELPER=" yay"
1010CONFIG_DIR=" $HOME /.config/devsetup"
1111CONFIG_FILE=" $CONFIG_DIR /config.sh"
1212INSTALLED_PACKAGES=()
1313export DIALOGRC=" /dev/null"
1414
15- [ -f " $CONFIG_FILE " ] && source " $CONFIG_FILE " # Load user config (e.g., AUR_HELPER)
15+ [ -f " $CONFIG_FILE " ] && source " $CONFIG_FILE "
1616
1717declare -A PKG_MAP=(
1818 [" git" ]=" git" [" curl" ]=" curl" [" wget" ]=" wget" [" htop" ]=" htop" [" neofetch" ]=" neofetch"
@@ -27,13 +27,13 @@ log() {
2727 level=" $1 "
2828 shift
2929 fi
30- echo " $( date ' +%Y-%m-%d %H:%M:%S' ) - [$level ] - $* " | tee -a " $LOG_FILE "
31- [ " $level " = " ERROR " ] && echo " $( date ' +%Y-%m-%d %H:%M:%S' ) - [$level ] - $* " >&2
30+ ( echo " $( date ' +%Y-%m-%d %H:%M:%S' ) - [$level ] - $* " | tee -a " $LOG_FILE " > /dev/null 2>&1 ) || true
31+ echo " $( date ' +%Y-%m-%d %H:%M:%S' ) - [$level ] - $* "
3232}
3333
3434debug () { [ " $VERBOSE " = true ] && log " DEBUG" " $@ " ; }
3535
36- command_exists () { command -v " $1 " > /dev/null 2>&1 ; }
36+ command_exists () { command -v " $1 " > /dev/null 2>&1 || return 1 ; }
3737
3838package_installed () {
3939 case " $PACKAGE_MANAGER " in
@@ -47,17 +47,26 @@ package_installed() {
4747}
4848
4949check_root () {
50- if [[ " $( id -u) " -eq 0 ]]; then
51- log " WARNING" " Running as root is not recommended."
52- ask_confirmation " Continue anyway?" " N" || exit 1
53- fi
50+ if [ -f /.dockerenv ]; then
51+ return 0 # Skip the check in Docker
52+ fi
53+
54+ if [ " $EUID " -eq 0 ]; then
55+ echo " [WARNING] Running as root is not recommended."
56+ read -rp " Continue? (y/N): " response
57+ if [[ ! " $response " =~ ^[Yy]$ ]]; then
58+ echo " [INFO] Exiting due to user choice."
59+ exit 1
60+ fi
61+ fi
5462}
5563
5664ask_confirmation () {
5765 local prompt=" $1 "
5866 local default=" ${2:- N} "
67+ local confirmation=" "
5968 read -t 10 -p " $prompt (y/N, timeout 10s): " confirmation || {
60- log " Timeout reached, defaulting to No."
69+ log " Timeout reached or no input , defaulting to No."
6170 return 1
6271 }
6372 case " $confirmation " in
@@ -141,22 +150,90 @@ ensure_aur_helper() {
141150
142151# --- Distribution Detection ---
143152detect_distribution () {
153+
154+ if [ " $DRY_RUN " = true ]; then
155+ log " [Dry Run] Detecting Linux distribution"
156+ fi
157+
144158 if [ -e /etc/os-release ]; then
145- . /etc/os-release
146- debug " Loaded /etc/os-release: ID=$ID , PRETTY_NAME=$PRETTY_NAME "
159+ . /etc/os-release # Ensure we source the file in the current shell
160+ log " Sourced /etc/os-release: ID=$ID , VERSION_ID=$VERSION_ID , PRETTY_NAME='$PRETTY_NAME '"
161+
162+ # Additional debug info
163+ if [ " $VERBOSE " = true ]; then
164+ debug " Full os-release contents:"
165+ debug " $( cat /etc/os-release) "
166+ fi
167+
168+ local original_package_manager=" $PACKAGE_MANAGER "
169+ local detected_id=" $ID "
170+
171+ # For handling ID_LIKE if ID is not recognized
172+ if [ -n " $ID_LIKE " ]; then
173+ log " Distribution has ID_LIKE=$ID_LIKE "
174+ fi
175+
147176 case " $ID " in
148- ubuntu|debian|linuxmint|pop) PACKAGE_MANAGER=" apt-get" ;;
149- fedora|centos|rhel|rocky|alma) PACKAGE_MANAGER=" dnf" ;;
150- arch|manjaro|endeavouros) PACKAGE_MANAGER=" pacman" ;;
151- opensuse* ) PACKAGE_MANAGER=" zypper" ;;
152- gentoo) PACKAGE_MANAGER=" emerge" ;;
153- * ) log " ERROR" " Unsupported distribution: $ID " ; exit 1 ;;
177+ ubuntu|debian|linuxmint|pop)
178+ PACKAGE_MANAGER=" apt-get"
179+ log " Detected Debian-based distribution: $ID " ;;
180+ fedora|centos|rhel|rocky|alma)
181+ PACKAGE_MANAGER=" dnf"
182+ log " Detected Red Hat-based distribution: $ID " ;;
183+ arch|endeavouros)
184+ PACKAGE_MANAGER=" pacman"
185+ log " Detected Arch-based distribution: $ID " ;;
186+ manjaro)
187+ PACKAGE_MANAGER=" pacman"
188+ log " Detected Manjaro Linux (Arch-based): $ID " ;;
189+ opensuse* )
190+ PACKAGE_MANAGER=" zypper"
191+ log " Detected openSUSE distribution: $ID " ;;
192+ * )
193+ if [ -n " $ID_LIKE " ]; then
194+ log " Unrecognized ID=$ID , trying to match based on ID_LIKE=$ID_LIKE "
195+ # Try to determine package manager from ID_LIKE
196+ if [[ " $ID_LIKE " == * " arch" * ]]; then
197+ PACKAGE_MANAGER=" pacman"
198+ log " Matched Arch-like distribution from ID_LIKE"
199+ elif [[ " $ID_LIKE " == * " debian" * ]]; then
200+ PACKAGE_MANAGER=" apt-get"
201+ log " Matched Debian-like distribution from ID_LIKE"
202+ elif [[ " $ID_LIKE " == * " fedora" * || " $ID_LIKE " == * " rhel" * ]]; then
203+ PACKAGE_MANAGER=" dnf"
204+ log " Matched Red Hat-like distribution from ID_LIKE"
205+ elif [[ " $ID_LIKE " == * " suse" * ]]; then
206+ PACKAGE_MANAGER=" zypper"
207+ log " Matched SUSE-like distribution from ID_LIKE"
208+ else
209+ log " ERROR" " Unsupported distribution: $ID (ID_LIKE=$ID_LIKE )"
210+ exit 1
211+ fi
212+ else
213+ log " ERROR" " Unsupported distribution: $ID "
214+ exit 1
215+ fi
216+ ;;
154217 esac
155- log " Detected $PRETTY_NAME with $PACKAGE_MANAGER "
218+
219+ if [ " $DRY_RUN " = true ]; then
220+ log " [Dry Run] Would use $PACKAGE_MANAGER for package management on $PRETTY_NAME "
221+ fi
222+
223+ # Check if PACKAGE_MANAGER is actually set
224+ if [[ -z " $PACKAGE_MANAGER " ]]; then
225+ log " ERROR" " PACKAGE_MANAGER is empty after detection. Exiting."
226+ exit 1
227+ fi
156228 else
157- log " ERROR" " /etc/os-release not found."
229+ if [ " $DRY_RUN " = true ]; then
230+ log " [Dry Run] Would fail: /etc/os-release not found"
231+ fi
232+ log " ERROR" " /etc/os-release not found. Cannot determine distribution."
158233 exit 1
159234 fi
235+
236+ log " Exiting detect_distribution"
160237}
161238
162239# --- Rollback ---
@@ -213,10 +290,21 @@ install_code_editors() {
213290
214291select_categories () {
215292 if ! command_exists dialog; then
216- log " ERROR" " Dialog is required but missing. Falling back to default installation."
217- install_defaults
218- exit 1
293+ if [ " $DRY_RUN " = true ]; then
294+ log " [Dry Run] Would install dialog and show menu (simulating essentials selection: git, curl, vscode)"
295+ log " [Dry Run] Would install: git"
296+ log " [Dry Run] Would install: curl"
297+ log " [Dry Run] Would install: vscode_${PACKAGE_MANAGER} "
298+ SELECTED_ESSENTIALS=(" git" " curl" )
299+ SELECTED_EDITORS=(" vscode_${PACKAGE_MANAGER} " )
300+ return 0
301+ else
302+ log " ERROR" " Dialog is required but missing. Falling back to default installation."
303+ install_defaults
304+ exit 1
305+ fi
219306 fi
307+
220308 dialog --menu " Select installation categories:" 15 50 5 \
221309 1 " Essential Tools" \
222310 2 " Code Editors" \
@@ -254,9 +342,12 @@ while getopts "ndvh" opt; do
254342 echo " -h Help: Show this message"
255343 exit 0
256344 ;;
345+ ? ) log " ERROR" " Invalid option: -$OPTARG " ; exit 1 ;;
257346 esac
258347done
348+ shift $(( OPTIND - 1 ))
259349
350+ log " Finished parsing options, proceeding with setup."
260351check_root
261352detect_distribution
262353if ! command_exists dialog; then
0 commit comments