#!/bin/bash

set -eu
set -o pipefail

from_dir=.
to_dir=

function usage
{
    retval="$1"
    case "$retval" in
        0)
            ;;
        *)
            exec 1>&2
            ;;
    esac
    echo "Usage:"
    echo "cd to the directory hierarchy you want to link from."
    echo "Then $0 --to-dir dir/ect/ory2"
    echo
    echo Hard links files from CWD to destination directory, recursively.
    echo
    echo The point of this script is to create a hierarchy of links, sans
    echo .views files and with all-new directories. This facilitates
    echo watching the same videos at different paces at different locations.
    exit "$retval"
}

from_dir=.

while [ "$#" -ge 1 ]
do
    case "$1" in
        --to-dir)
            to_dir="$2"
            shift
            ;;
        -h|--help)
            usage 0
            ;;
        *)
            echo "$0: Unrecognized option: $1" 1>&2
            usage 1
            ;;
    esac
    shift
done

case "$to_dir" in
    "")
        echo "$0: --to-dir is a required option" 1>&2
        usage 1
        ;;
esac

# Intentionally not set -eu'ing in this subshell
if ! (cd "$to_dir" 2>&1)
then
    echo "$0: $to_dir does not exist, or not relative to $from_dir" 1>&2
    usage 1
fi

find . -name '*.views' -prune -o -type f -print | \
    cpio -pdl "$to_dir"