#!/bin/sh

# take command-line arguments for non-standard installations
# be sure to use mkdir -p, to force the directories to exist

# try to determine the bpp, default to 16

bpp=16
for i in $PATH; do
  if test -f $i/xdpyinfo; then
    bpp=`xdpyinfo | grep ' depth:' | tr -s ' ' | cut -f 3 -d' ' | tail -1`
    break
  fi
done

echo "Detected colordepth : $bpp"

diff_cmd="diff -q -b -B -I ^#"

local_dir=~/GNUstep/Library/AfterStep
global_dir=/usr/local/share/afterstep
tmp_dir=/tmp/theme

mkdir -p $tmp_dir

# determine the PixmapPath

cd $local_dir
if test -f $local_dir/base.${bpp}bpp; then
  pixmap_path=`grep -i PixmapPath $local_dir/base.${bpp}bpp | sed "s/PixmapPath//"`
elif test -f $global_dir/base.${bpp}bpp; then
  pixmap_path=`grep -i PixmapPath $global_dir/base.${bpp}bpp | sed -e "s/PixmapPath//" -e "s/:/ /g" -e 's@/ @ @g' -e 's@/$@@'`
fi

pixmap_path=`eval "echo $pixmap_path"`

# clean out our shopping bags

files="configs look_files pixmaps fonts"

for i in $files; do
  rm -f $tmp_dir/$i
  touch $tmp_dir/$i
done

#########################################################################
# make a list of needed config files
#########################################################################

cd $local_dir
find . -maxdepth 1 -name "*" -type f | sed "s@^./@@" | grep -v '~$' > $tmp_dir/local_files

cd $global_dir
find . -maxdepth 1 -name "*" -type f | sed "s@^./@@" > $tmp_dir/global_files

for i in `cat $tmp_dir/local_files`; do
  if ! test -e $i; then
    echo $i >> $tmp_dir/configs
#    echo $i
  elif test ! -d $i && ! $diff_cmd $i $local_dir/$i > /dev/null 2>&1; then
    echo $i >> $tmp_dir/configs
#    echo $i
  fi
done

# figure out which looks & feels we're using

file=""
if test -d $local_dir/looks; then
  cd $local_dir/looks
  for j in `echo *`; do
    if $diff_cmd $j $local_dir/non-configurable/0_look.${bpp}bpp > /dev/null 2>&1; then
      file=$j
      break
    fi
  done
fi
if test "x$file" != "x"; then
  echo looks/$file >> $tmp_dir/configs
#  echo looks/$file
  look_file=$file
fi

file=""
if test -d $local_dir/feels; then
  cd $local_dir/feels
  for j in `echo *`; do
    if $diff_cmd $j $local_dir/non-configurable/0_feel.${bpp}bpp > /dev/null 2>&1; then
      file=$j
      break
    fi
  done
fi
if test "x$file" != "x"; then
  echo feels/$file >> $tmp_dir/configs
#  echo feels/$file
fi

#########################################################################
# make a list of needed fonts
#########################################################################
echo fonts

cd $local_dir
cat `cat $tmp_dir/configs` | sed "s/	/ /g" | grep -v "^ *#" | sed "s/[Ff][Oo][Nn][Tt] *[^ ]*/\\
&\\
/" | grep "[Ff][Oo][Nn][Tt] *[^ ]*" | sed "s/[Ff][Oo][Nn][Tt] *//" > $tmp_dir/fonts

for i in `cat $tmp_dir/fonts`; do
  case $i in
    -*)
      echo $i | cut -f2 -d'-'
      ;;
    *)
      echo $i | cut -f1 -d'-'
      ;;
  esac
done

#########################################################################
# make a list of needed pixmaps
#########################################################################
echo pixmaps

keywords="\
BackPixmap \
ButtonPixmap \
ForeStipple \
MArrowPixmap \
MTitlePixmap \
MenuHiPixmap \
MenuPinOff \
MenuPinOn \
MenuPixmap \
STitlePixmap \
TitlePixmap \
UTitlePixmap \
"

for i in $keywords; do
  cat $local_dir/looks/$look_file | grep -v "^#" | grep -i $i | tr '	' ' ' | tr -s ' ' | sed -e "s/ $//" -e "s/[^ ]* //g" >> $tmp_dir/pixmaps
done

# handle TitleButton specially

cat $local_dir/looks/$look_file | grep -v "^#" | tr '	' ' ' | tr -s ' ' | grep -i "TitleButton " | sed -e "s/ $//" -e "s/[^ ]* //" -e "s/[^ ]* //" -e "s/ /\\
/" >> $tmp_dir/pixmaps

# if database was modified, get a list of database icons

if test -f $local_dir/database; then
  cat $local_dir/database | grep -v "^#" | tr '	' ' ' | tr -s ' ' | sed "s/[Ii][Cc][Oo][Nn] [^ ]*/\\
&\\
/" | grep "[Ii][Cc][Oo][Nn] [^ ]*" | sed "s/[Ii][Cc][Oo][Nn] //" >> $tmp_dir/pixmaps
fi

# if wharf was modified, get a list of wharf pixmaps

if test -f $local_dir/wharf; then
  cat $local_dir/wharf | grep "^[*]" | tr '	' ' ' | tr -s ' ' | cut -f3 -d' ' | tr ',' ' ' | sed "s/ /\\
/" >> $tmp_dir/pixmaps
fi

# if zharf was modified, get a list of zharf pixmaps

if test -f $local_dir/zharf; then
  cat $local_dir/zharf | grep "^[*]" | tr '	' ' ' | tr -s ' ' | cut -f3 -d' ' | tr ',' ' ' | sed "s/ /\\
/" >> $tmp_dir/pixmaps
fi

# find the pixmaps

pixmaps=`cat $tmp_dir/pixmaps | sort | uniq`

echo "" > $tmp_dir/pixmaps
for i in $pixmaps; do
  file=""
  for j in $pixmap_path; do
    if test -e $j/$i; then
      file=$j/$i
      break
    fi
  done
  if test "x$file" != "x"; then
    echo $file >> $tmp_dir/pixmaps
#    echo $file
  else
    echo unable to find "'"$i"'", ignored
  fi
done

#########################################################################
# make the theme
#########################################################################

cd $tmp_dir

mkdir -p AfterStep
cat $tmp_dir/configs | sort | uniq > $tmp_dir/tmp
for i in `cat $tmp_dir/tmp`; do
  echo copying $i
  cp $local_dir/$i AfterStep
done

mkdir -p AfterStep/desktop/icons/common
cat $tmp_dir/pixmaps | grep -v $global_dir | sort | uniq > $tmp_dir/tmp
for i in `cat $tmp_dir/tmp`; do
  echo copying $i
  cp $i AfterStep/desktop/icons/common
done



