#!/bin/bash
#
# 	DESCRIPTION:	install package, if -s options not given,
#			get package from /Rocky/packages by default.
#
#	SCRIPT NAME:	install_pkg.sh
#
#	Usage:	echo "acl" |./install_pkg.sh -s /Rocky/packages
#
#	Input:	stdin
#			1. pkgname
#			argument:
#			[--help] show usage
#			[-s|--source]      package source directory, if not specify, default is /Rocky/packages.
#
#	Output:	
#			sucess:
#			@ install $pkgname success.
#	Return value:
#			1 argument error
#			2 source directory $PKG_SOURCE_DIR doesn't exist
#			3 pkgname doesn't exist in source directory
#
#	TODO:
#			1. need handle more case for install package (use pkgadd utils)
#				1). if package has already installed.
#				2). if devices nearly no spaces left.
#
#			2. add install package from below media:
#				1). install from network.
#					(1). get packages.txt from mirror.
#					(2). download package from mirror to hardisk /var/lib/pkg/cache,
#						make sure cache is have enough spaces to store download package.
#						Notice: download package, need convert package name of #->%23
#				2). install from nfs.
#					(1). mount nfs directory as package source directory.
#
#
#	AUTHOR:		Qin Bo
#
#	EMAIL:		bqin@linx-info.com
#
#	DATE:		2010-08-09
#
#	HISTORY:
#	REVISOR		DATE			MODIFICATION
#	Qin Bo		2010-08-09		create
#
#
#

source ./functions

usage ()
{
	if [ "$1" == "-n" ];then
		cmd="info"
		ret=0
	else
		cmd="err"
		ret=1
	fi
	$cmd "
This script will install a package

Usage: $0 options

options:
   [--help]      show this message
   [-s|--source]      package source directory, if not specify, default is /Rocky/packages.
   
   (ex: echo make|install_pkg.sh -s /Rocky/packages)
   "
   return $ret
}

# get the package file in source directory
get_pkg_file ()
{
	if [ $# -ne 1 ];then
		err "$FUNCNAME function argument error , it must 1 argument !"
		return 1
	fi
	
	local pkg_name="$1"

	# get the first package if have multi-package in source directory
	if [ -e "$PKG_SOURCE_DIR"/"$pkgname"'#'*tar.gz ]; then
		ls "$PKG_SOURCE_DIR"/"$pkgname"'#'*tar.gz|head -n 1 
    elif [ -e "/Rocky/install_lib_32/pkg_lib_i686"/"$pkgname"'#'*tar.gz ]; then
		ls "/Rocky/install_lib_32/pkg_lib_i686"/"$pkgname"'#'*tar.gz|head -n 1 
	else
		err "package \"$pkgname\" doesn't exist in "$PKG_SOURCE_DIR" "
		return 3
	fi
}

add_pkg ()
{
	if [ $# -ne 1 ];then
		err "$FUNCNAME function argument error , it must have 1 argument!"
		return 1
	fi

	local pkg_src=$1

	pkgadd -r $TARGET $pkg_src 1>>$DEV_LOG 2>&1
}

install_pkg ()
{
	if [ $# -ne 1 ];then
		err "$FUNCNAME function argument error , it must have 1 argument!"
		return 1
	fi  

	local pkgname
	local db_dir
	local db
	local pkg_src

	pkgname="$1"

	# create db for pkgutils
	db_dir="/var/lib/pkg"
	db="$TARGET$db_dir/db"

	if [ ! -e "$db" ];then
		info "create $db ..."
		mkdir -p "$TARGET$db_dir"
		touch "$db"
	fi

	pkg_src=$(get_pkg_file $pkgname)
    if [ "${?}" -eq 0 ];then
        info "installing $pkg_src to $TARGET"
        add_pkg $pkg_src
        erv
    fi
}

main ()
{
	local line 
	local tmp
	local pkgname
	tmp=$(getopt -o s: --long source:,help -- "$@" 2>>$DEV_LOG)
	if [ $? -ne 0 ];then
		usage; erv
	fi

	# set all argument to arg variable
	eval set -- "$tmp"

	while true ;do
		case "$1" in
			-s|--source) 
				PKG_SOURCE_DIR="$2" 
				shift 2
			;;
			--help) usage -n; exit 0 ;;
			# shift the last "--", this dash is define by getopt (more information see man getopt)
			--) shift; break;;
			*) usage; erv ;;
		esac
	done

	if [ ! -e "$PKG_SOURCE_DIR" ];then
		err "source directory: $PKG_SOURCE_DIR doen't exist !"
		exit 2
	fi

	while read line ;do
		echo "$line"
	done |	sort -u | 
	while read pkgname;do 
		if [ -n "$pkgname" ];then
			install_pkg "$pkgname"
			erv
			info "install $pkgname success" stdout
		fi
	done
	
}

PKG_SOURCE_DIR="/Rocky/packages"

main "$@"

