summaryrefslogtreecommitdiff
path: root/kogaion-functions.sh
blob: 586c02d1f4cdd751ef6c964f64cdceb9ef70d915 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/bin/bash

GDM_FILE="/usr/share/gdm/defaults.conf"
CUSTOM_GDM_FILE="/etc/gdm/custom.conf"
KDM_FILE="/usr/share/config/kdm/kdmrc"
LXDM_FILE="/etc/lxdm/lxdm.conf"
LIGHTDM_FILE="/etc/lightdm/lightdm.conf"
OEM_FILE="/etc/oemlive.sh"
OEM_FILE_NEW="/etc/oem/liveboot.sh"
LIVE_USER_GROUPS="audio bumblebee cdrom cdrw clamav console entropy games \
kvm lp lpadmin messagebus plugdev polkituser portage pulse pulse-access pulse-rt \
scanner usb users uucp vboxguest vboxusers video wheel"
LIVE_USER=${ROGENTOS_USER:-kogaionuser}

kogaion_setup_autologin() {
	# GDM - GNOME
	if [ -f "${GDM_FILE}" ]; then
		sed -i "s/^AutomaticLoginEnable=.*/AutomaticLoginEnable=true/" ${GDM_FILE}
		sed -i "s/^AutomaticLogin=.*/AutomaticLogin=${LIVE_USER}/" ${GDM_FILE}

		sed -i "s/^TimedLoginEnable=.*/TimedLoginEnable=true/" ${GDM_FILE}
		sed -i "s/^TimedLogin=.*/TimedLogin=${LIVE_USER}/" ${GDM_FILE}
		sed -i "s/^TimedLoginDelay=.*/TimedLoginDelay=0/" ${GDM_FILE}

	elif [ -f "${CUSTOM_GDM_FILE}" ]; then
		# FIXME: if this is called multiple times, it generates duplicated entries
		sed -i "s:\[daemon\]:\[daemon\]\nAutomaticLoginEnable=true\nAutomaticLogin=${LIVE_USER}\nTimedLoginEnable=true\nTimedLogin=${LIVE_USER}\nTimedLoginDelay=0:" \
			"${CUSTOM_GDM_FILE}"
		# change other entries there
		sed -i "s/^TimedLogin=.*/TimedLogin=${LIVE_USER}/" "${CUSTOM_GDM_FILE}"
		sed -i "s/^AutomaticLogin=.*/AutomaticLogin=${LIVE_USER}/" "${CUSTOM_GDM_FILE}"
	fi

	# KDM - KDE
	if [ -f "$KDM_FILE" ]; then
		sed -i "s/AutoLoginEnable=.*/AutoLoginEnable=true/" $KDM_FILE
		sed -i "s/AutoLoginUser=.*/AutoLoginUser=${LIVE_USER}/" $KDM_FILE
		sed -i "s/AutoLoginDelay=.*/AutoLoginDelay=0/" $KDM_FILE
		sed -i "s/AutoLoginAgain=.*/AutoLoginAgain=true/" $KDM_FILE

		sed -i "s/AllowRootLogin=.*/AllowRootLogin=true/" $KDM_FILE
		sed -i "s/AllowNullPasswd=.*/AllowNullPasswd=true/" $KDM_FILE
		sed -i "s/AllowShutdown=.*/AllowShutdown=All/" $KDM_FILE

		sed -i "/^#.*AutoLoginEnable=/ s/^#//" $KDM_FILE
		sed -i "/^#.*AutoLoginUser=/ s/^#//" $KDM_FILE
		sed -i "/^#.*AutoLoginDelay=/ s/^#//" $KDM_FILE
		sed -i "/^#.*AutoLoginAgain=/ s/^#//" $KDM_FILE

		sed -i "/^#AllowRootLogin=/ s/^#//" $KDM_FILE
		sed -i "/^#AllowNullPasswd=/ s/^#//" $KDM_FILE
		sed -i "/^#AllowShutdown=/ s/^#//" $KDM_FILE
	fi

	# LXDM
	if [ -f "$LXDM_FILE" ]; then
		sed -i "s/autologin=.*/autologin=${LIVE_USER}/" $LXDM_FILE
		sed -i "/^#.*autologin=/ s/^#//" $LXDM_FILE
	fi

	# LightDM
	if [ -f "$LIGHTDM_FILE" ]; then
		sed -i "s/autologin-user=.*/autologin-user=${LIVE_USER}/" $LIGHTDM_FILE
		sed -i "/^#.*autologin-user=/ s/^#//" $LIGHTDM_FILE
	fi

	# Setup correct login session
	kogaion_is_normal_boot && kogaion_fixup_gnome_autologin_session
}

kogaion_disable_autologin() {
	# GDM - GNOME
	if [ -f "${GDM_FILE}" ]; then
		sed -i "s/^AutomaticLoginEnable=.*/AutomaticLoginEnable=false/" ${GDM_FILE}
	fi

	# KDM - KDE
	KDM_FILE="/usr/share/config/kdm/kdmrc"
	if [ -f "$KDM_FILE" ]; then
		sed -i "s/AutoLoginEnable=.*/AutoLoginEnable=false/" $KDM_FILE
	fi

	# LXDM
	if [ -f "$LXDM_FILE" ]; then
		sed -i "s/^autologin=.*/autologin=/" $LXDM_FILE
	fi

	# LightDM
	if [ -f "$LIGHTDM_FILE" ]; then
		sed -i "s/^autologin-user=.*/#autologin-user=/" $LIGHTDM_FILE
	fi
}

