awk	'
#Name:		
#Usage:		rmfileset  logfile_name
#Result:	remove all the files content in a log file generated by 
#               tar -tvf tarfile >logfile
#         but do not remove files listed in notrm_logfile
#         empty directories are  removed
#Author:	G. Pepellin ESRF
#Comments:
BEGIN {	
  print "Hello, welcome to rm_tarfileset that will remove the files that "
  print "  were extracted from a tar file if you have used the following"
  print "  way to extract these files:" 
  print "                tar -tvf tarfile >uncustomize/xxx.uncustomize"
  print "                tar -xvf tarfile "
  print "  You will prompt to work in interactive or automatic mode"
  print "  and for another logfile that contains files not to be removed\n"

########### Promp user to what has to be done ##################
# Prompt user for the master logfile name. exit if it does not exist
  printf "What is the name of the uncustomize file (or \ to abort): "
  getline
  if($0 == "\\") exit
  if (system("test -f " $0) != 0)
  {
    printf "Sorry the file %s does not exist\n", $0
    exit
   }
  logfile = $0

# Prompt user for the mode he wants (automatic or interactif)
  $0 = "nill"
  while( ($0 != "i") && ($0 != "a") && ($0 != "\\") )
  {
      printf "Choose the mode you want to proceed \n"
      printf "   interractive or automatic [i or a or \ to abort] :"
      getline    
  }
  if($0 == "\\") exit    
  if($0 == "a") interactif = " "
  if($0 == "i") interactif = " -i "

# Prompt user if he wants to remove all the files or keep files
#        listed in a not_to_be_removed log file
  $0 = "nill"
  while( ($0 != "y") && ($0 != "n") && ($0 != "\\") )
  {
      printf "Do you want to keep some files [y or n or \ to abort] :"
      getline    
  }
  if($0 == "\\") exit
  if($0 == "n")  allfiles = "TRUE"
  if($0 == "y")
  {
    allfiles = "FALSE"
    printf "Name of the logfile that contains files to be kept (or \ to abort): "
    getline
    if($0 == "\\") exit
    notrm_file = $0
    if (system("test -f " $0) != 0)
    {
      printf "Sorry the file %s does not exist\n", $0
      exit
    }
  }


############ Read the logfile that contains files that have not to #########
############   be removed into the array notrm_A                   #########
  if (allfiles == "FALSE")
  {
    while ((getline < notrm_file) >0)
    {
      notrm_A[$1] = $0
    }
  }
#  for (x in notrm_A) print notrm_A[x]

####### Remove files and save directory created in dir_created ########
	while ((getline < logfile) >0) 
	{
   split ($0,f," ")
	 #printf "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" ,\
	 #        f[1],f[2],f[3],f[4],f[5],f[6],f[7],f[8]
# test if the current record corresponds either to a directory 
#      or to a file created by tar -xvf
   if(f[8] !~ /\/$/)  #f[8] contains the last field of the log line  
   { # we deal with a file, isolate extension and create wildename = *.extension
     filename = f[8]
     nbfields = split(filename,A1,".")
     extension = A1[nbfields]
     wildename = sprintf("*.%s",extension)     
     if( (filename in notrm_A) || \
          (wildename in notrm_A) || \
          (filename ==  "uncustomize_fileset") )
     { # It must not be removed. Its name is saved into files_not_removed
       printf "%s was asked to be kept\n",filename
       printf "%s\n",filename >"files_not_removed"
     } #ends if clause of: if( (filename in notrm_A) || (wildename in notrm_A) 
     else
     { # It is removed
       printf "remove: %s\n " , f[8]  
       system("rm " interactif f[8]) 
     } #ends else clause of: if( (filename in notrm_A) || (wildename in notrm_A) 
   }#ends if clause of: f(f[8] !~ /\/$/
     
   else
   { # we deal with a directory. Its name is saved into dir_created
     printf "%s is a directory\n", f[8]
     printf "%s\n", f[8] > "dir_created"
   } #ends else clause of: if(f[8] !~ /\/$/)
	} #ends while loop
#  Sort in reverse order dir_created to get the much more deph directories 
#  first
  system("sort -r -o dir_created dir_created")

# close files
	close logfile
  close "dir_created"
  close "files_not_removed"

###### remove the empty directories and saved the name of not removed #######
######  directories into dir_not_removed                              #######
	while ((getline < "dir_created") >0) 
  {
    printf "Try to remove the directory %s\n", $0
    if (system("rmdir " $0) != 0)
    {
#     printf "%s\n", $0 > "dir_not_removed"      
    }
  }
# Sort in reverse order dir_created to get the much more deph directories first
  system("sort -r -o dir_not_removed dir_not_removed")

# close opened files 
	close "dir_created"
  close "dir_not_removed"
  close "files_not_removed"
# remove unnecessary working files
  system("rm dir_created")
# exit
}'  






