#!/bin/bash
set -euf
if [[ ! "$#" = 1 ]]; then
echo "You need to supply the folder containing p4merge as arguement"
exit 1
elif [[ ! -d "$1" ]]; then
echo "The file is not a folder"
exit 1
elif [[ ! "$(find $1 -type f | grep p4merge)" ]]; then
echo "The folder does not contain p4merge"
exit 1
fi
[[ $EUID -eq 0 ]] && dest='/opt/p4merge' || dest="$HOME/p4merge"
[[ $EUID -eq 0 ]] && lnk='/usr/local/bin' || lnk="$HOME/bin"
# remove the destination folder if it exists
rm -rf "$dest"
# Create the directory
mkdir -p "$dest"
mkdir -p "$lnk"
# copy the files to the new directory
cp -r "$1/." "$dest"
# Create a link to the tool and put it somewhere accessible
ln -sf "$dest/bin/p4merge" "$lnk/p4merge"
if [[ ! "$(echo $PATH | grep "$lnk")" ]]; then
echo "Add the following to your .profile or .bash_profile"
echo 'export PATH='"$lnk"':"$PATH"'
fi
echo -e 'Run the following commands to configure git to use p4merge:\n'
echo 'git config --global merge.tool p4merge'
echo 'git config --global mergetool.prompt false'
echo 'git config --global mergetool.p4merge.path'" $lnk/p4merge"
echo 'git config --global diff.tool p4merge'
echo 'git config --global difftool.prompt false'
echo 'git config --global difftool.p4merge.path'" $lnk/p4merge"