kogaion_setup_live_user() {
	local live_user="${1}"
	local live_uid="${2}"
	if [ -z "${live_user}" ]; then
		live_user="${LIVE_USER}"
	fi
	if [ -n "${live_uid}" ]; then
		live_uid="-u ${live_uid}"
	fi
	id ${live_user} &> /dev/null
	if [ "${?}" != "0" ]; then
		local live_groups=""
		local avail_groups=$(cat /etc/group | cut -d":" -f 1 | xargs echo)
		for a_group in ${avail_groups}; do
			for p_group in ${LIVE_USER_GROUPS}; do
				if [ "${p_group}" = "${a_group}" ]; then
					if [ -z "${live_groups}" ]; then
						live_groups="${p_group}"
					else
						live_groups="${live_groups},${p_group}"
					fi
				fi
			done
		done
		# then setup live user, that is missing
		useradd -d "/home/${live_user}" -g root -G ${live_groups} -c "kogaionuser" \
			-m -N -p "" -s /bin/bash ${live_uid} "${live_user}"
		return 0
	fi
	return 1
}

kogaion_setup_motd() {
	echo -e "\n\tWelcome to `cat /etc/kogaion-edition`\n\t`uname -p`\n\t`uname -o` `uname -r`\n" > /etc/motd
}

kogaion_setup_vt_autologin() {
	if systemd_running; then
		cp /usr/lib/systemd/system/getty@.service \
			/etc/systemd/system/autologin@.service
		sed -i "/^ExecStart=/ s:/sbin/agetty:/sbin/agetty --autologin root:g" \
			/usr/lib/systemd/system/getty@.service
		sed -i "/^ExecStart=/ s:--noclear::g" \
			/usr/lib/systemd/system/getty@.service
		systemctl daemon-reload
		systemctl restart getty@tty1
	fi
}

kogaion_setup_oem_livecd() {
	if [ -x "${OEM_LIVE_NEW}" ]; then
		${OEM_FILE_NEW} || return 1
	elif [ -x "${OEM_LIVE}" ]; then
		${OEM_FILE} || return 1
	fi
	return 0
}

kogaion_is_live() {
	local cmdl=$(cat /proc/cmdline | grep cdroot)
	if [ -n "${cmdl}" ]; then
		return 0
	else
		return 1
	fi
}

kogaion_setup_gui_installer() {
	# Configure Fluxbox
	local dmrc_file="/home/${LIVE_USER}/.dmrc"
	local flux_dir="/home/${LIVE_USER}/.fluxbox"
	local flux_startup_file="${flux_dir}/startup"
	if [ ! -d "${flux_dir}" ]; then
		mkdir "${flux_dir}" && chown "${LIVE_USER}" "${flux_dir}"
	fi
	echo "[Desktop]" > "${dmrc_file}"
	echo "Session=fluxbox" >> "${dmrc_file}"
	chown kogaionuser "${dmrc_file}"
	sed -i "/installer --fullscreen/ s/^# //" "${flux_startup_file}"
	if [ -x "/usr/libexec/gdm-set-default-session" ]; then
		# oh my fucking glorious god, this
		# is AccountsService bullshit
		# cross fingers
		/usr/libexec/gdm-set-default-session fluxbox
	fi
	if [ -x "/usr/libexec/gdm-set-session" ]; then
		# GDM 3.6 support
		/usr/libexec/gdm-set-session kogaionuser fluxbox
	fi
}

# This function reads /etc/skel/.dmrc and properly
# set the Session= value inside AccountsService.
# Blame the idiots who broke de-facto standards
# and created this fugly thing called AccountsService
kogaion_fixup_gnome_autologin_session() {
	local cur_session=

	if [ -f "/etc/skel/.dmrc" ]; then
		cur_session=$(cat /etc/skel/.dmrc | grep ^Session | cut -d"=" -f 2)
	fi
	if [ -z "${cur_session}" ]; then
		return 0
	fi

	local sess_file="/usr/share/xsessions/${cur_session}.desktop"
	if [ ! -f "${sess_file}" ]; then
		return 0
	fi

	if [ -x "/usr/libexec/gdm-set-default-session" ]; then
		# this edits /etc/gdm/custom.conf adding [daemon]\nDefaultSession=${cur_session}
		/usr/libexec/gdm-set-default-session "${cur_session}"
	fi

	if [ -x "/usr/libexec/gdm-set-session" ]; then
		# GDM 3.6 support
		local users_in_users=$(cat /etc/group | grep "^users" | awk -F':' '{ print $4 }' | sed "s:,: :g")
		for usr in ${users_in_users}; do
			/usr/libexec/gdm-set-session "${usr}" "${cur_session}"
		done
	fi
}

kogaion_setup_text_installer() {
	kogaion_setup_text_installer_motd
}

kogaion_setup_text_installer_motd() {
	echo "Welcome to Kogaion Linux Text installation." >> /etc/motd
	echo "to run the installation type: installer <and PRESS ENTER>" >> /etc/motd
}

kogaion_is_text_install() {
	local _is_install=$(cat /proc/cmdline | grep installer-text)
	if [ -n "${_is_install}" ]; then
		return 0
	else
		return 1
	fi
}

kogaion_is_gui_install() {
	local _is_install=$(cat /proc/cmdline | grep installer-gui)
	if [ -n "${_is_install}" ]; then
		return 0
	else
		return 1
	fi
}

kogaion_is_live_install() {
	( kogaion_is_text_install || kogaion_is_gui_install ) && return 0
	return 1
}

kogaion_is_mce() {
	local _is_mce=$(cat /proc/cmdline | grep kogaionmce)
	if [ -n "${_is_mce}" ]; then
		return 0
	else
		return 1
	fi
}

kogaion_is_normal_boot() {
	if ! kogaion_is_mce && ! kogaion_is_live_install; then
		return 0
	else
		return 1
	fi
}

systemd_running() {
	test -d /run/systemd/system
}