From dfecbd5b588c1fbdaacf3df8a0ab93a668c282d2 Mon Sep 17 00:00:00 2001
From: flu0r1ne <flu0r1ne@flu0r1ne.net>
Date: Sun, 22 Aug 2021 23:11:03 -0500
Subject: handle --dir with --init-with

---
 git-update-agent | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)

diff --git a/git-update-agent b/git-update-agent
index 6c205a7..e932afe 100644
--- a/git-update-agent
+++ b/git-update-agent
@@ -169,9 +169,10 @@ init_if_empty() {
     if [[ -z "$REMOTE_URL" ]]; then 
       die_no_git_dir
     else
-      init_repo "$REMOTE_URL"
-    fi
+      init_repo "$1" "$REMOTE_URL"
 
+      echo "INITIALIZED"
+    fi
   fi
 }
 
@@ -195,7 +196,7 @@ cat <<_EOF
 Usage: ${PROGRAM} [OPTIONS]...
   -h, --help         output this help info
   -d, --dir          update in directory
-  --init             initialize a project with the given URL
+  --init-with        initialize a project with the given URL
   --track            track a git repository not initialized with $PROGRAM
   --disable-tracking disable tracking
 _EOF
@@ -229,12 +230,12 @@ update() {
   local next_version
 
   dir="$1"
-  cd_or_die "$dir"
 
+  mkbase_or_die "$dir"
+  cd_or_die "$dir"
+  
   previous_version="$(current_version)"
 
-  init_if_empty
-
   if [[ "$(should_track)" != "true" ]]; then
     die_not_tracking     
   fi
@@ -266,14 +267,27 @@ update() {
   fi
 }
 
+mkbase_or_die() {
+  if [[ ! -d "$1" ]] && ! mkdir "$1"; then
+    die <<_EOF
+Cannot create $1 directory
+_EOF
+  fi
+}
 
 init_repo() {
+  local dir
   local remote
   local remote_url
   local upstream_branch
   local remote_ref
 
-  remote_url="$1"
+  dir="$1"
+
+  mkbase_or_die "$dir"
+  cd_or_die "$dir"
+
+  remote_url="$2"
   remote="$(remote)"
 
   DEFAULT_BRANCH="main"
@@ -359,7 +373,7 @@ git_update_agent() {
   fi
 
   if [[ -n "$remote_url" ]]; then
-    init_repo "$remote_url"
+    init_repo "$dir" "$remote_url"
     exit 0
   fi
 
-- 
cgit v1.2.3