#!/usr/bin/perl

# Copyright Shane Nay <shane@agendacomputing.com>
# Under the GPL license
my %path;
if($#ARGV<1) {
  die "Not enough options.\nUsage: vrpcpkg packagename directory\n";
}
my $fakeroot=$ARGV[1];
my $packagename=$ARGV[0];
die("$fakeroot is not a directory\n") if(! -d $fakeroot);
my $outpath="/tmp/vrpout";
if(-e $outpath) {
  if (-d $outpath) {
    my $dtmp=dirlist($outpath);
    my $ftmp=filelist($outpath);
    die "Please empty the contents of $outpath before running this program.\n" if(@$tmp||@$ftmp); 
  } else {
    die("$outpath was expected to be a directory..., please delete the file and make this a directory.");
  }
} else {
  open(FP,"|mkdir $outpath")|| die("Please make the temp directory : $outpath");;
  close(FP);
}
my @sploc=qw {
	      home
	      sbin
	      bin
	      lib
	      etc
	      log
	      usr
	      usr/bin
	      usr/etc
	      usr/share
	      usr/lib
	      usr/man
	      usr/doc
	      usr/sbin
	      usr/local
	      usr/local/bin
	      usr/local/etc
	      usr/local/man
	      usr/local/lib
	      usr/local/share
	      usr/local/doc
	      usr/local/sbin
	     };
foreach my $sp (@sploc) {
  my @splp=split('/',$sp);
  bdepth(\%path,\@splp);
}
pdepth($fakeroot,\%path,"root");
my $geninstaller='#!/bin/sh
###########VRP Generic Installer .1 Beta################
# Copyright Shane R. Nay (shane@agendacomputing.com)
# License terms under the GPL
# There are deficiencies in this program.  Specifically
# dealing with "diff"s of already present files.  This
# will have to be worked on, but the core is all here.


if [ ! -f $1 ]; then
	echo "Config file must be specified"
	exit 1;
fi
source $1
VRP_PACKAGE=$2

VRP_CONSIST="home|VRP_HOME"
VRP_CONSIST="${VRP_CONSIST}:root|VRP_ROOT|${VRP_ROOT}"
VRP_CONSIST="${VRP_CONSIST}:bin|VRP_BIN|${VRP_BIN}"
VRP_CONSIST="${VRP_CONSIST}:usrbin|VRP_USR_BIN|${VRP_USR_BIN}"
VRP_CONSIST="${VRP_CONSIST}:usrlocalbin|VRP_USR_LOCAL_BIN|${VRP_USR_BIN}"
VRP_CONSIST="${VRP_CONSIST}:etc|VRP_ETC|${VRP_ETC}"
VRP_CONSIST="${VRP_CONSIST}:usretc|VRP_USR_ETC|${VRP_USR_ETC}"
VRP_CONSIST="${VRP_CONSIST}:usrlocaletc|VRP_USR_LOCAL_ETC|${VRP_USR_LOCAL_ETC}"
VRP_CONSIST="${VRP_CONSIST}:dev|VRP_DEV|${VRP_DEV}"
VRP_CONSIST="${VRP_CONSIST}:usrman|VRP_USR_MAN|${VRP_USR_MAN}"
VRP_CONSIST="${VRP_CONSIST}:usrlocalman|VRP_USR_LOCAL_MAN|${VRP_USR_LOCAL_MAN}"
VRP_CONSIST="${VRP_CONSIST}:lib|VRP_LIB|${VRP_LIB}"
VRP_CONSIST="${VRP_CONSIST}:usrlib|VRP_USR_LIB|${VRP_USR_LIB}"
VRP_CONSIST="${VRP_CONSIST}:usrlocallib|VRP_USR_LOCAL_LIB|${VRP_USR_LOCAL_LIB}"
VRP_CONSIST="${VRP_CONSIST}:share|VRP_SHARE|${VRP_SHARE}"
VRP_CONSIST="${VRP_CONSIST}:usrshare|VRP_USR_SHARE|${VRP_USR_SHARE}"
VRP_CONSIST="${VRP_CONSIST}:usrlocalshare|VRP_USR_LOCAL_SHARE|${VRP_USR_LOCAL_SHARE}"
VRP_CONSIST="${VRP_CONSIST}:doc|VRP_DOC|${VRP_DOC}"
VRP_CONSIST="${VRP_CONSIST}:usrdoc|VRP_USR_DOC|${VRP_USR_DOC}"
VRP_CONSIST="${VRP_CONSIST}:usrlocaldoc|VRP_USR_LOCAL_DOC|${VRP_USR_LOCAL_DOC}"
VRP_CONSIST="${VRP_CONSIST}:sbin|VRP_SBIN|${VRP_SBIN}"
VRP_CONSIST="${VRP_CONSIST}:usrsbin|VRP_USR_SBIN|${VRP_USR_SBIN}"
VRP_CONSIST="${VRP_CONSIST}:usrlocalsbin|VRP_USR_LOCAL_SBIN|${VRP_USR_LOCAL_SBIN}"
VRP_CONSIST="${VRP_CONSIST}:log|VRP_LOG|${VRP_LOG}"
VRP_CONSIST="${VRP_CONSIST}:usr|VRP_USR|${VRP_USR}"
VRP_CONSIST="${VRP_CONSIST}:usrlocal|VRP_USR_LOCAL|${VRP_USR_LOCAL}"

