#!/bin/sh

set -e

case "$1" in
install) ;;
upgrade)
  # Move conffiles from old location to new location
  if test -f /etc/im/imrc; then
    echo -n "Moving existing Imlib configuration files to /etc/imlib ... "
    test -d /etc/imlib || (mkdir /etc/imlib; chmod 755 /etc/imlib)
    sed -e 's,/etc/im/im_palette.pal,/etc/imlib/im_palette.pal,g' /etc/im/imrc > /etc/imlib/imrc
    rm -f /etc/im/imrc
    echo "done."

    test -f /etc/im/im_palette.pal && cp /etc/im/im_palette.pal /etc/imlib

    cat >&2 <<EOF

********************************************************
NOTE: The imlib system directory has been moved from
      /etc/im to /etc/imlib.
      Any existing ~/.imrc files may need to be updated
      to reflect the new location of the system palette
      file now found in /etc/imlib.
      Read the file README.Debian in
        /usr/share/doc/imlib-base
      for more information.
********************************************************
EOF

  elif test -f /etc/imlib/imrc && egrep '/etc/im/im_palette.pal' /etc/imlib/imrc 2>&1 > /dev/null; then
    sed -e 's,/etc/im/im_palette.pal,/etc/imlib/im_palette.pal,g' /etc/imlib/imrc > /etc/imlib/imrc.new
    mv /etc/imlib/imrc.new /etc/imlib/imrc
    echo "Corrected PaletteFile entry in \`/etc/imlib/imrc'."
  else
    echo "Checking for missing files in \`/etc/imlib' directory."

    test -d /etc/imlib || mkdir /etc/imlib

    if test ! -f /etc/imlib/imrc; then
      echo Recreating /etc/imlib/imrc
      # Recreate the Imlib initialization/configuration file `imrc'.
      cat > /etc/imlib/imrc <<EOF
################################
#    Config file for Imlib     #
################################

# The file that contains palette entries for a global palette for all Imlib
# based programs.
# options: full path to palette file
PaletteFile                       /etc/imlib/im_palette.pal
# This defines if when the display is greater than 8 bit, that it still remaps
# the images to the palette defined, rather than using "perfect" rendering
# options: yes/no
PaletteOverride                   no
# If remapping to the palette, whether to use Floyd-Steinberg dithering. Saying
# yes will slow things down though.
# options: yes/no
Dither                            yes
# when remapping to the palette, saying fast will reduce accuracy, but improve
# speed quite considerably
# options: fast/slow
Remap                             fast
# This turns on dithering for 15/16 bpp. This makes smooth gradients look much
# smoother - in fact almost perfect. You will find it nigh impossible to tell
# the difference between 15/16bpp dithered and 24bpp. Unless you have extra
# CPU to burn, its not recommended, unless you are a image quality freak, and
# you insist on maximum quality in 15/16bpp. It does slow things down. It
# would be best to leave it off and let the applications themselves allow
# you to select it for certain purposes only.
HighQuality                       off
# This option if specified off will force MIT-SHM off, otherwise will allow
# Imlib to work it out itself.
Mit-Shm                           on
# This will turn shared pixmaps on or off (off forces off, on lets imlib
# work it out). This is yet another speedup. leave it on unless it doesn't
# work.. then turn it off.
SharedPixmaps                     on
# This speeds up rendering considerably, but may not work on your hardware
# due to it bypassing a few layers and byte-twiddling the rendered image data
# manually, and due to endianess, bit-ordering or RGB ordering it may screw up
# and not work, so try it.. if things work great!, if not, wait until a
# renderer for your situation is written, or write one yourself and donate
# it. It's easy to do, just look at rend.c
FastRender                        on
# This is in fact a workaround due to Solaris's shared memory theories.
# This specifies the maximum size of a shared memory chunk in bytes. If an
# image is larger that this in bytes for the video mode you're in, imlib will
# not use MIT-SHM. if you comment this out, imlib will use as much memory as
# necessary to render the image.
# Shm_Max_Size                      1000000
# This turns Image loading (24) bit caching on or off. HIGHLY suggested to be
# turned ON!
Image_Cache                       on
# Image cache size in bytes.  As with any cache, the more, the better.  If you
# load the same image more than once. Imlib will used a previously loaded
# copy, and if its freed, the Image_Cache_Size amount of bytes of image data
# are kept even after being freed, in case the same image is loaded again soon
# afterwards. Neat eh?
Image_Cache_Size                  524288
# This turns the pixmap caching system on or off.  If on, only well-behaved
# programs that conform to the specs for using Imlib will exhibit the
# behavior as expected. It is suggested to leave this on, as it will boost
# performance considerably, speed-wise and memory-wise. The reason apps need
# to be well-behaved is so that they don't go drawing on, and XFreePixmap'ing
# these pixmaps themselves, because this will trample all over the cache
# and give very horrid effects, or even make the apps crash with segfaults or
# Xlib errors.
Pixmap_Cache                      on
# Pixmap cache is in **-> BITS <-**... the end result is APPROXIMATELY
# 10000000 bits of pixmap make your Xserver grow by 1Mb of RAM (VERY rough).
# As with any cache, the more, the better. The more you have, the less likely
# it is that you will get cache misses and so performance on scaling the same
# image to commonly used sizes (ie if 3 or 4 sizes of the same image are used)
# will be lightning fast, in fact in some tests I did, in 16bpp up to 38 times
# as fast, and in 8bpp (with dithering on) up to 105 times faster!!! (these
# are nominal figures obtained on my machine. these are MAXIMUM speedup
# results.  Results may vary on other machines and according to the way
# programs are written and use Imlib)
Pixmap_Cache_Size                 5242880
# This FORCES Imlib to use the hexadecimal visual id stated here if it is
# defined in the imrc. This bypasses Imlib's routines that hunt for the best
# visual. You can obtain a list of visual ID's using the xdpyinfo command.
# You should only need this if Imlib doesn't pick the correct visual or you
# have strange hardware/Xserver combinations.
#ForceVisualID                   22
# This allows Imlib to fall back on Imagemagick and/or NETPBM
# utilities if it can't load the file.
Fallback                         on
# Default Gamma, Brightness and Contrast stuff....
Gamma                            1.0
Brightness                       1.0
Contrast                         1.0
Red_Gamma                        1.0
Red_Brightness                   1.0
Red_Contrast                     1.0
Green_Gamma                      1.0
Green_Brightness                 1.0
Green_Contrast                   1.0
Blue_Gamma                       1.0
Blue_Brightness                  1.0
Blue_Contrast                    1.0
Ordered_Dither                    on
EOF
    fi

    if test ! -f /etc/imlib/im_palette.pal; then
      echo Recreating /etc/imlib/im_palette.pal
      # Recreate the primary palette file
      cat > /etc/imlib/im_palette.pal <<EOF
