script/gentree: clean up custom config applicator to ignore missing cpios
[CDN/Mosi.git] / script / loadconf
1 #!/bin/sh
2
3 _root="$(dirname "${0}")"; . "${_root}/lib/env.sh"
4 want root log
5
6 err "Don't use me yet!"
7
8 pebkac() {
9   echo "Insert help here"
10 }
11
12 while getopts "d:o:h" opt
13 do
14   case "${opt}" in
15     d) dev="${OPTARG}" ;;
16     o) conf="${OPTARG}" ;;
17     h) pebkac ;;
18     [?]) pebkac "Unrecognized option ${opt}" ;;
19   esac
20 done
21 shift $(( $OPTIND - 1 ))
22
23 _dev="${dev}"
24 tgt="${tgt:-${_root}/../src/conf}"
25
26 [ -b "${dev}" -o -c "${dev}" ] || dev="/dev/${dev}"
27 [ -b "${dev}" -o -c "${dev}" ] || err "${_dev}: no such file or directory"
28 [ -d "${tgt}" ] || err "${tgt}: not a directory"
29
30 tmpdir="$(mktemp -d "$(dirname "${tgt}").XXXXXXXX")"
31 tmpmnt="$(mktemp -d "/tmp/conf.XXXXXXXX")"
32 trap "umount -f '${tmpmnt}'; rm -Rf '${tmpdir}' '${tmpmnt}'" exit hup int term kill
33 chk cd "${tmpdir}"
34 chk mount "${dev}" "${tmpmnt}"
35
36 ( zcat "${tmpmnt}/default/etc.cpio.gz" | cpio -id ) || chk
37 ( zcat "${tmpmnt}/default/var.cpio.gz" | cpio -id ) || chk
38 chk umount "${tmpmnt}"
39 chk rm -Rf "${tmpmnt}"
40 rm -Rf "${tgt}.old"
41 chk mv "${tgt}" "${tgt}.old"
42 chk mv "${tmpdir}" "${tgt}"
43 trap "" exit hup int term kill