#!/opt/zope213/bin/python

"""
ScoDoc post-upgrade script.

This script is launched by upgrade.sh after each SVN update.

Run as "root" with Zope shutted down and postgresql up,
_before_ upgrading the database.

E. Viennet, June 2008
Mar 2017: suppress upgrade of very old Apache configs
Aug 2020: move photos to .../var/scodoc/
"""
import os
import sys
import glob
import shutil 
from scodocutils import log, SCODOC_DIR, SCODOC_VAR_DIR, SCODOC_LOGOS_DIR

if os.getuid() != 0:
    log('postupgrade.py: must be run as root')
    sys.exit(1)

# ---
# Migrate photos (2020-08-16, svn 1908)
old_photo_dir = os.path.join(SCODOC_DIR, "static", "photos")
photo_dirs = glob.glob( old_photo_dir + "/F*")
if photo_dirs:
    log("Moving photos to new <var> directory...")
    shutil.move(old_photo_dir, SCODOC_VAR_DIR)

# Migrate depts (2020-08-17, svn 1909)

old_depts_dir = os.path.join(SCODOC_DIR, "config", "depts")
cfg_files = glob.glob( old_depts_dir + "/*.cfg")
depts_dir = os.path.join(SCODOC_VAR_DIR, "config/depts/")
for cfg in cfg_files:
    log("Moving %s to new <var> directory..." % cfg)
    shutil.move(cfg, depts_dir)

# Move logos
if not os.path.exists(SCODOC_LOGOS_DIR):
    old_logos = os.path.join(SCODOC_DIR,"logos")
    if os.path.exists(old_logos):
        log("Moving logos to new <var> directory...")
        dest = os.path.normpath(os.path.join(SCODOC_LOGOS_DIR, ".."))
        shutil.move(old_logos, dest)
    else:
        log("Warning: logos directory is missing (%s)" % SCODOC_LOGOS_DIR)

# Move dept-specific logos
for d in glob.glob( SCODOC_DIR + "/logos_*" ):
    log("Moving %s to %s" % (d, SCODOC_LOGOS_DIR))
    shutil.move(d, SCODOC_LOGOS_DIR)

# Continue here...

# ---
sys.exit(0)