#!/bin/sh

# This file is part of the element-web-patches.
#
# element-web-patches is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License,
# or (at your option) any later version.
#
# element-web-patches is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with element-web-patches. If not, see <https://www.gnu.org/licenses/>.

echo 'Backing up compact.json...'
if [ ! -f ${1}/node_modules/emojibase-data/en/compact_backup.json ]; then
    cp ${1}/node_modules/emojibase-data/en/compact.json ${1}/node_modules/emojibase-data/en/compact_backup.json
else
    cp ${1}/node_modules/emojibase-data/en/compact_backup.json ${1}/node_modules/emojibase-data/en/compact.json
fi

echo 'Adding custom emoji...'
{
    ./reactions/generate.sh > "./reactions/data-injects~.json";
    jq -sc '.[0] + .[1]' "${1}/node_modules/emojibase-data/en/compact_backup.json" "./reactions/data-injects~.json" > "${1}/node_modules/emojibase-data/en/compact.json";
    rm "./reactions/data-injects~.json";
} || { echo "Failed to inject reactions: Exit code $?" 1>&2; exit $?; }


echo 'Backing up _EmojiPicker.pcss...'
if [ ! -f "${1}"/res/css/views/emojipicker/_EmojiPicker.pcss.backup ]; then
    cp "${1}"/res/css/views/emojipicker/_EmojiPicker.pcss "${1}"/res/css/views/emojipicker/_EmojiPicker.pcss.backup
else
    cp "${1}"/res/css/views/emojipicker/_EmojiPicker.pcss.backup "${1}"/res/css/views/emojipicker/_EmojiPicker.pcss
fi

echo 'Adding custom emoji picker CSS...'
emoji_picker_css_code=$(cat << EOF
.mx_EmojiPicker_item_wrapper {
    text-overflow: clip;
    white-space: nowrap;
    overflow: hidden;
}
EOF
)

echo "${emoji_picker_css_code}" >> "${1}"/res/css/views/emojipicker/_EmojiPicker.pcss