#!/bin/sh

# PolipoPostflight gets invoked after any install or upgrade.

ADDSYSUSER=$RECEIPT_PATH/addsysuser
if [ ! -x "$ADDSYSUSER" ]; then
  echo "Could not find addsysuser script."
  exit 1
fi

POLIPOUSER=_polipo
POLIPOGROUP=daemon
TARGET=$2/Library/Polipo
LOGDIR=$TARGET/log

# Check defaults for TARGET
if [ "$TARGET" == "//Library/Polipo" ]; then
	TARGET=/Library/Polipo
fi

# Create user $POLIPOUSER in group daemon.  If it's already there, great.
$ADDSYSUSER $POLIPOUSER "Polipo System user" $POLIPODIR

# Create the polipo direcpolipoy, if it doesn't exist.
if [ ! -d $POLIPODIR ]; then
  mkdir -p $POLIPODIR
fi
if [ ! -d $LOGDIR ]; then
  mkdir -p $LOGDIR
fi
# Check its permissions.
chown $POLIPOUSER $POLIPODIR
chgrp daemon $POLIPODIR
chmod 700 $POLIPODIR
chown $POLIPOUSER $LOGDIR
chgrp daemon $LOGDIR
chmod 700 $LOGDIR

# Create the configuration file only if there wasn't one already.
if [ ! -f $TARGET/config]; then
  cp $TARGET/config.osx $TARGET/config
fi

cd /usr/share/man/man1
MAN1=$TARGET/man/man1
ln -sf $MAN1/*.1 .

if [ -d /Library/StartupItems/Polipo ]; then
  find /Library/StartupItems/Polipo -print0 | xargs -0 chown root:wheel
fi

# Copy Uninstaller
if [ -f $PACKAGE_PATH/Contents/Resources/uninstall_polipo_bundle.sh ]; then
   cp $PACKAGE_PATH/Contents/Resources/uninstall_polipo_bundle.sh $TARGET/uninstall_polipo_bundle.sh
   chmod 755 $TARGET/uninstall_polipo_bundle.sh
fi
