#!/usr/local/bin/tcsh -f

## ץȤǻȤѿ
set INST_ORG=/tmp/inst.org
set INST_NEW=/tmp/inst.new
set INST_COMM=/tmp/inst.comm
set INST_LOCK=/tmp/inst.lock
set TR=`which tr`
set SED=`which jsed`
set FIND=`which gfind`
set SORT=`which sort`
set LN=`which ln`
set MAKE=`which make`
set COMM=`which comm`
set CAT=`which cat`
set EGREP=`which egrep`
set RM=`which rm`
set TAR=`which tar`
set GZIP=`which gzip`
set LS=`which ls`
set AWK=`which gawk`
set KPKG=/var/log/kpkg
set UNAME=`which uname`
set ADDSFX="`$UNAME -m`_`$UNAME -s``$UNAME -r | $TR -d "."`.tar"

## $argv[1]ˤswitchʸ
switch ($argv[1])

	## Ϳ줿ѥMakefileפƥǥ쥯ȥؤΥѥˤ
	case Mfpath:
		echo $argv[2] | $SED 's/Makefile$//'
	breaksw

	## åե뤬뤫ɤǧ
	case LockCheck:
		if (-e $INST_LOCK) then
			echo YES
		endif
	breaksw

	## ְκեθФȥåեκ
	case Findfiles:
		$FIND $argv[2] -mtime -1 | $SORT > $INST_ORG
		$LN -s $INST_ORG $INST_LOCK
	breaksw	

	## make installμ¹
	case Installfile:
		cd $argv[2]
		$MAKE install

		if ($argv[3] == "YES") then
			$MAKE install.man >& /dev/null
		endif

		if ($#argv == 4) then
			$MAKE $argv[4] >& /dev/null
		endif
	breaksw

	## 󥹥ȡեθФȥåեκ
	case FindNewfiles :
		set PLIST=$KPKG"/PLIST."$argv[2]

		$FIND $argv[3] -mtime -1 | $SORT > $INST_NEW
		$COMM -13 $INST_ORG $INST_NEW > $INST_COMM

		foreach i (`$CAT $INST_COMM`)
			if (-f $i) then
				if ($#argv == 4) then
					if ($argv[4] == "") then
						echo $i >> $PLIST
					else
						echo $i | $EGREP -v $argv[4] >> $PLIST
					endif
				else
					echo $i >> $PLIST
				endif
			endif
		end
		$RM $INST_LOCK
	breaksw

	## ѥå"YES"ʤѥå
	case Mkpkg:
		set PLIST=$KPKG"/PLIST."$argv[2]
		set PKG_NAME="/"$argv[2]"_"$ADDSFX

		cd /
		foreach i (`$CAT $PLIST`)
			$TAR -rf $PKG_NAME $i >& /dev/null
		end
		$TAR -rf $PKG_NAME $PLIST >& /dev/null
		$GZIP $PKG_NAME >& /dev/null
	breaksw

	## DeleteInformationǻѤѥåν񤭽Ф
	##PLIST.פȤʸƤޤ
	case PkgList :
		$LS $KPKG | $SED 's/PLIST.//'
	breaksw

	## ѥåȤPLISTեõޤ
	case DeletePkg :
		set PLIST_NAME=$KPKG"/PLIST."$argv[2]

		foreach i (`$CAT $PLIST_NAME`)
			$RM -r $i
		end
		$RM $PLIST_NAME
	breaksw

	## ѥåΥե륵֤ޤʥХʥ꤫
	case BinSize :
		set VV=0

		foreach i (`$TAR -tvzf $argv[2] | $AWK '{print $3}'`)
			@ VV += $i
		endif
		echo $VV"B"
	breaksw

	## ѥåΥե֤ޤʥХʥ꤫
	case BinList :
		$TAR -tvzf $argv[2] | $AWK '{print $6}'
	breaksw

	## ѥåΥե륵֤ޤPLIST
	case InfoSize :
		set PLIST_NAME=$KPKG"/PLIST."$argv[2]
		set VV=0

		foreach i (`$CAT $PLIST_NAME`)
			if ( -f $i ) then
			set V=`$LS -l $i | $AWK '{print $5}'`
			 @ VV += $V
			endif
		end
		echo $VV"B"
	breaksw

	## ѥåΤˤPLISTΤǤˤκ֤ޤ
	case InfoDate :
		set PLIST_NAME=$KPKG"/PLIST."$argv[2]

		$LS -l $PLIST_NAME | $AWK '{printf "%s / %s / %s",$6,$7,$8}'
	breaksw

	## ѥåΥե֤ޤPLIST
	case InfoList :
		$CAT $KPKG"/PLIST."$argv[2]
	breaksw

endsw
