#! /bin/bash

####################################
# Gimp versions that I have in store
####################################
declare -A versions

# The usual versions
versionOld=2.10.32
versionStd=2.10.34
versionDev=3.00

versions['gimp']=$versionStd
versions['gimp210']=$versionStd
versions['gimpold']=$versionOld
versions['gimpdev']=$versionDev
versions['gimpnew']=$versionDev

# Some more specific versions
# "gimpfresh" is just the gimp-dev with the factory profile 
versions['gimpfresh']=$versionDev

# My oldies
versions['gimp204']=2.04
versions['gimp208']=2.08

# The future
versions['gimp299']=2.99
# The future
versions['gimp300']=3.00

##############################
# The various profiles
# It's a many-to-many relation
##############################
declare -A profiles
profiles['gimp']=~/.config/GIMP/2.10
profiles['gimp210']=~/.config/GIMP/2.10
profiles['gimpdev']=~/.config/GIMP/3.00
profiles['gimpnew']=~/.config/GIMP/3.00
profiles['gimpold']=~/.config/GIMP/2.10-old
profiles['gimpfresh']=/tmp/gimpfresh  # start with blank profile
profiles['gimp204']=/tmp/gimp204
profiles['gimp208']=~/.config/GIMP/2.08
profiles['gimp299']=~/.config/GIMP/2.99
profiles['gimp300']=~/.config/GIMP/3.00

# How we are called
me=$(basename $0)

# Profile and version from name
version=${versions[$me]}
profile=${profiles[$me]}

showplayground='--show-playground'
[[ $version == 2.0* ]] && showplayground=""

[[ -n "$version" && -n "$profile" ]] || { printf "Missing settings for %s\n" "$me" >&2 ; exit 1 ; } 
    
>&2 printf "Running %s: version %s in profile: %s\n" "$me" "$version" "$profile"

#. /Gimp-dev/$version/setenv
# The directory where the various bit of code come from
# All my Gimp versions (locally compiled) are in /Gimp-dev/$version
# and each such directory has a "run" subdir where the code is installed
# so for instance "/Gimp-dev/2.10.24/run"? For a regulatar deb/rpm install
# that would be "/usr"

# Set a few environment variables
export GIMP_PREFIX=/Gimp-dev/$version/run
[[ -n "$GIMP2_DIRECTORY" ]] || export GIMP2_DIRECTORY="$profile"
[[ -n "$GIMP3_DIRECTORY" ]] || export GIMP3_DIRECTORY="$profile" # Just in case

printf "GIMP_PREFIX: %s\n" "$GIMP_PREFIX"
printf "GIMP2_DIRECTORY %s\n" "$GIMP2_DIRECTORY"
printf "GIMP3_DIRECTORY %s\n" "$GIMP3_DIRECTORY"

arch="$(dpkg-architecture -qDEB_HOST_MULTIARCH 2> /dev/null)"
export PATH=$GIMP_PREFIX/bin:$PATH
export LD_LIBRARY_PATH=$GIMP_PREFIX/lib:$GIMP_PREFIX/lib/$arch:$LD_LIBRARY_PATH
export GI_TYPELIB_PATH="${GIMP_PREFIX}/lib/girepository-1.0:${GIMP_PREFIX}/lib/${arch}/girepository-1.0:$GI_TYPELIB_PATH"


# Become Gimp
exec $GIMP_PREFIX/bin/gimp $showplayground "$@"
