#!/bin/sh

# No copyright is claimed.  This code is in the public domain; do
# with it what you wish.  Written by Joost van Baal-Ilić in 2017.

# update-publicfile-contenttype - generate a CT-style envdir from
#  /etc/mime.types formatted input

set -e

comment='# generated by update-publicfile-contenttype'

if test -d "$1"
then
    dir="$1"
else
    echo error: first argument "$1" is not a directory, exiting
    exit 1
fi

while read mimetype ext exts
do
    test "$mimetype" = '#' && continue
    test -z "$mimetype" && continue
    test -z "$ext" && continue

    for e in "$ext" $exts
    do
            cat <<EOT >"$dir/CT_$e"
$mimetype
$comment
EOT
    done
done

# add symlinks
# root@perun:/etc/publicfile/mime# ln -s CT_jpg CT_JPG
# CT_[a-z0-9] CT_[!A-Z]

# tr from GNU coreutils
ls /etc/publicfile/mime | while read f
do
    F="$(echo $f | tr '[:lower:]' '[:upper:]')"
    test ! -e "$F" && test "$f" != "$F" && cp -a "$f" "$F"
done
