From fa30d22dc7f3b7c6442e3a7dab4b30eaa507ec4f Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Wed, 2 Dec 2020 15:07:17 +0100
Subject: [PATCH] Can now install using the stpkg command

---
 install.sh |  14 ++----
 stpkg      | 128 +++++++++++++++++++++++++++++++++++------------------
 2 files changed, 87 insertions(+), 55 deletions(-)

diff --git a/install.sh b/install.sh
index 5dc52eb..8d08083 100755
--- a/install.sh
+++ b/install.sh
@@ -8,17 +8,9 @@ die() {
     exit 1
 }
 
-error() {
-    echo -ne '\e[31m'$*'\e[39m\n'
-}
-
-warn() {
-    echo -ne '\e[33m'$*'\e[39m\n'
-}
-
-info() {
-    echo -ne '\e[32m'$*'\e[39m\n'
-}
+error() { echo -ne '\e[31m'$*'\e[39m\n'; }
+warn()  { echo -ne '\e[33m'$*'\e[39m\n'; }
+info()  { echo -ne '\e[32m'$*'\e[39m\n'; }
 
 # Clone the stickerpicker into the html repository
 cd ~/html >/dev/null 2>&1 \
diff --git a/stpkg b/stpkg
index bf3c44a..9d974d7 100755
--- a/stpkg
+++ b/stpkg
@@ -29,10 +29,14 @@ __exit() {
     cd "$BASE_DIR"
     exit $1
 }
