#!/bin/bash

PRG=$(basename $0)

ATTACH_MODE="-m BOTH"

BOLD='[1m'
RED='[0;31m'
GREEN='[0;32m'
YELLOW='[0;33m'
BLUE='[0;34m'
MAGENTA='[0;35m'
CYAN='[0;36m'
NC='[0m' # No Color
NL='
'

TTY=$(tty)

LOG () {
    echo "$1" >> "${TTY}"
}

usage () {
    echo "Usage: ${PRG} mbox"
    exit 1
}

[ "$#" -eq 1 ] || usage

mbox=$(realpath $1)


cd $(dirname $0)
DOMAINNAME="$(cat domainname)"
JIRAFEAU_URL="https://depot.${DOMAINNAME}"
JIRAFEAU_LOCAL="${JIRAFEAU_URL}"

cd ../..

mkdir -p tmp
rm -fr tmp/PJ-name.txt tmp/PJ-Keys.txt tmp/PJ tmp/archive-content.txt tmp/url-to-refresh.txt tmp/new-mbox
echo -e "time: $(date "+%Y-%m-%d-%H:%M:%S")\nid: $(date +%s)" > "tmp/archive-content.txt"

LOG "  - ${BLUE}mbox: ${mbox}${NC}"
build/out/eMailShrinker -l "${mbox}"
LOG

build/out/eMailShrinker -u "${mbox}" > "tmp/url-to-refresh.txt" 2>> "${TTY}"


cat "tmp/url-to-refresh.txt" | grep "${JIRAFEAU_URL}" | while read REMOTE_LINK; do
    REMOTE_REF=$(echo "${REMOTE_LINK}" | sed -e 's/.*h=\([^&]*\).*/\1/' -e 's/.*http.*//')
    [ -z "${REMOTE_REF}" ] && continue
    LOG "  - ${BLUE}update ${REMOTE_REF}${NC}"
    build/out/jirafeauAPI -f "${JIRAFEAU_LOCAL}" -t "month" update "${REMOTE_REF}" 2>> "${TTY}"
    LOG
    echo "old: ${REMOTE_REF} ${REMOTE_KEY}" >> "tmp/archive-content.txt"
done

build/out/eMailShrinker -s "5ki" -d tmp/PJ "${mbox}" > "tmp/PJ-name.txt"

LOG "  - ${BLUE}PJ-name: ${NC}"
cat tmp/PJ-name.txt
LOG

cat "tmp/PJ-name.txt" | {
    while read ATTACH_TMP_NAME; do
	LOG " - ${BLUE}find ATTACH_TMP_NAME: (${ATTACH_TMP_NAME}) ${NC}"
	if [ -d "${ATTACH_TMP_NAME}" ]; then
	    ATTACH_MEDIA="${ATTACH_TMP_NAME}/media"
	    ATTACH_NAME=$(grep "^Name: " "${ATTACH_TMP_NAME}/meta" | cut -c 7- )
	    ATTACH_CONTENT_TYPE=$(grep "^Content-Type: " "${ATTACH_TMP_NAME}/meta" | cut -c 15- )
	else
	    LOG " - ${RED}no ATTACH_TMP_NAME: ${ATTACH_TMP_NAME}${NC}"
	    # XXX error
	    continue
	fi
	LOG " - ${BLUE}find ${ATTACH_NAME} / (${ATTACH_CONTENT_TYPE}) / ${ATTACH_MEDIA} ${NC}"
	PASSWORD=$(apg -n 1 -m 12)
	PASSWORD_MD5=$(echo -n ${PASSWORD} | ${MD5_CMD} | cut -d \  -f 1)
	build/out/jirafeauAPI -f "${JIRAFEAU_LOCAL}" -t "month" -s "1Gi" -c "${ATTACH_CONTENT_TYPE}" -n "${ATTACH_NAME}" send "${ATTACH_MEDIA}" "${PASSWORD}"  2>> "${TTY}" > "tmp/one.txt"

	cat "tmp/one.txt"  | {
	    read JIR_TOKEN
	    read JIR_CODE
	    case "${JIR_TOKEN}" in
		"" | no | *Error* | \<* )
		    LOG "  - ${RED}can't upload ${ATTACH_MEDIA} <${JIR_TOKEN}> <${JIR_CODE}>${NC}"
		    cat "tmp/one.txt" >> "${TTY}"
		    echo "url:"
		    ;;
		* )
		    LOG "  - ${GREEN} upload ${ATTACH_MEDIA}${NC}"
		    echo "url: ${JIRAFEAU_URL}/f.php?d=1&h=${JIR_TOKEN}&k=${PASSWORD_MD5}"
		    echo "new: ${JIR_TOKEN} ${PASSWORD_MD5}" >> "tmp/archive-content.txt"
		    ;;
	    esac
	}
    done
    NB_ATTACH=$(grep -e "^old: " -e "^new: " "tmp/archive-content.txt" | wc -l)
    if [ "${NB_ATTACH}" -gt 1 ]; then
	PASSWORD=$(apg -n 1 -m 12)
	PASSWORD_MD5=$(echo -n ${PASSWORD} | ${MD5_CMD} | cut -d \  -f 1)
	build/out/jirafeauAPI -f "${JIRAFEAU_LOCAL}" -t "month" -s "1Gi" -c "text/kaz_email_archive" -n "archive_content" send "tmp/archive-content.txt" "${PASSWORD}" > "tmp/one.txt"  2>> "${TTY}"
	cat "tmp/one.txt"  | {
	    read JIR_TOKEN
	    read JIR_CODE
	    case "${JIR_TOKEN}" in
		"" | no | *Error* | \<* )
		    LOG "  - ${RED}can't upload tmp/archive-content.txt${NC}"
		    echo "arch: bad"
		    ;;
		* )
		    LOG "  - ${GREEN} upload archive-content.txt${NC}"
		    echo "arch: ${JIRAFEAU_URL}/a.php?g=${JIR_TOKEN}~${PASSWORD_MD5}"
		    ;;
	    esac
	}
    else
	LOG "  - ${GREEN} no archive${NC}"
	echo "arch: none"
    fi
} > "tmp/PJ-Keys.txt"

LOG "  - ${BLUE}PJ-Keys: ${NC}"
cat tmp/PJ-Keys.txt
LOG

LOG "  - ${GREEN}ATTACH_MODE: ${ATTACH_MODE}${NC}"

cat "tmp/PJ-Keys.txt" | build/out/eMailShrinker ${ATTACH_MODE} -s "5ki" "${mbox}" "tmp/new-mbox" 2>> "${TTY}"


LOG "  - ${BLUE}new-mbox:${NC}"
build/out/eMailShrinker -l "tmp/new-mbox" 2>> "${TTY}"
