#!/bin/sh
#
# Builds a deb package 
# This script installs a deb package in the repository
# the script assumes that all the required scripts are in the
# same directory as the script
# Usage -- <path of the directory to install> < location of deb package> <ubuntu|debian> <intrepid|lenny|etch>
export CDPATH=
which reprepro > /dev/null 2>&1
if [ $? -eq 1 ]; then
 echo You need \"reprepro\" to run this, install it!
 exit 1
fi
if [ "X$1" = "X" ]; then
 echo The installation directory should be a valid name!
 exit 1
fi
if [ ! -f "$2" ]; then
 echo The package for installation does not exit!
 exit 1
fi
if [ "$3" != "debian" ] && [ "$3" != "ubuntu" ]; then
 echo "The third argument has to be either <ubuntu|debian>!"
 exit 1
fi
if [ "$4" != "intrepid" ] && [ "$4" != "lenny" ] && [ "$4" != "etch" ] && [ "$4" != "jaunty" ]; then
 echo "The fourth argument has to be either <jaunty|intrepid|lenny|etch>!"
 exit 1
fi

CONF_DIR=$1/conf
DB_DIR=$1/db
if [ ! -d "$CONF_DIR" ]; then
 echo The installation directory does not exist. Creating it!
 mkdir -p $CONF_DIR
 mkdir -p $DB_DIR
fi
SCRIPT_DIR=install/client/$3

##Deb package scripts
if [ ! -f $CONF_DIR/distributions ]; then
cp $SCRIPT_DIR/$4.distributions $CONF_DIR/distributions
else
  if [ `grep -c $4 $CONF_DIR/distributions` -lt 1 ]; then
    echo "\n" >> $CONF_DIR/distributions
    cat $SCRIPT_DIR/$4.distributions >> $CONF_DIR/distributions
  fi
fi
reprepro -Vb "$1" --confdir $CONF_DIR --dbdir $DB_DIR includedeb $4 "$2"
