#!/usr/bin/bash
# -*- sh; sh-shell: bash; -*-

# Script to import archive with contact data:
# - contacts_linphone.tar.xz
# - linphone contacts in vCard and CSV format
# - avatars used

# Script Requires: bash tar xz sed sqlite3 regexp.so
# regexp.so for linux x86 is inside archive https://github.com/nalgeon/sqlean/releases/download/0.21.8/sqlean-linux-x86.zip
# https://hakanu.net/sqlite/2021/04/10/how-to-achieve-regular-expression-based-replace-in-sqlite-regex/
# https://github.com/nalgeon/sqlean/blob/main/docs/regexp.md

# Needs contacts_linphone.tar.xz file as argument
# Tested with linphone v5.2.0

if [ $# -eq 0 ]; then
    echo "Searching for possible contacts_linphone.tar.xz files you may want to import..."
    echo
    find . -maxdepth 2 -type f -name "contacts_linphone*.tar.xz"
    find .. -maxdepth 1 -type f -name "contacts_linphone*.tar.xz"
    #DIR=$(xdg-user-dir DOWNLOAD)
    find "$(xdg-user-dir DOWNLOAD)" -maxdepth 2 -name "contacts_linphone*.tar.xz" -type f
    find "$(xdg-user-dir DOCUMENTS)" -maxdepth 2 -name "contacts_linphone*.tar.xz" -type f
    echo
    echo "Usage:"
    #echo "  $(basename "$0") contacts_linphone.tar.xz"
    echo "  ${0} contacts_linphone.tar.xz"
    echo
    echo "Please specify contacts_linphone.tar.xz file path to import into linphone."
    exit 1
fi

SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
source $SCRIPT_DIR/linphone-contacts-library

linphone-contacts-import-archive-tar-xz "$1"

exit 0