0x0 0x0 0x0
0xff 0xff 0xff
0xc0 0xc0 0xc0
0x0 0x0 0xff
0x99 0x0 0x66
0xff 0x0 0x0
0xff 0xff 0xcc
0x0 0xff 0x0
0x95 0x95 0x95
0x80 0x0 0x0
0x33 0x33 0x66
0x66 0x66 0xcc
0x80 0x80 0x80
0x99 0x99 0xff
0x0 0x0 0x80
0x22 0x22 0x22
0xff 0xff 0x0
0x80 0x80 0x0
0x0 0x80 0x80
0x42 0x9a 0xa7
0x0 0xff 0xff
0x0 0x37 0x3c
0x0 0x80 0x0
0xff 0x66 0x33
0xff 0x66 0xcc
0x1a 0x5f 0x67
0x0 0x0 0xee
0x55 0x1a 0x8b
0xe4 0xe4 0xe4
0x6a 0x6a 0x6a
0xa3 0xa3 0xa3
0x99 0x99 0x99
0x0 0x0 0x33
0x0 0x0 0x66
0x0 0x0 0x99
0x0 0x0 0xcc
0x0 0x33 0x0
0x0 0x33 0x33
0x0 0x33 0x66
0x0 0x33 0x99
0x0 0x33 0xcc
0x0 0x33 0xff
0x0 0x66 0x0
0x0 0x66 0x33
0x0 0x66 0x66
0x0 0x66 0x99
0x0 0x66 0xcc
0x0 0x66 0xff
0x0 0x99 0x0
0x0 0x99 0x33
0x0 0x99 0x66
0x0 0x99 0x99
0x0 0x99 0xcc
0x0 0x99 0xff
0x0 0xcc 0x0
0x0 0xcc 0x33
0x0 0xcc 0x66
0x0 0xcc 0x99
0x0 0xcc 0xcc
0x0 0xcc 0xff
0x0 0xff 0x33
0x0 0xff 0x66
0x0 0xff 0x99
0x0 0xff 0xcc
0x33 0x0 0x0
0x33 0x0 0x33
0x33 0x0 0x66
0x33 0x0 0x99
0x33 0x0 0xcc
0x33 0x0 0xff
0x33 0x33 0x0
0x33 0x33 0x33
0x33 0x33 0x99
0x33 0x33 0xcc
0x33 0x33 0xff
0x33 0x66 0x0
0x33 0x66 0x33
0x33 0x66 0x66
0x33 0x66 0x99
0x33 0x66 0xcc
0x33 0x66 0xff
0x33 0x99 0x0
0x33 0x99 0x33
0x33 0x99 0x66
0x33 0x99 0x99
0x33 0x99 0xcc
0x33 0x99 0xff
0x33 0xcc 0x0
0x33 0xcc 0x33
0x33 0xcc 0x66
0x33 0xcc 0x99
0x33 0xcc 0xcc
0x33 0xcc 0xff
0x33 0xff 0x0
0x33 0xff 0x33
0x33 0xff 0x66
0x33 0xff 0x99
0x33 0xff 0xcc
0x33 0xff 0xff
0x66 0x0 0x0
0x66 0x0 0x33
0x66 0x0 0x66
0x66 0x0 0x99
0x66 0x0 0xcc
0x66 0x0 0xff
0x66 0x33 0x0
0x66 0x33 0x33
0x66 0x33 0x66
0x66 0x33 0x99
0x66 0x33 0xcc
0x66 0x33 0xff
0x66 0x66 0x0
0x66 0x66 0x33
0x66 0x66 0x66
0x66 0x66 0x99
0x66 0x66 0xff
0x66 0x99 0x0
0x66 0x99 0x33
0x66 0x99 0x66
0x66 0x99 0x99
0x66 0x99 0xcc
0x66 0x99 0xff
0x66 0xcc 0x0
0x66 0xcc 0x33
0x66 0xcc 0x66
0x66 0xcc 0x99
0x66 0xcc 0xcc
0x66 0xcc 0xff
0x66 0xff 0x0
0x66 0xff 0x33
0x66 0xff 0x66
0x66 0xff 0x99
0x66 0xff 0xcc
0x66 0xff 0xff
0x99 0x0 0x0
0x99 0x0 0x33
0x99 0x0 0x99
0x99 0x0 0xcc
0x99 0x0 0xff
0x99 0x33 0x0
0x99 0x33 0x33
0x99 0x33 0x66
0x99 0x33 0x99
0x99 0x33 0xcc
0x99 0x33 0xff
0x99 0x66 0x0
0x99 0x66 0x33
0x99 0x66 0x66
0x99 0x66 0x99
0x99 0x66 0xcc
0x99 0x66 0xff
0x99 0x99 0x0
0x99 0x99 0x33
0x99 0x99 0x66
0x99 0x99 0xcc
0x99 0xcc 0x0
0x99 0xcc 0x33
0x99 0xcc 0x66
0x99 0xcc 0x99
0x99 0xcc 0xcc
0x99 0xcc 0xff
0x99 0xff 0x0
0x99 0xff 0x33
0x99 0xff 0x66
0x99 0xff 0x99
0x99 0xff 0xcc
0x99 0xff 0xff
0xcc 0x0 0x0
0xcc 0x0 0x33
0xcc 0x0 0x66
0xcc 0x0 0x99
0xcc 0x0 0xcc
0xcc 0x0 0xff
0xcc 0x33 0x0
0xcc 0x33 0x33
0xcc 0x33 0x66
0xcc 0x33 0x99
0xcc 0x33 0xcc
0xcc 0x33 0xff
0xcc 0x66 0x0
0xcc 0x66 0x33
0xcc 0x66 0x66
0xcc 0x66 0x99
0xcc 0x66 0xcc
0xcc 0x66 0xff
0xcc 0x99 0x0
0xcc 0x99 0x33
0xcc 0x99 0x66
0xcc 0x99 0x99
0xcc 0x99 0xcc
0xcc 0x99 0xff
0xcc 0xcc 0x0
0xcc 0xcc 0x33
0xcc 0xcc 0x66
0xcc 0xcc 0x99
0xcc 0xcc 0xcc
0xcc 0xcc 0xff
0xcc 0xff 0x0
0xcc 0xff 0x33
0xcc 0xff 0x66
0xcc 0xff 0x99
0xcc 0xff 0xcc
0xcc 0xff 0xff
0xff 0x0 0x33
0xff 0x0 0x66
0xff 0x0 0x99
0xff 0x0 0xcc
0xff 0x0 0xff
0xff 0x33 0x0
0xff 0x33 0x33
0xff 0x33 0x66
0xff 0x33 0x99
0xff 0x33 0xcc
0xff 0x33 0xff
0xff 0x66 0x0
0xff 0x66 0x66
0xff 0x66 0x99
0xff 0x66 0xff
0xff 0x99 0x0
0xff 0x99 0x33
0xff 0x99 0x66
0xff 0x99 0x99
0xff 0x99 0xcc
0xff 0x99 0xff
0xff 0xcc 0x0
0xff 0xcc 0x33
0xff 0xcc 0x66
0xff 0xcc 0x99
0xff 0xcc 0xcc
0xff 0xcc 0xff
0xff 0xff 0x33
0xff 0xff 0x66
0xff 0xff 0x99
EOF
    fi

    if test ! -f /etc/imlib/im_palette-small.pal; then
      echo Recreating /etc/imlib/im_palette-small.pal
      # Recreate the small palette file
      cat > /etc/imlib/im_palette-small.pal<<EOF
