OSDN Git Service

[update] : Added autologin
authorhayao <hayao@fascode.net>
Wed, 3 Feb 2021 13:07:13 +0000 (22:07 +0900)
committerhayao <hayao@fascode.net>
Wed, 3 Feb 2021 13:07:13 +0000 (22:07 +0900)
lightdm-config

index 2d8dbfc..7951b8e 100755 (executable)
@@ -34,15 +34,16 @@ script_usage(){
     echo "A simple tool for switching LightDM Greeters"
     echo
     echo " General command:"
-    echo "    greeter-change [greeter]     Specify the greeter to use"
-    echo "    greeter-edit [greeter]       Edit the greeter configs"
-    echo "    greeter-list                 Shows a list of currently installed Greeters"
-    echo "    remove                       Removes all changes made by this command"
-    echo "    edit                         Edit lightdm config"
+    echo "    autologin [username]           Set up automatic login (with blank username to disable)"
+    echo "    greeter-change [greeter]       Specify the greeter to use"
+    echo "    greeter-edit [greeter]         Edit the greeter configs"
+    echo "    greeter-list                   Shows a list of currently installed Greeters"
+    echo "    remove                         Removes all changes made by this command"
+    echo "    edit                           Edit lightdm config"
     echo
     echo " General option:"
-    echo "    -e | --editer                Specifies the editor to use for editing"
-    echo "    -h | --help                  This help message and exit"
+    echo "    -e | --editer                  Specifies the editor to use for editing"
+    echo "    -h | --help                    This help message and exit"
     echo "    --comment-replace"
 }
 
@@ -74,12 +75,12 @@ set_config(){
     local key="${1}" value="${2}"
     if [[ "${COMMENT_REPLACE}" = true ]]; then
         if cat "${LIGHTDM_CONFIG}" | grep -E "^#? ?${key} ?=" > /dev/null; then
-            sed -i -r "s|^# ?${key} ?=.+|${key}=${value}|g" "${LIGHTDM_CONFIG}" "${LIGHTDM_CONFIG}"
+            sed -i -r "s|^# ?${key} ?=.+|${key}=${value}|g" "${LIGHTDM_CONFIG}"
         fi
     fi
 
     if cat "${LIGHTDM_CONFIG}" | grep -E "^ ?${key} ?=" > /dev/null; then
-        sed -i -r "s|^ ?${key} ?=.+|${key}=${value}|g" "${LIGHTDM_CONFIG}" "${LIGHTDM_CONFIG}"
+        sed -i -r "s|^ ?${key} ?=.+|${key}=${value}|g" "${LIGHTDM_CONFIG}"
         return 0
     fi
 
@@ -87,6 +88,15 @@ set_config(){
     write_config "${1}=${2}"
 }
 
+# 設定ファイルのキーを削除する
+remove_key(){
+    local key="${1}" _config
+    if cat "${LIGHTDM_CONFIG}" | grep -E "^ ?${key}.+" > /dev/null; then
+        sed -i -r "s|^ ?${key} ?=.+||g" "${LIGHTDM_CONFIG}"
+        sed -i '/^$/d' "${LIGHTDM_CONFIG}"
+    fi
+}
+
 # 設定ファイルを作成
 init_configs(){
     if [[ ! -f "${LIGHTDM_CONFIG}" ]]; then
@@ -96,6 +106,18 @@ init_configs(){
     fi
 }
 
+# 現在設定されている値を取得する
+get_value(){
+    local _current_value="$(lightdm --show-config 2>&1 | grep -E "^[A-Z]  ${1}=" | sed "s|^[A-Z]  ||g" | cut -d "=" -f "2")"
+    if [[ "${_current_value}" ]]; then
+        echo -n "${_current_value}"
+        return 0
+    else
+        echo -n ""
+        return 0
+    fi
+}
+
 # greeter-changeコマンド
 run_greeter_change() {
     # 引数チェック
@@ -173,6 +195,51 @@ run_edit(){
     done
 }
 
+# autologin
+run_auto_login(){
+    if [[ -z "${1}" ]]; then
+        # 既に自動ログインが設定されているかを確認
+        local autologin_user="$(get_value autologin-user)"
+            if [[ -n "${autologin_user}" ]]; then
+            # autologinを無効化
+            for _autologin in "autologin-guest" "autologin-user" "autologin-user-timeout" "autologin-in-background" "autologin-session"; do
+                remove_key "${_autologin}"
+            done
+            echo "Canceled automatic login of ${autologin_user}"
+        fi
+    else
+        local autologin_session
+        local autologin_user="${1}"
+
+        if (( $(ls "/usr/share/xsessions" 2> /dev/null | wc -l) <= 1 )); then
+            autologin_session="$(ls "/usr/share/xsessions" | sed 's|.desktop$||g')"
+        else
+            echo "Select the desktop session to autologin"
+            local session
+            for session in "/usr/share/xsessions/"*; do
+                echo "   ${session}"
+            done
+            echo -n "(session name) > "
+            read session
+            if [[ -f "/usr/share/xsessions/${session}.desktop" ]]; then
+                autologin_session="${session}"
+            else
+                msg_error "Please enter the correct session name."
+                exit 1
+            fi
+        fi
+
+        set_config "autologin-guest" "false"
+        set_config "autologin-user" "${autologin_user}"
+        set_config "autologin-user-timeout" "0"
+        set_config "autologin-in-background" "false"
+        set_config "autologin-session" "${autologin_session}"
+
+        echo "${autologin_user} will automatically log in with ${autologin_session}"
+    fi
+
+}
+
 # 引数を解析
 ARGUMENT="${@}"
 OPTS="eh"
@@ -204,10 +271,18 @@ while true; do
     esac
 done
 
+if [[ -z "${1}" ]]; then
+    script_usage
+    exit 1
+fi
 COMMAND="${1}"
 shift 1
 
 case "${COMMAND}" in
+    "autologin")
+        init_configs
+        run_auto_login "${1}"
+        ;;
     "greeter-change")
         init_configs
         run_greeter_change "${1}"