From 742c217fd05945e45bb94d867371543162ab1ec9 Mon Sep 17 00:00:00 2001
From: Recolic Keghart <root@recolic.net>
Date: Thu, 9 May 2019 01:58:26 -0700
Subject: [PATCH] working

---
 create_secret_file.sh              | 11 +++++++++--
 on_gnome_start.sh                  | 29 +++++++++++++++++++++++++++++
 src/unlock_keyrings.cc             |  6 +++---
 unlock_keyring_from_secret_file.sh | 29 +++++++++++++++++++++++++++++
 4 files changed, 70 insertions(+), 5 deletions(-)
 create mode 100644 on_gnome_start.sh
 create mode 100755 unlock_keyring_from_secret_file.sh

diff --git a/create_secret_file.sh b/create_secret_file.sh
index ca22cb4..07ff605 100755
--- a/create_secret_file.sh
+++ b/create_secret_file.sh
@@ -3,6 +3,8 @@
 filename="$1"
 gpg_pubkey_id="$2" # leave empty to use default receipt.
 
+[[ "$filename" = '' ]] && echo "Usage: $0 <filename> [gpg_pubkey_id]" && exit 1
+
 echo '>>> Please type keyring_name and password in the following format:
 
 keyring1:password1
@@ -13,5 +15,10 @@ login:12345678
 
 >>> When you are done, use Ctrl-D to end.'
 
-gpg --encrypt -o "$filename" -a -r "$gpg_pubkey_id"
-exit $?
+if [[ "$gpg_pubkey_id" = '' ]]; then
+    gpg --encrypt -o "$filename" -a
+    exit $?
+else
+    gpg --encrypt -o "$filename" -a -r "$gpg_pubkey_id"
+    exit $?
+fi
diff --git a/on_gnome_start.sh b/on_gnome_start.sh
new file mode 100644
index 0000000..c72cdce
--- /dev/null
+++ b/on_gnome_start.sh
@@ -0,0 +1,29 @@
+#!/bin/bash
+# This script should be run after gnome being started.
+
+_my_path="$0"
+secret_file="$1"
+
+[[ "$secret_file" = '' ]] && echo "Usage: $0 <secret_file>" && exit 1
+
+function where_is_him () {
+    SOURCE="$1"
+    while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
+        DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
+        SOURCE="$(readlink "$SOURCE")"
+        [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
+    done
+    DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
+    echo -n "$DIR"
+ }
+
+function where_am_i () {
+    where_is_him "$_my_path"
+}
+
+cd `where_am_i` &&
+gpg --decrypt "$secret_file" | bin/unlock_keyrings --secret-file - # --quiet
+
+exit $?
+
+
diff --git a/src/unlock_keyrings.cc b/src/unlock_keyrings.cc
index 3425f6c..8e829ae 100644
--- a/src/unlock_keyrings.cc
+++ b/src/unlock_keyrings.cc
@@ -3,17 +3,17 @@
 #include <rlib/stream.hpp>
 #include "keyring_op.hpp"
 
+rlib::logger rlog(std::cerr);
 
 int main(int argc, char **argv) {
     rlib::opt_parser args(argc, argv);
-    rlib::logger rlog(std::cout);
 
     if(args.getBoolArg("-h", "--help")) {
         rlog.info("Usage: {} [-h/--help] [-q/--quiet] --secret-file <filename> # use `-` as stdin.");
         return 0;
     }
     if(args.getBoolArg("-q", "--quiet")) {
-        rlog = rlib::logger(rlib::null_stream);
+        rlog.set_log_level(rlib::log_level_t::FATAL);
     }
 
     auto secret_file_name = args.getValueArg("--secret-file");
@@ -43,7 +43,7 @@ int main(int argc, char **argv) {
         auto res = do_unlock(keyring_and_pswd.at(0), keyring_and_pswd.at(1));
         auto msg = keyringResultToString(res);
         if(res == GNOME_KEYRING_RESULT_OK)
-            rlog.verbose("line {}: {}.", line_num, msg);
+            rlog.info("line {}: {}.", line_num, msg);
         else {
             rlog.error("line {}: {}.", line_num, msg);
             no_error = false;
diff --git a/unlock_keyring_from_secret_file.sh b/unlock_keyring_from_secret_file.sh
new file mode 100755
index 0000000..43e3071
--- /dev/null
+++ b/unlock_keyring_from_secret_file.sh
@@ -0,0 +1,29 @@
+#!/bin/bash
+# This script should be run after gnome being started.
+
+_my_path="$0"
+secret_file="$1"
+
+[[ "$secret_file" = '' ]] && echo "Usage: $0 <secret_file>" && exit 1
+
+function where_is_him () {
+    SOURCE="$1"
+    while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
+        DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
+        SOURCE="$(readlink "$SOURCE")"
+        [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
+    done
+    DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
+    echo -n "$DIR"
+ }
+
+function where_am_i () {
+    where_is_him "$_my_path"
+}
+
+cd `where_am_i` &&
+gpg --decrypt "$secret_file" | bin/unlock_keyrings --secret-file - --quiet
+
+exit $?
+
+
-- 
GitLab