#! /usr/bin/env tclsh
# -*- tcl -*-
# <20200223.1512.21>
#
# This basically a replacement for:
proc squak {pre} {
  if {$pre != {}} {
    puts "$pre\n"
  }
  puts "Syntax: toUnix.tcl {-f <name> | source} \[-u mask\] dest\
        \n\nCopy files from source to dest AND \
        \nIf the path required for 'dest' does not exist the needed dirs are created AND\
        \nThe output is always in unix mode (i.e. always a lf) AND\
        \n-f <name>  supplies a list of files to copy AND\
        \n-u <umask> supplies a mask which will be used for all\
        \n           created file. Default is to use the source files permissions."
  exit 1
}
#

#
# Get the instructions and create a list of files to copy


proc putsd {m} {puts "$m"}
proc putsd {m} {}             ;# comment this line for debug messages

set MSW [expr {$tcl_platform(platform) eq "windows"}]

set arg $argv
set sourcelist {}
set dest {}
set umask {}

while {$arg !={}} {
  set arg [lassign $arg this]
  if {$this == "-f"} {
    set arg [lassign $arg x]
    set r [catch {split [read [set pid [open $x r]]] \n} sourcelist]
    if {$r != 0} {
      squak "Failed to open $x: $sourcelist"
      exit 1
    }
    close $pid
  } elseif {$this == "-u"} {
    set arg [lassign $arg umask]
  } elseif {$sourcelist == {}} {
    set sourcelist [file join $this]
  } else {
    set dest $this
    if {$arg != {}} {
      puts "Too many arguments: $argv"
    }
  }
}

# a couple of sanity checks...
if {$umask != {} &&\
	([string index $umask 0] != "0"\
	|| $umask < 1\
	|| $umask > 0777)} {
  squak "This looks like a bad umask \"$umask\""
}
if {$dest == {}} {
  squak "No destination!"
}

putsd "s:$sourcelist d:$dest"
foreach file $sourcelist { 
  # Get a proven source before we try the destination
  if {$file == {}} {continue}
  regsub -all {\{|\}} $file {} fileN
  # puts "$file \n$fileN"
  set fileN [file norm $fileN]
  if {![file exists $fileN]} {
    puts "File \"$fileN\" (from \"$file\") not found!"
    continue
  }
  # now lets figure the destination
  set to [file norm [file join $dest [regsub -all {\{|\}} $file {}]]]
  putsd "$to\n$file"
  if {![file isdir $fileN]} {
    # puts "$to"
    # exit
    file mkdir [file dir $to]
  } else {
    continue
  }
  # ok all looks good, lets copy the data...
  putsd "copying $fileN to $to"
  if {$fileN == $to} {
    puts "PANIC: You are attempting to copy a file into itself"
    exit 1
  } 
  set sPid [open $fileN r]
  set dPid [open $to  w]
  if {!$MSW} {
    if {$umask == {}} {
      set perm [file attributes $fileN -permissions]
    } else {
      set perm $umask
    }
    file attributes $to -permissions $perm
  }
  # Introduce a bit of paranoia...
  # If we change the translation of files that are not text we can
  # make them useless...
  #
  chan configure $sPid -translation binary
  set k [read $sPid 400]
  chan configure $sPid -translation auto
  # we only translate crlf (\c\n) if we find them
  if {[file extension $fileN] ni {.gz .tar .so} && \
	  [set first [string first \r\n $k]] != -1 &&\
	  [string last \r\n $k] != $first} {
    chan configure $dPid -translation {auto lf}
    puts "configured translation for $file"
  } else {
    chan configure $dPid -translation binary
    chan configure $sPid -translation binary
  }
  chan seek $sPid 0
  chan copy $sPid $dPid
  chan close $sPid
  chan close $dPid
}
puts "$argv0 copy finished"
exit 0
