#!/usr/bin/env Rscript

# Author: E.Korsching  2024
###############################################################################


#### unify the  .bash_history  file using this R script

# r script (placed in bin) run by Rscript
# cmd:  Rscript ./bin/uni_bash_hist.R /home/korschi/.bash_history

# process input
args <- commandArgs(trailingOnly=T)
args.l <- length(args)
# test if there is at least one argument
if(args.l==0|args.l>1){ stop("no file path given") }
file <- as.character(args[1])
if(!file.exists(file)){ stop(paste("file does not exist: ",file,sep="")) }
cat("\n R - remove duplicates in")
cat("\n",file)

charvec <- read.table(file, header=F, sep="\t", quote="")
nr1 <- nrow(charvec)
uni <- unique(charvec[ ,1])
nr2 <- length(uni)

cat("\n in lines ",nr1," out lines ",nr2,"\n\n")

# input file is renamed - if .bak is existing it is overwritten
file.old <- paste(file, ".", format(Sys.time(), format="%Y%m%d-%H%M"), sep="")
file.rename(file, file.old)
cat("\n new file is now:    ",file)
cat("\n backup file is now: ",file.old,"\n\n")
# a new file is written
write.table(uni, file=file, append=F, quote=F, sep="\t", row.names=F, col.names=F)


