#!/usr/bin/env bash

DEST=${1:-/etc/skel}

sudo apt install -y quilt

# https://www.debian.org/doc/manuals/maint-guide/modify.en.html

# It's useful to have a slightly customized default, so let's create an alias dquilt for Debian packaging by adding the following lines to ${DEST}/.bashrc
#  The second line provides the same shell completion feature of the quilt command to the dquilt command

# BASH aliases
if [[ ! -f ${DEST}/.bash_aliases ]]; then
	touch ${DEST}/.bash_aliases
fi

if [[ -f ${DEST}/.bash_aliases ]]; then
cat >> ${DEST}/.bash_aliases <<'EOF' 
if [[ -d ${DEST}/.bash_aliases.d ]]; then
	for alias_file in ${DEST}/.bash_aliases.d/*
	do
		source "$alias_file"
	done
fi
EOF
fi

mkdir -p ${DEST}/.bash_aliases.d
cat > ${DEST}/.bash_aliases.d/10_quilt <<EOF
alias dquilt="quilt --quiltrc=${DEST}/.quiltrc-dpkg"
complete -F _quilt_completion -o filenames dquilt
EOF

cat > ${DEST}/quiltrc-dpkg <<'EOF'
d=. ; while [ ! -d $d/debian -a $(readlink -e $d) != / ]; do d=$d/..; done
if [ -d $d/debian ] && [ -z $QUILT_PATCHES ]; then
    # if in Debian packaging tree with unset $QUILT_PATCHES
    QUILT_PATCHES="debian/patches"
    QUILT_PATCH_OPTS="--reject-format=unified"
    QUILT_DIFF_ARGS="-p ab --no-timestamps --no-index --color=auto"
    QUILT_REFRESH_ARGS="-p ab --no-timestamps --no-index"
    QUILT_COLORS="diff_hdr=1;32:diff_add=1;34:diff_rem=1;31:diff_hunk=1;33:diff_ctx=35:diff_cctx=33"
    if ! [ -d $d/debian/patches ]; then mkdir $d/debian/patches; fi
fi
EOF

# ZSH aliases
mkdir -p ${DEST}/.zsh_aliases.d
cat > ${DEST}/.zsh_aliases.d/010_quilt.sh <<'EOF'
alias dquilt="quilt --quiltrc=${DEST}/.quiltrc-dpkg"
compdef dquilt="quilt"
setopt complete_aliases
EOF


echo "See. quilt(1) and /usr/share/doc/quilt/quilt.pdf.gz on how to use quilt."



