#? 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 test -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] 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=/usr/bin
      configdir=/etc/?proj
      libdir=/usr/lib/?proj
      docdir=/usr/share/?proj/doc
      datadir=/usr/share/?proj/data
      nimbleDir="/opt/nimble/pkgs/?c.nimblePkgName-?c.version"
      ;;
    "/usr/local/bin")
      bindir=/usr/local/bin
      configdir=/etc/?proj
      libdir=/usr/local/lib/?proj
      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"
      mkdir -p /opt/?proj
      mkdir -p $bindir
      mkdir -p $configdir
      ;;
    *)
      bindir="$1/?proj/bin"
      configdir="$1/?proj/config"
      libdir="$1/?proj/lib"
      docdir="$1/?proj/doc"
      datadir="$1/?proj/data"
      nimbleDir="$1/?proj"
      mkdir -p $1/?proj
      mkdir -p $bindir
      mkdir -p $configdir
      ;;
  esac

  mkdir -p $libdir
  mkdir -p $docdir
  mkdir -p $configdir
  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] 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
