#!/usr/bin/env bash

# Usage: cp-p file path
#    OR  cp-p file... directory

# Like cp but implementing preservation of time modified.

if [[ $# -lt 2 ]]; then
    echo "Usage: cp-p [cp options] file directory"
    exit 2
fi

lastarg=${!#}

if [[ -f $lastarg ]]; then
    echo "cp-p: Target file exists."
    exit 3
fi

cp "$@"

if [[ -d $lastarg ]]; then
    for (( i=1; i<$#; i++ ));
      do touch -r ${!i} $lastarg/${!i}
    done
else
    touch -r $1 $2
fi
