#!/usr/bin/perl

# perl script to maketag(and emaketag)
# on/off の選択肢は tagfile.dkc を使うので jnl1/wnn1 あたりは手動で
# 調整が必要

use Cwd;
$dir = cwd();
(@path) = split(/\//, $dir);
$here = $path[$#path];  # last element of path list
$discfile = "disk".$here;
open(DESC, $discfile) || die "cannot open $discfile";
$cont = <DESC>;
while(<DESC>){
    chomp;
    ($name, $desc) = split(/:\s+/);
    if ($old_desc !~ $name) {
	$desc{$name} = $desc;
    }
    $old_desc = $name;
}

open(SESAME, "ls *.tgz |");
@files = <SESAME>;
close(SESAME);
for $i ( 0 .. $#files){
    $files[$i] =~ s/\.tgz\n//;
}

open(TAGS, "tagfile.dkc");
while(<TAGS>){
    chomp;
    $_ =~ s/ //g;
    ($pkg, $inst) = split(/:/);
    $tag{$pkg} = $inst;
}

for $i ( 0 .. $#files) {
  $check = 0;
  for $j (keys %tag){
    if ($files[$i] eq $j) { $check = 1 ; next; }
  }
  if (!$check){print "$files[$i] is not in taglist";}
}

print << 'End_of_File';
#!/bin/sh
#item   ####description                                      ###on off ###
cat /dev/null > /tmp/SeTnewtag 
dialog --title "hoge(hoge) シリーズの選択" \
       --checklist "hogeシリーズの中からインストールしたいパッケージを \
選んでください。カーソルの上下キーで \
動かし、選択は SPACE キーです。 \
選択が終われば ENTER を押してください。" 24 72 16 \
End_of_File

for $i ( 0 .. $#files){
    print "\"$files[$i]\" \"$desc{$files[$i]}\" ";
    if ($tag{$files[$i]} =~ ADD) { print "\"on\" \\\n";}
    else { print "\"off\" \\\n";}
}

print << 'End_of_File';
2> /tmp/SeTpkgs
if [ $? = 1 -o $? = 255 ]; then 
 rm -f /tmp/SeTpkgs 
 > /tmp/SeTnewtag 
End_of_File

print "for pkg in ";
for $i ( 0..$#files ){
    print "$files[$i] ";
}

print << 'End_of_File';
; do
  echo "$pkg: SKP" >> /tmp/SeTnewtag 
 done 
 exit 
fi 
cat /dev/null > /tmp/SeTnewtag
End_of_File

print "for PACKAGE in ";
for $i ( 0..$#files ){
    print "$files[$i] ";
}
print << 'End_of_File';
; do
  if fgrep \"$PACKAGE\" /tmp/SeTpkgs 1> /dev/null 2> /dev/null ; then
echo "$PACKAGE: ADD" >> /tmp/SeTnewtag
 else  echo "$PACKAGE: SKP" >> /tmp/SeTnewtag
 fi
done
rm -f /tmp/SeTpkgs
End_of_File

