#!/bin/sh
#
# ports(8) - ports system
#

VERSION="4.0"
ROOT="/var/ports"

check_root() {
    if [ ! -d "$ROOT" ]; then
	echo "$COMMAND: directory $ROOT not found, creating it."
	mkdir -p $ROOT
    fi
}

update_ports() {
    if [ -f /etc/ports.conf ]; then
        . /etc/ports.conf
      else
        echo "$COMMAND: configuration file /etc/ports.conf not found"
        exit 1
    fi

    if [ "$UID" != "0" ]; then
	echo "$COMMAND: only root can update ports"
	exit 1
    fi
 
    if [ -z "$PSERVER" ]; then
        echo "$COMMAND: no server specificed in /etc/ports.conf"
        exit 1
    fi

    if [ -z "$PPATH" ]; then
        echo "$COMMAND: no path specificed in /etc/ports.conf"
        exit 1
    fi

    if [ ! -z "$PUSER" ]; then
      PPUSER="${PUSER}@"
    fi

    if [ "`type -p rsync`" = "" ]; then
	echo "$COMMAND: command 'rsync' not found"
        exit 1
    fi
    
    (cd $ROOT && rsync -q -av --delete ${PPUSER}${PSERVER}::${PPATH} .)
}

list_ports() {
    (cd $ROOT && find . -name pkg.build -printf "%h\n" | sed 's|^./||g' | sort)
}

list_differences_at_exit() {
    rm $installed_list $ports_list $output $output_sorted &> /dev/null
}

list_differences() {
    installed_list=/tmp/ports.tmp-1$$
    ports_list=/tmp/ports.tmp-2$$
    output=/tmp/ports.tmp-3$$
    output_sorted=/tmp/ports.tmp-4$$
    found_diff="no"

    trap list_differences_at_exit EXIT

    pkginfo -i > $installed_list
    ports -l > $ports_list

    for package in `cat $installed_list | gawk '{ print $1 }'`; do
	installed_version=`cat $installed_list | grep "^$package " | gawk '{ print $2 }'`
	port_list=`cat $ports_list | grep "/$package\$"`
	for port in $port_list; do
	    port_version=`cd $ROOT/$port; . pkg.build; echo $version-$release`
	    if [ "$installed_version" != "$port_version" ]; then
		echo "${port%/*} $package $port_version $installed_version" >> $output
		found_diff="yes"
	    fi
	done
    done

    if [ "$found_diff" = "yes" ]; then
        echo "Collection Name Port Installed" > $output_sorted
	sort $output >> $output_sorted
	column -t $output_sorted
    else
	echo "No differences found"
    fi
}

print_try_help() {
    echo "Try '$COMMAND --help' for more information."
}

print_help() {
    echo "Usage: $COMMAND [OPTION]..."
    echo "Update or list the ports collection."
    echo
    echo "   -u, --update    update ports"
    echo "   -l, --list      list ports"
    echo "   -d, --diff      list version differences"
    echo "   -h, --help      print this help and exit"
    echo "   -v, --version   output version information and exit"
    echo
    echo "Report bugs to <johne@rootlinux.org>."
}

parse_options() {
    OPT_VERBOSE="no"
    OPT_MODE=""
    OPT_PASSIVE=""

    for OPT in "$@"; do
        case $OPT in
	    -u|--update)
		OPT_MODE="update" ;;
	    -l|--list)
		OPT_MODE="list" ;;
	    -d|--diff)
		OPT_MODE="diff" ;;
	    -v|--version)
		echo "$COMMAND $VERSION"
		exit 0 ;;
	    -h|--help)
		print_help
                exit 0 ;;
	    *)
                echo "$COMMAND: invalid option $OPT"
                print_try_help
		exit 1 ;;
        esac
    done
}

main() {
    parse_options "$@"

    if [ "$OPT_MODE" = "update" ]; then
	check_root
	update_ports
    elif [ "$OPT_MODE" = "list" ]; then
	check_root
	list_ports
    elif [ "$OPT_MODE" = "diff" ]; then
	check_root
	list_differences
    else
	echo "$COMMAND: option missing"
	print_try_help
	exit 1
    fi

    exit 0
}

COMMAND=`basename $0`

main "$@"

# End of file
