#!/bin/sh

set -e

if [ -d /data/squid/cache ]; then

  /usr/sbin/squid -NYC -d 1 -f /etc/squid/squid.conf
else

  mkdir -p /data/squid/cache

  # init cache dir
  if [ "$SQUID_CAHCE_SIZE" ]; then

    echo "cache_dir ufs /data/squid/cache $SQUID_CAHCE_SIZE 16 256" >> /etc/squid/squid.conf

    chown squid:squid /data/squid/cache
    /usr/sbin/squid -N -z -f /etc/squid/squid.conf
  fi

  # set cache peer
  if [ "$SQUID_PEER_HOST" ]; then
    echo "cache_peer $SQUID_PEER_HOST parent $SQUID_PEER_PORT 0 no-query no-digest" >> /etc/squid/squid.conf
  fi

  /usr/sbin/squid -NYC -d 1 -f /etc/squid/squid.conf
fi
