#? stdtmpl(subsChar = '?') | standard
#proc generateInstallScript(c: ConfigData): string =
#  result = "#!/bin/sh\n# Generated by niminst\n"
#  var proj = c.name.toLowerAscii

set -e

if [ $# -eq 1 ] ; then
#if c.cat[fcUnixBin].len > 0:
  if [ -f "?{c.cat[fcUnixBin][0].toUnix}" ]
  then
    echo "?c.displayName build detected"
  else
    echo "Please build ?c.displayName before installing it"
    exit 1
  fi
#end if
  case $1 in
    "--help"|"-h"|"help"|"h")
      echo "?c.displayName installation script"
      echo "Usage: [sudo] [env DESTDIR=...] sh install.sh DIR"
      echo "Where DIR may be:"
      echo "  /usr/bin"
      echo "  /usr/local/bin"
      echo "  /opt"
      echo "  <some other dir> (treated similar to '/opt')"
      echo "To deinstall, use the command:"
      echo "sh deinstall.sh DIR"
      exit 1
      ;;
    "/usr/bin")
      bindir=$1
      configdir="/etc/?proj"
      libdir="/usr/lib/?proj/lib"
      docdir="/usr/share/?proj/doc"
      datadir="/usr/share/?proj/data"
      nimbleDir="/opt/nimble/pkgs/?c.nimblePkgName-?c.version"
      ;;
    "/usr/local/bin")
      bindir=$1
      configdir="/etc/?proj"
      libdir="/usr/local/lib/?proj/lib"
      docdir="/usr/local/share/?proj/doc"
      datadir="/usr/local/share/?proj/data"
      nimbleDir="/opt/nimble/pkgs/?c.nimblePkgName-?c.version"
      ;;
    "/opt")
      bindir="/opt/?proj/bin"
      configdir="/opt/?proj/config"
      libdir="/opt/?proj/lib"
      docdir="/opt/?proj/doc"
      datadir="/opt/?proj/data"
      nimbleDir="/opt/nimble/pkgs/?c.nimblePkgName-?c.version"
      ;;
    *)
      bindir="$1/?proj/bin"
      configdir="$1/?proj/config"
      libdir="$1/?proj/lib"
      docdir="$1/?proj/doc"
      datadir="$1/?proj/data"
      nimbleDir="$1/?proj"
      ;;
  esac

  bindir="${DESTDIR}${bindir}"
  configdir="${DESTDIR}${configdir}"
  libdir="${DESTDIR}${libdir}"
  docdir="${DESTDIR}${docdir}"
  datadir="${DESTDIR}${datadir}"
  nimbleDir="${DESTDIR}${nimbleDir}"

  mkdir -p "$bindir"
  mkdir -p "$configdir"
  mkdir -p "$libdir"
  mkdir -p "$docdir"
  mkdir -p "$datadir"
  mkdir -p "$nimbleDir"
  echo "copying files..."
#var createdDirs = newStringTable()
#for cat in {fcConfig..fcLib, fcNimble}:
#  for f in items(c.cat[cat]):
#    var mk = splitFile(f.skipRoot).dir
#    if cat != fcNimble:
#      mk = unixDirVars[cat] & "/" & mk
#    else:
#      mk = "$nimbleDir" / splitFile(f).dir
#    end if
#    if mk.len > 0 and not createdDirs.hasKey(mk):
#      createdDirs[mk] = "true"
  mkdir -p "?{mk.toUnix}"
#    end if
#  end for
#end for

#for f in items(c.cat[fcUnixBin]):
  cp "?f.toUnix" "$bindir/?f.skipRoot.toUnix"
  chmod 755 "$bindir/?f.skipRoot.toUnix"
#end for
#for f in items(c.cat[fcConfig]):
  cp "?f.toUnix" "$configdir/?f.skipRoot.toUnix"
  chmod 644 "$configdir/?f.skipRoot.toUnix"
#end for
#for f in items(c.cat[fcData]):
  if [ -f "?f.toUnix" ]; then
    cp "?f.toUnix" "$datadir/?f.skipRoot.toUnix"
    chmod 644 "$datadir/?f.skipRoot.toUnix"
  fi
#end for
#for f in items(c.cat[fcDoc]):
  if [ -f "?f.toUnix" ]; then
    cp "?f.toUnix" "$docdir/?f.skipRoot.toUnix"
    chmod 644 "$docdir/?f.skipRoot.toUnix"
  fi
#end for
#for f in items(c.cat[fcLib]):
  cp "?f.toUnix" "$libdir/?f.skipRoot.toUnix"
  chmod 644 "$libdir/?f.skipRoot.toUnix"
#end for
#for f in items(c.cat[fcNimble]):
  cp "?f.toUnix" "$nimbleDir/?f.toUnix"
  chmod 644 "$nimbleDir/?f.toUnix"
#end for
cp "?{c.nimblePkgName}.nimble" "$nimbleDir/?{c.nimblePkgName}.nimble"
chmod 644 "$nimbleDir/?{c.nimblePkgName}.nimble"

  echo "installation successful"
else
  echo "?c.displayName installation script"
  echo "Usage: [sudo] [env DESTDIR=...] sh install.sh DIR"
  echo "Where DIR may be:"
  echo "  /usr/bin"
  echo "  /usr/local/bin"
  echo "  /opt"
  echo "  <some other dir> (treated similar to '/opt')"
  echo "To deinstall, use the command:"
  echo "sh deinstall.sh DIR"
  exit 1
fi