+__EXIT() { cd "$BASE_DIR"; }
+trap __exit EXIT
+alias exit=__exit
 
 usage() {
 cat << EOF
 $0 usage:
+    $0 --install [install path]
     $0 update
     $0 list [-p -e -np] [bash regex]
     $0 add <pack>
@@ -44,70 +48,114 @@ Flags:
     -np     The pack is not present in the sticker repo
 
 ENV variables:
-    STPKG_INSTALL   [path]       The cloned stickerpicker project location. Defaut is ~/html/stickerpicker.
+    STPKG_INSTALL   [path]       The cloned stickerpicker project location. [~/html/stickerpicker]
     STPKG_BASE      [path]       Where the sticker project is located.
-    STPKG_COLORS    ['yes'|'no'] Use colors for output. Default is 'yes'.
-    STPKG_NO_MSG    ['yes'|'no'] Disable messages, no 'info', 'warn' or 'error'. Default is 'no'.
-
-    The STPKG_INSTALL STPKG_BASE should have been given by the 'install.sh' script.
+    STPKG_COLORS    ['yes'|'no'] Use colors for output. ['yes']
+    STPKG_NO_MSG    ['yes'|'no'] Disable messages, no 'info', 'warn' or 'error'. ['no']
+
+Notes:
+    The '$0 --install [path]' must be called only once. The default install
+    path is ~/html/stickerpicker. This command will give you a STPKG_INSTALL
+    and a STPKG_BASE env that you must put in your bashrc for the rest of the
+    stpkg commands to work.
 EOF
 
     if [ "x$DISPLAY_DEPS_NOTES" = "xyes" ]; then
 cat << EOF
-Note: $0 requires 'jq' and 'egrep'. If 'egrep' is not found, 'grep' will be used.
-Note: 'git' is obviously required, but if you are using this repo you should have clone it so it's ok.
+
+Requirements:
+    The stpkg command requires 'jq' and 'egrep'. If 'egrep' is not found,
+    'grep' will be used. 'git' is obviously required, but if you are using this
+    repo you should have clone it so it's ok.
 EOF
     fi
 
-    __exit 0
+    exit 0
 }
 
 die() {
     error $*
-    __exit 1
+    exit 1
+}
+
+require() {
+    local __cmd=`which $1`
+    [ $? -ne 0 ] && die "Failed to find the '$1' command"
+    alias $1=$__cmd
+}
+
+require git
+require sponge
+require ls
+require find
+require jq
+
+################################
+# The special install function #
+################################
+
+__install() {
+    # Set install dir
+    local STPKG_INSTALL=$1
+    STPKG_INSTALL=${STPKG_INSTALL:="~/html/stickerpicker/"}
+    [ -d "$STPKG_INSTALL" ] \
+        && die "Folder '$STPKG_INSTALL' already exists." \
+               "If you already installed the sticker picker you can use '$0 update'"
+
+    # Clone and copy packs
+    info "Will install sticker picker in folder: $STPKG_INSTALL"
+    git clone https://github.com/maunium/stickerpicker.git "$STPKG_INSTALL" \
+        || die "Failed to git clone the stickerpicker project."
+
+    cd $STPKG_INSTALL/   || die "Failed to cd to '$STPKG_INSTALL/'"
+    cp packs/* web/packs || die "Failed to copy default packs to '$STPKG_INSTALL/web/packs/'"
+
+    jq -n "{ \"homeserver_url\": \"https://matrix.iiens.net\",
+             \"packs\": [
+                    \"scalar-privacy_pam.json\"
+           ] }" > $STPKG_INSTALL/web/packs/index.json || die "Failed to create default index.json file"
+
+    # Needed ENV for future use
+    local STPKG_BASE=$BASE_DIR
+    info 'Installation done successfully, you may copy the "stpkg" file into your path or create an alias.'
+    info 'You may add the following lines in your bashrc file:'
+    echo "export STPKG_INSTALL='$STPKG_INSTALL'"
+    echo "export STPKG_BASE='$STPKG_BASE'"
 }
+if [ "x$1" = "x--install" ]; then
+    [ "x$STPKG_NO_MSG" = "xyes" ] \
+        && echo "!!! STPKG_NO_MSG is turn on, you won't see what's going on during the install"
+    shift
+    REPO_DIR="$(dirname $(readlink -f "$0"))"
+    [ ! "$REPO_DIR" = "$BASE_DIR" ] && die "You must run this script from the root of the sticker repo"
+    __install $*
+    exit 0
+fi
 
 #########################
 # Get some exec / paths #
 #########################
 
-if [ "x$STPKG_INSTALL" = "x" ]; then
-    STPKG_INSTALL="~/html/stickerpicker/"
-    [ ! -d "$STPKG_INSTALL" ] \
-        && die "STPKG_INSTALL not specified but the default one ($STPKG_INSTALL) doesn't exist." \
-               "Did you use the install script?"
-else
-    info "Use custom STPKG_INSTALL: $STPKG_INSTALL"
+STPKG_INSTALL=${STPKG_INSTALL:="~/html/stickerpicker/"}
+if [ ! -d "$STPKG_INSTALL" ]; then
+    error "STPKG_INSTALL folder ($STPKG_INSTALL) doesn't exist. Did you use the install script?"
+    usage
 fi
+info "Use STPKG_INSTALL: $STPKG_INSTALL"
 
 if [ "x$STPKG_BASE" = "x" ]; then
     die "The STPKG_BASE env var is mendatory, it must point to the sticker repo." \
         "It should have been given by the install script"
 fi
 
-__git=`which git`
-[ $? -ne 0 ] && die "Failed to find the 'git' command, WTF?"
-__sponge=`which sponge`
-[ $? -ne 0 ] && die "Failed to find the 'sponge' command"
-__ls=`which ls`
-[ $? -ne 0 ] && die "Failed to find the 'ls' command"
-__find=`which find`
-[ $? -ne 0 ] && die "Failed to find the 'find' command"
-__jq=`which jq`
-[ $? -ne 0 ] && die "Failed to find the 'jq' command"
 __grep=`which egrep`
 if [ $? -ne 0 ]; then
     __grep=`which grep`
     [ $? -ne 0 ] && die "Failed to find 'grep' or 'egrep'"
     warn "Cound not find 'grep', will use 'egrep 'instead"
 fi
-DISPLAY_DEPS_NOTES="no"
-alias jq=$__jq
 alias grep=$__grep
-alias git=$__git
-alias sponge=$__sponge
-alias find=$__find
-alias ls=$__ls
+DISPLAY_DEPS_NOTES="no"
 
 #########################
 # Cmd handler functions #
@@ -159,13 +207,7 @@ __list() {
         && die "The -p and -np flags are incompatible"
 
     # Get the regex
-    if [ "x$*" = "x" ]; then
-        REG=
-        info "List packs with no regex"
-    else
-        REG="$*"
-        info "List packs with regex: $REG"
-    fi
+    REG="$*"
 
     # A sticker pack can be:
     # - enabled and present in the picker folder
@@ -184,13 +226,13 @@ __list() {
         local PRESENT=`find $STPKG_BASE/packs/ -type f -name "$LINE.json"`
         [ "x$FILTER_PRESENT"     = "xyes" -a   "x$PRESENT" = "x" ] && continue
         [ "x$FILTER_NOT_PRESENT" = "xyes" -a ! "x$PRESENT" = "x" ] && continue
-        PRESENT=`[ ! "x$PRESENT" = "x" ] && echo '[present]' || echo '         '`
+        PRESENT=`[ ! "x$PRESENT" = "x" ] && echo 'p' || echo 'a'`
 
         local ENABLED=`jq -r ".packs | index( \"$LINE.json\") | ." < $STPKG_INSTALL/web/packs/index.json`
         [ "x$FILTER_ENABLED" = "xyes" -a "x$ENABLED" = "xnull" ] && continue
-        ENABLED=`[ "x$ENABLED" = "xnull" ] && echo '[disabled]' || echo '[enabled ]'`
+        ENABLED=`[ "x$ENABLED" = "xnull" ] && echo 'd' || echo 'e'`
 
-        echo -ne "$PRESENT $ENABLED $LINE\n"
+        echo -ne "$PRESENT$ENABLED $LINE\n"
     done
 }
 
@@ -253,5 +295,3 @@ case "$1" in
         usage
         ;;
 esac
-
-__exit 0
-- 
GitLab