PACKAGEFILE=${VRP_PACKAGE##*/}
oIFS=$IFS
IFS=":";
for cnunsplit in $VRP_CONSIST; do
	TARNAME=${cnunsplit%%|*}
	[ ! -f ${VRP_TEMP}/${TARNAME}.tar.gz ] && continue
	PACKAGEPART=""
	PACKAGEPART=${cnunsplit#*|}
	LOGICALLOC=${PACKAGEPART%%|*}
	PACKAGEPART=${PACKAGEPART##*|}
	echo "|${LOGICALLOC}=" >> ${VRP_TEMP}/${PACKAGEFILE}.consist
	IFS=${oIFS#??}
	for filename in `/bin/zcat ${VRP_TEMP}/${TARNAME}.tar.gz|/bin/tar t` ; do
		echo "$filename" >> ${VRP_TEMP}/${PACKAGEFILE}.consist
	done
	cd ${VRP_OUTPUT}${PACKAGEPART}; /bin/zcat ${VRP_TEMP}/${TARNAME}.tar.gz|/bin/tar x
	/bin/rm -f ${VRP_TEMP}/${TARNAME}.tar.gz #Save space.
done
/bin/gzip ${VRP_TEMP}/${PACKAGEFILE}.consist
mv ${VRP_TEMP}/${PACKAGEFILE}.consist.gz $VRP_CONSIST_HOME


IFS=$oIFS
';

open(INSTOUT,'>'.$outpath."/install");
print INSTOUT $geninstaller;
close(INSTOUT);

open(FHD,"|/bin/sh -s");
print FHD "OUTP=`pwd` ; cd ".$outpath.' ; tar cf ${OUTP}/'.$packagename.".vrp *";
close(FHD);

open(FHD,"|rm -r ".$outpath."/*");
close(FHD);

sub bdepth {
  my $hashr=shift;
  my $arref=shift;
  my $take=shift @{$arref};
  if(!defined($hashr->{$take})) {
    my %bogushash;
    $hashr->{$take}=\%bogushash;
  }
  bdepth($hashr->{$take},$arref) if(@$arref);
}

sub pdepth {
  my $dir=shift;
  my $dirref=shift;
  my $outfile=shift;
  createtar($outfile,$dir,$dirref) if(shoulddotar($dir,$dirref));
  $outfile="" if($outfile eq "root");
  my $dl=dirlist($dir);
  foreach my $subdir (@$dl) {
    pdepth($dir."/".$subdir,$dirref->{$subdir},$outfile.$subdir) if(defined($dirref->{$subdir}));
  }
}
sub createtar {
  my $outfile=shift;
  my $indir=shift;
  my $dirref=shift;
  my @kdirs=keys %$dirref;
  my $tarcom="/bin/sh -s";
  my $postop="cd ".$indir." \n ".'/bin/tar cfp '.$outpath."/".$outfile.".tar . ";
  foreach my $top (@kdirs) {
    $postop.="--exclude=./".$top." ";
  }
  $postop.="\n /bin/gzip ".$outpath."/".$outfile.".tar \n";
  open(FHD,"|$tarcom");
  print FHD $postop;
  close(FHD);
}

sub shoulddotar {
  my $dir=shift;
  my $dirref=shift;
  my $flist=filelist($dir);
  return 1 if(@$flist);
  my $dlist=dirlist($dir);
  foreach my $curd (@$dlist) {
    return 1 if (!defined($dirref->{$curd}));
  }
  0;
}

sub filelist {
  my $dir=shift;
  opendir(MYDIR,$dir);
  my @dl=grep !-d "$dir/$_", readdir(MYDIR);
  closedir(MYDIR);
  \@dl;
}

sub dirlist {
  my $dir=shift;
  opendir(MYDIR,$dir);
  my @dl=grep -d "$dir/$_", readdir(MYDIR);
  closedir(MYDIR);
  shift @dl;
  shift @dl;
  \@dl;
}