0x0 0x0 0x0
0xff 0xff 0xff
0xff 0x0 0x0
0xff 0xff 0x0
0x0 0xff 0x0
0x0 0x0 0xff
0x0 0xff 0xff
0x99 0x99 0x99
0xff 0x88 0x0
0x88 0x0 0x0
0x0 0x88 0x88
0x88 0x88 0x0
0xff 0xcc 0x97
0xbb 0xbb 0xbb
0x9f 0x6b 0x42
0x55 0x55 0x55
0xdd 0xdd 0xdd
0x77 0x77 0x77
0x33 0x33 0x33
0xcc 0x0 0x0
0xff 0x44 0x0
0xff 0xcc 0x0
0xcc 0xcc 0x0
0x60 0x60 0x0
0x0 0x43 0x0
0x0 0x7f 0x0
0x0 0xcc 0x0
0x0 0x44 0x44
0x0 0x0 0x44
0x0 0x0 0x88
0xef 0xb1 0x7b
0xdf 0x98 0x5f
0xbf 0x87 0x56
0x7f 0x57 0x26
0x5f 0x39 0xc
0x3f 0x1c 0x0
0x21 0x0 0x0
0x0 0x43 0x87
0x2d 0x70 0xaf
0x5a 0x9e 0xd7
0x87 0xcc 0xff
0xff 0xe0 0xba
0x21 0x43 0xf
0x3d 0x5d 0x25
0x59 0x78 0x3a
0x75 0x93 0x4f
0x91 0xae 0x64
0xad 0xc8 0x7a
0xf0 0xa8 0xef
0xd0 0x88 0xd0
0xaf 0x66 0xaf
0x8e 0x44 0x8e
0x6d 0x22 0x6d
0x4b 0x0 0x4b
0xff 0xc0 0xbc
0xff 0x93 0x91
0xff 0x66 0x67
0xd8 0xf2 0xbf
0xff 0xc9 0x68
0xff 0x96 0x67
0xa5 0x60 0xff
0x51 0xff 0x99
0x3f 0xa5 0x63
0x98 0x90 0x67
EOF
    fi

    if test ! -f /etc/imlib/im_palette-tiny.pal; then
      echo Recreating /etc/imlib/im_palette-tiny.pal
      # Recreate the tiny palette file
      cat > /etc/imlib/im_palette-tiny.pal<<EOF
0x0 0x0 0x0
0xff 0xff 0xff
0xff 0x0 0x0
0xff 0xff 0x0
0x0 0xff 0x0
0x0 0x0 0xff
0x0 0xff 0xff
0x99 0x99 0x99
0xff 0x88 0x0
0x88 0x0 0x0
0x0 0x88 0x88
0x88 0x88 0x0
0xff 0xcc 0x97
0xbb 0xbb 0xbb
0x9f 0x6b 0x42
0x55 0x55 0x55
EOF
    fi
  fi
  ;;
abort-upgrade) ;;
*)
  echo "ERROR: unrecognized imlib-base preinst arguments: $@" 1>&2
  echo "       installation of imlib-base package aborted" 1>&2
  exit 1; # Failure
  ;;
esac
