#!/usr/bin/perl

&print_usage if ($#ARGV < 1);

use Getopt::Std;
getopts("admtcvh");

$datafile = $ARGV[0];

&print_usage() if $opt_h;


sub print_usage {
    print "$0 [-avcmth] <datafile>\n";
    print " -m : create (e)maketag(.ez)\n";
    print " -t : cretate tagfiles \n";
    print " -d : cretate (e)diskXXX \n";
    print " -a : create all \n";
    print " -c : check tgz files \n";
    print " -v : be vorbose \n";
    print " -h : show this message \n";
    print " <datafile> : diskset description file \n";
    exit;
}

sub truncate {
    $old = $_[0];
    $new = $old;
    $new =~ s/\n//g;
    $new =~ s/^[\s]+//;
    $new =~ s/[\s]+$//;
    return ($new);
}

$/ = "";
if ( ! -f $datafile) {
    print "cannot find diskset description file!! \n";
    exit;
}
open (IN, "$datafile");
$tmpdt = <IN>;
($set, $kind) = split(/&/, $tmpdt);
$set = &truncate($set);
$kind = &truncate($kind);
print "set:$set, kind:$kind\n" if $opt_v;
@lines = <IN>;
close(IN);
for $i (@lines) {
    ($d1, $d2, $d3, $d4, $d5, $d6, $d7) = split(/&/, $i);
    for $j (1..7) {
	$tmp = "d".$j;
	$$tmp = &truncate($$tmp);
    }
    push(@tmplist, $d1);
    $tag_s{$d1}  = $d2;
    $tag_m{$d1}  = $d3;
    $jtitle{$d1} = $d4;
    $jdesc{$d1}  = &format($d5);
    $etitle{$d1} = $d6;
    $edesc{$d1}  = &format($d7);
}
@filelist = sort( @tmplist );

&checkfiles() if $opt_c;

if ( $opt_a ) {
    $opt_t = $opt_d = $opt_m = $opt_a;
}

if ( $opt_d ) {
    &diskfile;
    &ediskfile;
}

if ( $opt_m ) {
    &maketag;
    &maketag_ez;
    &emaketag;
    &emaketag_ez;
}

if ( $opt_t ) {
    &tagfiles;
}

sub format {
    require 'fold.pl';
    $text = $_[0];
    my $bytes = 60;
    my $results = "";
    while (length($text)) {
	(my $folded, $text) = fold($text, $bytes,0,1,'a', 1);
	$results = $results.$folded."\n";
    }
    return $results;
}

sub diskfile {
    $name = "disk".$set;
    print "making $name\n" if $opt_v;
    system("mv $name $name.old") if ( -f $name);

    open (OUT, ">$name");
    print OUT "CONTENTS: ";
    for $i (@filelist) {
	print OUT "$i ";
    }
    print OUT "\n";
    for $i (@filelist) {
	@tmplines = split(/\n/, $jdesc{$i});
	printf(OUT "%-9.9s %s\n", $i.":", $jtitle{$i});
	printf(OUT "%-9.9s \n", $i.":");
	for ($j = 2; $j < 11; $j++) {
	    printf(OUT "%-9.9s %s\n", $i.":", $tmplines[$j-2]);
	}
    }
    close(OUT);
    chmod(0664, $name);
}

sub ediskfile {
    $name = "edisk".$set;
    print "making $name\n" if $opt_v;
    system("mv $name $name.old") if ( -f $name);

    open (OUT, ">$name");
    print OUT "CONTENTS: ";
    for $i (@filelist) {
	print OUT "$i ";
    }
    print OUT "\n";
    for $i (@filelist) {
	@tmplines = split(/\n/, $edesc{$i});
	printf(OUT "%-9.9s %s\n", $i.":", $etitle{$i});
	printf(OUT "%-9.9s \n", $i.":");
	for ($j = 2; $j < 11; $j++) {
	    printf(OUT "%-9.9s %s\n", $i.":", $tmplines[$j-2]);
	}
    }
    close(OUT);
    chmod(0664, $name);
}

sub maketag {
    print "making maketag\n" if $opt_v;
    system("mv maketag maketag.old") if ( -f "maketag");

    open (OUT, ">maketag");
    print OUT "#!/bin/sh\n";
    print OUT "cat /dev/null > /tmp/SeTnewtag\n";
    print OUT "dialog --title \"$set($kind)シリーズの選択\" \\\n";
    print OUT "  --checklist \"$set シリーズの中からインストールしたいパッケージを \\\n";
    print OUT "選んでください．カーソルの上下キーで \\\n";
    print OUT "対象を選択し，space キーでマーク(X)します． \\\n";
    print OUT "Enter キーでインストールを開始します．\" 24 72 15 \\\n";
    for $i (@filelist) {
	printf(OUT "\"%s\" \"%s\" \"%s\" \\\n", $i, $jtitle{$i}, $tag_s{$i} =~ /[Yy]/ ? "on" : "off");
    }
    print OUT "2> /tmp/SeTpkgs\n";
    print OUT "if [ $? = 1 -o $? = 255 ]; then\n";
    print OUT "  rm -f /tmp/SeTpkgs\n";
    print OUT "  > /tmp/SeTnewtag\n";
    print OUT "  for pkg in ";
    for $i (@filelist){ print OUT "$i "};
    print OUT " ; do \n";
    print OUT "  echo \"\$pkg: SKP\" >> /tmp/SeTnewtag\n";
    print OUT "  done\n";
    print OUT "  exit\n";
    print OUT "fi\n";
    print OUT "cat /dev/null > /tmp/SeTnewtag\n";
    print OUT "for PACKAGE in ";
    for $i (@filelist) {print OUT "$i "};
    print OUT " ; do\n";
    print OUT "    if grep \\\"\$PACKAGE\\\" /tmp/SeTpkgs 1> /dev/null 2> /dev/null ; then\n";
    print OUT "        echo \"\$PACKAGE: ADD\" >> /tmp/SeTnewtag\n";
    print OUT "    else echo \"\$PACKAGE: SKP\" >> /tmp/SeTnewtag\n";
    print OUT "    fi\n";
    print OUT "done\n";
    print OUT "rm -f /tmp/SeTpkgs\n";

    close(OUT);
    chmod(0664, "maketag");
}

sub emaketag {
    print "making emaketag\n" if $opt_v;
    system("mv emaketag emaketag.old") if ( -f "emaketag");

    open (OUT, ">emaketag");
    print OUT "#!/bin/sh\n";
    print OUT "cat /dev/null > /tmp/SeTnewtag\n";
    print OUT "dialog --title \"select pkgs from $set($kind)\" \\\n";
    print OUT "  --checklist \"select packages from $set series. \\\n";
    print OUT "You can move cursor with UP/DOWN key and push space \\\n";
    print OUT "key to select pkgs. After finish selecting, \\\n";
    print OUT "push Enter to start installation. \" 24 72 15 \\\n";
    for $i (@filelist) {
	printf(OUT "\"%s\" \"%s\" \"%s\" \\\n", $i, $etitle{$i}, $tag_s{$i} =~ /[Yy]/ ? "on" : "off");
    }
    print OUT "2> /tmp/SeTpkgs\n";
    print OUT "if [ $? = 1 -o $? = 255 ]; then\n";
    print OUT "  rm -f /tmp/SeTpkgs\n";
    print OUT "  > /tmp/SeTnewtag\n";
    print OUT "  for pkg in ";
    for $i (@filelist){ print OUT "$i "};
    print OUT " ; do \n";
    print OUT "  echo \"\$pkg: SKP\" >> /tmp/SeTnewtag\n";
    print OUT "  done\n";
    print OUT "  exit\n";
    print OUT "fi\n";
    print OUT "cat /dev/null > /tmp/SeTnewtag\n";
    print OUT "for PACKAGE in ";
    for $i (@filelist) {print OUT "$i "};
    print OUT " ; do\n";
    print OUT "    if grep \\\"\$PACKAGE\\\" /tmp/SeTpkgs 1> /dev/null 2> /dev/null ; then\n";
    print OUT "        echo \"\$PACKAGE: ADD\" >> /tmp/SeTnewtag\n";
    print OUT "    else echo \"\$PACKAGE: SKP\" >> /tmp/SeTnewtag\n";
    print OUT "    fi\n";
    print OUT "done\n";
    print OUT "rm -f /tmp/SeTpkgs\n";

    close(OUT);
    chmod(0664, "emaketag");
}

sub maketag_ez {
    print "making maketag_ez\n" if $opt_v;
    system("mv maketag.ez maketag.ez.old") if ( -f "maketag.ez");

    open (OUT, ">maketag.ez");
    print OUT "#!/bin/sh\n";
    print OUT "cat /dev/null > /tmp/SeTnewtag\n";
    print OUT "dialog --title \"$set($kind)シリーズの選択\" \\\n";
    print OUT "  --checklist \"$set シリーズの中からインストールしたいパッケージを \\\n";
    print OUT "選んでください．システムに必須のパッケージは \\\n";
    print OUT "自動的にインストールされるので表示されません． \\\n";
    print OUT "カーソルの上下キーで対象を選択し， \\\n";
    print OUT "space キーでマーク(X)します． \\\n";
    print OUT "Enter キーでインストールを開始します．\" 24 72 12 \\\n";
    for $i (@filelist) {
	if ($tag_s{$i} !~ /[Yy]/ ) {
	    printf(OUT "\"%s\" \"%s\" \"%s\" \\\n", $i, $jtitle{$i}, $tag_s{$i} =~ /[Yy]/ ? "on" : "off");
	}
    }
    print OUT "2> /tmp/SeTpkgs\n";
    print OUT "if [ $? = 1 -o $? = 255 ]; then\n";
    print OUT "  rm -f /tmp/SeTpkgs\n";
    print OUT "  > /tmp/SeTnewtag\n";
    print OUT "  for pkg in ";
    for $i (@filelist){ print OUT "$i "};
    print OUT " ; do \n";
    print OUT "  echo \"\$pkg: SKP\" >> /tmp/SeTnewtag\n";
    print OUT "  done\n";
    print OUT "  exit\n";
    print OUT "fi\n";
    print OUT "cat /dev/null > /tmp/SeTnewtag\n";
    print OUT "for PACKAGE in ";
    for $i (@filelist) {
	if ( $tag_s{$i} =~ /[Yy]/ ) {
	    print OUT "$i ";
	}
    }
    print OUT " ; do\n";
    print OUT "    echo \"\$PACKAGE: ADD\" >> /tmp/SeTnewtag\n";
    print OUT "done\n";
    print OUT "for PACKAGE in ";
    for $i (@filelist) {
	if ( $tag_s{$i} !~ /[Yy]/) {
	    print OUT "$i ";
	}
    }
    print OUT "  ; do\n";
    print OUT "    if grep \\\"\$PACKAGE\\\" /tmp/SeTpkgs 1> /dev/null 2> /dev/null ; then\n";
    print OUT "        echo \"\$PACKAGE: ADD\" >> /tmp/SeTnewtag\n";
    print OUT "    else echo \"\$PACKAGE: SKP\" >> /tmp/SeTnewtag\n";
    print OUT "    fi\n";
    print OUT "done\n";
    print OUT "rm -f /tmp/SeTpkgs\n";

    close(OUT);
    chmod(0664, "maketag.ez");
}

sub emaketag_ez {
    print "making emaketag_ez\n" if $opt_v;
    system("mv emaketag.ez emaketag.ez.old") if ( -f "emaketag.ez");

    open (OUT, ">emaketag.ez");
    print OUT "#!/bin/sh\n";
    print OUT "cat /dev/null > /tmp/SeTnewtag\n";
    print OUT "dialog --title \"select pkgs from $set($kind)\" \\\n";
    print OUT "  --checklist \"select packages from $set series. Mandatory packages  \\\n";
    print OUT "are automatically selected and wouldn't apper this list. \\\n";
    print OUT "Move cursor with UP/DOWN key and push space key to select(mark). \\\n";
    print OUT "Push Enter to start install. \" 24 72 14 \\\n";
    for $i (@filelist) {
	if ($tag_s{$i} !~ /[Yy]/ ) {
	    printf(OUT "\"%s\" \"%s\" \"%s\" \\\n", $i, $etitle{$i}, $tag_s{$i} =~ /[Yy]/ ? "on" : "off");
	}
    }
    print OUT "2> /tmp/SeTpkgs\n";
    print OUT "if [ $? = 1 -o $? = 255 ]; then\n";
    print OUT "  rm -f /tmp/SeTpkgs\n";
    print OUT "  > /tmp/SeTnewtag\n";
    print OUT "  for pkg in ";
    for $i (@filelist){ print OUT "$i "};
    print OUT " ; do \n";
    print OUT "  echo \"\$pkg: SKP\" >> /tmp/SeTnewtag\n";
    print OUT "  done\n";
    print OUT "  exit\n";
    print OUT "fi\n";
    print OUT "cat /dev/null > /tmp/SeTnewtag\n";
    print OUT "for PACKAGE in ";
    for $i (@filelist) {
	if ( $tag_s{$i} =~ /[Yy]/ ) {
	    print OUT "$i ";
	}
    }
    print OUT " ; do\n";
    print OUT "    echo \"\$PACKAGE: ADD\" >> /tmp/SeTnewtag\n";
    print OUT "done\n";
    print OUT "for PACKAGE in ";
    for $i (@filelist) {
	if ( $tag_s{$i} !~ /[Yy]/) {
	    print OUT "$i ";
	}
    }
    print OUT "  ; do\n";
    print OUT "    if grep \\\"\$PACKAGE\\\" /tmp/SeTpkgs 1> /dev/null 2> /dev/null ; then\n";
    print OUT "        echo \"\$PACKAGE: ADD\" >> /tmp/SeTnewtag\n";
    print OUT "    else echo \"\$PACKAGE: SKP\" >> /tmp/SeTnewtag\n";
    print OUT "    fi\n";
    print OUT "done\n";
    print OUT "rm -f /tmp/SeTpkgs\n";

    close(OUT);
    chmod(0664, "emaketag.ez");
}

sub tagfiles {
    print "making tagfiles\n" if $opt_v;

    for $i ('tagfile', 'tagfile.l', 'tagfile.m', 'tagfile.s') {
	system("mv $i $i.old") if ( -f $i );
    }

    open (OUT, ">tagfile");
    for $i (@filelist) {
	printf(OUT "%-9.9s %s\n", $i.":", $tag_m{$i} =~ /[Yy]/ ? "ADD": "SKP");
    }
    close(OUT);
    chmod (0664, "tagfile");

    open (OUT, ">tagfile.l");
    for $i (@filelist) {
	printf(OUT "%-9.9s ADD\n", $i.":");
    }
    close(OUT);
    chmod (0664, "tagfile.l");

    open (OUT, ">tagfile.m");
    for $i (@filelist) {
	printf(OUT "%-9.9s %s\n", $i.":", $tag_m{$i} =~ /[Yy]/ ? "ADD": "SKP");
    }
    close(OUT);
    chmod (0664, "tagfile.m");

    open (OUT, ">tagfile.s");
    for $i (@filelist) {
	printf(OUT "%-9.9s %s\n", $i.":", $tag_s{$i} =~ /[Yy]/ ? "ADD": "SKP");
    }
    close(OUT);
    chmod (0664, "tagfile.s");
}

sub checkfiles {
    opendir (DIR, ".");
    @tmpfiles = sort grep { /tgz$/ } readdir(DIR);
    closedir(DIR);
    for $i ( @tmpfiles ) {
	$i =~ s/.tgz//;
	push (@tgzs, $i);
    }

    $warn = 0;
    for $i ( @filelist ) {
	$match = 0;
	for $j ( @tgzs ) {
	    if ( $i eq $j ) {
		$match = 1;
		break;
	    }
	}
	if ($match != 1) {
	    print "$i is in descfile($datafile) but not $i.tgz found here.\n";
	    $warn++;
	}
    }
    for $i ( @tgzs ) {
	$match = 0;
	for $j ( @filelist ) {
	    if ( $i eq $j ) {
		$match = 1;
		$break;
	    }
	}
	if ( $match != 1 ) {
	    print "$i.tgz exists but not in descfile($datafile)\n";
	    $warn++;
	}
    }
    if ( $warn == 0 ) {
	print "No error found\n";
    }
    else {
	print "Total warn: $warn\n";
    }
}
