From 289a6ea30f3d34a72f539d72f17f10f9d14d637b Mon Sep 17 00:00:00 2001
From: Nick Sweeting <git@nicksweeting.com>
Date: Wed, 17 Apr 2019 05:42:09 -0400
Subject: [PATCH] fix database file location and init process

---
 archivebox/core/settings.py | 11 +++++---
 archivebox/legacy/config.py |  4 +--
 archivebox/legacy/logs.py   |  2 +-
 archivebox/legacy/main.py   | 52 +++++++++++++++++++++----------------
 4 files changed, 41 insertions(+), 28 deletions(-)

diff --git a/archivebox/core/settings.py b/archivebox/core/settings.py
index b168e6e2..ff1fbe67 100644
--- a/archivebox/core/settings.py
+++ b/archivebox/core/settings.py
@@ -5,6 +5,11 @@ import os
 SECRET_KEY = '---------------- not a valid secret key ! ----------------'
 DEBUG = True
 
+OUTPUT_DIR = os.path.abspath(os.curdir)
+DATABASE_DIR_NAME = 'database'
+DATABASE_FILE_NAME = 'database.sqlite3'
+DATABASE_FILE = os.path.join(OUTPUT_DIR, DATABASE_DIR_NAME, DATABASE_FILE_NAME)
+
 
 INSTALLED_APPS = [
     'django.contrib.admin',
@@ -15,7 +20,7 @@ INSTALLED_APPS = [
     'django.contrib.staticfiles',
 
     'core',
-    
+
     'django_extensions',
 ]
 
@@ -51,7 +56,7 @@ WSGI_APPLICATION = 'core.wsgi.application'
 DATABASES = {
     'default': {
         'ENGINE': 'django.db.backends.sqlite3',
-        'NAME': os.path.join(os.path.abspath(os.curdir), 'database', 'database.sqlite3'),
+        'NAME': DATABASE_FILE,
     }
 }
 
@@ -67,7 +72,7 @@ LANGUAGE_CODE = 'en-us'
 TIME_ZONE = 'UTC'
 USE_I18N = True
 USE_L10N = True
-USE_TZ = True
+USE_TZ = False
 
 
 STATIC_URL = '/static/'
diff --git a/archivebox/legacy/config.py b/archivebox/legacy/config.py
index 8842b793..64c4ce87 100644
--- a/archivebox/legacy/config.py
+++ b/archivebox/legacy/config.py
@@ -98,7 +98,7 @@ DATABASE_FILE_NAME = 'database.sqlite3'
 ARCHIVE_DIR = os.path.join(OUTPUT_DIR, ARCHIVE_DIR_NAME)
 SOURCES_DIR = os.path.join(OUTPUT_DIR, SOURCES_DIR_NAME)
 DATABASE_DIR = os.path.join(OUTPUT_DIR, DATABASE_DIR_NAME)
-DATABASE_FILE = os.path.join(DATABASE_DIR, DATABASE_FILE_NAME)
+DATABASE_FILE = os.path.join(OUTPUT_DIR, DATABASE_DIR_NAME, DATABASE_FILE_NAME)
 
 PYTHON_DIR = os.path.join(REPO_DIR, 'archivebox')
 LEGACY_DIR = os.path.join(PYTHON_DIR, 'legacy')
@@ -346,7 +346,7 @@ try:
         'DATABASE_DIR': {
             'path': os.path.abspath(DATABASE_DIR),
             'enabled': True,
-            'is_valid': os.path.exists(os.path.join(DATABASE_DIR, DATABASE_FILE)),
+            'is_valid': os.path.exists(DATABASE_FILE),
         },
         'CHROME_USER_DATA_DIR': {
             'path': CHROME_USER_DATA_DIR and os.path.abspath(CHROME_USER_DATA_DIR),
diff --git a/archivebox/legacy/logs.py b/archivebox/legacy/logs.py
index 8b0dda9f..0f3eb5dc 100644
--- a/archivebox/legacy/logs.py
+++ b/archivebox/legacy/logs.py
@@ -71,7 +71,7 @@ def log_indexing_started(out_dir: str, out_file: str):
 def log_indexing_finished(out_dir: str, out_file: str):
     end_ts = datetime.now()
     _LAST_RUN_STATS.index_end_ts = end_ts
-    print('\r    √ {}/{}'.format(pretty_path(out_dir), out_file))
+    print('\r    √ {}/{}'.format(out_dir, out_file))
 
 
 ### Archiving Stage
diff --git a/archivebox/legacy/main.py b/archivebox/legacy/main.py
index 72e949ad..0dd4ffd6 100644
--- a/archivebox/legacy/main.py
+++ b/archivebox/legacy/main.py
@@ -20,6 +20,7 @@ from .config import (
     SOURCES_DIR,
     ARCHIVE_DIR,
     DATABASE_DIR,
+    DATABASE_FILE,
     check_dependencies,
     check_data_folder,
     setup_django,
@@ -39,21 +40,19 @@ from .logs import (
 def init():
     os.makedirs(OUTPUT_DIR, exist_ok=True)
 
-    harmless_files = {'.DS_Store', '.venv', 'venv', 'virtualenv', '.virtualenv'}
+    harmless_files = {'.DS_Store', '.venv', 'venv', 'virtualenv', '.virtualenv', 'sources', 'archive', 'database', 'logs', 'static'}
     is_empty = not len(set(os.listdir(OUTPUT_DIR)) - harmless_files)
     existing_index = os.path.exists(os.path.join(OUTPUT_DIR, 'index.json'))
 
-    if not is_empty:
+    if is_empty:
+        stderr('{green}[+] Initializing new archive directory: {}{reset}'.format(OUTPUT_DIR, **ANSI))
+        write_main_index([], out_dir=OUTPUT_DIR, finished=True)
+    else:
         if existing_index:
-            stderr('{green}[√] You already have an archive index in: {}{reset}'.format(OUTPUT_DIR, **ANSI))
-            stderr('    To add new links, you can run:')
-            stderr("        archivebox add 'https://example.com'")
-            stderr()
-            stderr('    For more usage and examples, run:')
-            stderr('        archivebox help')
-            # TODO: import old archivebox version's archive data folder
-
-            raise SystemExit(1)
+            stderr('{green}[√] You already have an ArchiveBox collection in the current folder.{reset}'.format(**ANSI))
+            stderr(f'    {OUTPUT_DIR}')
+            stderr(f'    > index.html')
+            stderr(f'    > index.json')
         else:
             stderr(
                 ("{red}[X] This folder already has files in it. You must run init inside a completely empty directory.{reset}"
@@ -65,23 +64,32 @@ def init():
             )
             raise SystemExit(1)
 
-
-    stderr('{green}[+] Initializing new archive directory: {}{reset}'.format(OUTPUT_DIR, **ANSI))
-    os.makedirs(SOURCES_DIR)
-    stderr(f'    > {SOURCES_DIR}')
-    os.makedirs(ARCHIVE_DIR)
-    stderr(f'    > {ARCHIVE_DIR}')
-    os.makedirs(DATABASE_DIR)
-    stderr(f'    > {DATABASE_DIR}')
-
-    write_main_index([], out_dir=OUTPUT_DIR, finished=True)
+    os.makedirs(SOURCES_DIR, exist_ok=True)
+    stderr(f'    > sources/')
+    os.makedirs(ARCHIVE_DIR, exist_ok=True)
+    stderr(f'    > archive/')
+    os.makedirs(DATABASE_DIR, exist_ok=True)
 
     setup_django()
     from django.core.management import call_command
+    from django.contrib.auth.models import User
+    stderr(f'    > database/')
+    
+    stderr('\n{green}[+] Running Django migrations...{reset}'.format(**ANSI))
     call_command("makemigrations", interactive=False)
     call_command("migrate", interactive=False)
+    
+    if not User.objects.filter(is_superuser=True).exists():
+        stderr('{green}[+] Creating admin user account...{reset}'.format(**ANSI))
+        call_command("createsuperuser", interactive=True)
 
-    stderr('{green}[√] Done.{reset}'.format(**ANSI))
+    stderr('\n{green}------------------------------------------------------------{reset}'.format(**ANSI))
+    stderr('{green}[√] Done. ArchiveBox collection is set up in current folder.{reset}'.format(**ANSI))
+    stderr('    To add new links, you can run:')
+    stderr("        archivebox add 'https://example.com'")
+    stderr()
+    stderr('    For more usage and examples, run:')
+    stderr('        archivebox help')