#!/usr/bin/with-contenv bash
# shellcheck shell=bash

# create symlinks for imagemagick policy.xml
rm -rf /etc/ImageMagick-6/policy.xml
ln -s /defaults/policy.xml /etc/ImageMagick-6/policy.xml

# create Google drive client_secrets.json file
if [[ ! -f /config/client_secrets.json ]]; then
    echo "{}" > /config/client_secrets.json
fi

# check if kepubify is present and if so make executable
if [[ -f /usr/bin/kepubify ]] && [[ ! -x /usr/bin/kepubify ]]; then
    chmod +x /usr/bin/kepubify
fi

# Pre-stage some files & directories for permissions purposes
mkdir -p /app/calibre-web/cps/cache

# permissions
lsiown -R abc:abc \
    /config \
    /app/calibre-web/cps/cache

export CALIBRE_DBPATH=/config

if [[ ! -f /config/app.db ]]; then
  echo "First time run, creating app.db..."
  cd /app/calibre-web && s6-setuidgid abc python3 /app/calibre-web/cps.py -d > /dev/null 2>&1
  # handle app.db updates for kepubify and ext binary path
  # borrowed from crocodilestick because it's a better solution than what i was doing
  sqlite3 /config/app.db <<EOS
    update settings set config_kepubifypath='/usr/bin/kepubify' where config_kepubifypath is NULL or LENGTH(config_kepubifypath)=0;
EOS

  if [[ $? == 0 ]]; then
    echo "Successfully set kepubify paths in '/config/app.db'!"
  elif [[ $? > 0 ]]; then
    echo "Could not set binary paths for '/config/app.db' (see errors above)."
  fi
fi
